Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Nested switch statement is printing the incorrect answer

import java.io.*;
import java.util.*;

public class Planets 
{

    public static void main(String[] args) throws IOException
    {
        Scanner in;
        in = new Scanner(System.in);
        boolean runAgain = true;
        System.out.println("Starting program...\n\n");
        
        double weight = getWeight();
        
        while(runAgain)
        {
            String planet = getPlanet();
            calculateWeight(weight, planet);
            
            System.out.println("\nWould you like to see your weight on a different planet? (Yes or No)");
            String input = in.nextLine();
            if(input.charAt(0)=='N' || input.charAt(0)=='n')
            {
                runAgain = false;
            }
        }
            

    } //end main
    
    public static double getWeight() throws IOException
    { //Prompts user to enter their weight
        Scanner in;
        in = new Scanner(System.in);
        System.out.println("Greetings! This program calculates how much you would weigh on other planets.\n\nPlease enter your weight (lbs.): ");
        double weight = in.nextDouble();
        return weight;
    }
    
    public static String getPlanet() throws IOException
    { //Prompts user to enter their planet
        Scanner in;
        in = new Scanner(System.in);
        System.out.println("\nChoose your planet:\nSun\nMoon\nSaturn\nMercury\nMars\nUranus\nVenus\nJupiter\nNeptune");
        String planet = in.nextLine();
        return planet;
    }
    
    public static void calculateWeight(double weight, String planet) throws IOException
    { //Outputs the weight of the given planet ; takes two parameters weight and planet
        switch(planet.charAt(0))
        {
            case 's':
            case 'S':
                switch(planet.charAt(1))
                {
                case 'u':
                case 'U':
                    System.out.println("Your weight on the Earth is " + weight + " lbs.\nYour weight on the Sun is " + 27.94*weight);
                    break;
                case 'a':
                case 'A':
                    System.out.println("Your weight on the Earth is " + weight + " lbs.\nYour weight on Saturn is " + .91*weight);
                    break;
                }
            case 'm':
            case 'M':
            switch(planet.charAt(1))
            {
                case 'o':
                case 'O':
                    System.out.println("Your weight on the Earth is " + weight + " lbs.\nYour weight on the Moon is " + .17*weight);
                    break;
                case 'e':
                case 'E':
                    System.out.println("Your weight on the Earth is " + weight + " lbs.\nYour weight on Mercury is " + .37*weight);
                    break;
                case 'a':
                case 'A':
                    System.out.println("Your weight on the Earth is " + weight + " lbs.\nYour weight on Mars is " + .38*weight);
                    break;
            } 
            case 'u':
            case 'U':
                System.out.println("Your weight on Earth is " + weight + " lbs.\nYour weight on Uranus is " + .88*weight);
                break;
            case 'v':
            case 'V':
                System.out.println("Your weight on Earth is " + weight + " lbs.\nYour weight on Venus is " + .90*weight);
                break;
            case 'j':
            case 'J':
                System.out.println("Your weight on Earth is " + weight + " lbs.\nYour weight on Jupiter is " + 2.64*weight);
                break;
            case 'n':
            case 'N':
                System.out.println("Your weight on Earth is " + weight + " lbs.\nYour weight on Neptune is " + 1.12*weight);
                break;
        } 
    } 
    
} //end class

Working on an assignment for my CS101 class. Our professor wants us to print our weight on a specific planet using nested switch statements with charAt to determine which planet the user is talking about. The nested switch statement is not doing as expected.

Errors:
When I pick Sun it outputs the result of Sun AND Uranus

When I pick Moon it outputs the result of Moon AND Uranus

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

When I pick Saturn it outputs the result of Saturn, Mars, AND Uranus.

When I pick Mercury it outputs the result of Mercury AND Uranus

When I pick Mars it outputs the result of Mars AND Mercury.

I don’t understand why it is printing the result of multiple planets, even if the letter at the index does not match. Uranus, Venus, Jupiter, and Neptune works fine and prints as expected.

>Solution :

You need additional break statements after the nested switch so that you break out of the outer switch as well:

    switch(planet.charAt(0))
    {
        case 's':
        case 'S':
            switch(planet.charAt(1))
            {
            case 'u':
            case 'U':
                ...
                break;
            }
            break; // this one is needed.
        case 'm':
            ...

Alternatively you can label both switch statements and then do break outer;.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading