Dave
Dave

Reputation: 75

Java - While loop for menu selection (console based program)

Update The first time the user makes a choice such as "1" the menu is displayed again. The next time a selection is made the payment information begins to cycle. After the cycling is complete and the menu is displayed again, it works as it should. Also, the first two years are output instead of just the first when a selection begins to cycle, but then outputs one year at a time as intended.

//create scanner object for choosing a loan, then prompt for and accept input
    Scanner choose = new Scanner(System.in);
    String choice;
    System.out.println("\nType 1, 2, or 3 and press enter to see the monthly payment information for the respective loan. To end the program type \"end\".");
    choice = choose.next();

    //cycle loan 1 payment information
    //create scanner object to advance to the next year's payments

    //loop for cycling payment information
    //initialize loan principal to variable

    while (!"end".equals(choice)) {
        System.out.println("\nType 1, 2, or 3 and press enter to see the monthly payment information for the respective loan. To end the program type \"end\".");
        choice = null;
        choice = choose.next();
        if ("1".equals(choice)) {
           //calculation code
                    }
                if (j < 6) {
                    System.out.println("Press enter to get the mortgage information for year " + (j + 2));
                    choice = choose.nextLine();
                } else {
                    System.out.println("Congratulations, your mortgage has been paid off.");
                }

            }

            choice = null;
        }
        if ("2".equals(choice)) {
            //calculation code
                }
                if (j < 14) {
                    System.out.println("Press enter to get the mortgage information for year " + (j + 2));
                    choice = choose.nextLine();
                } else {
                    System.out.println("Congratulations, your mortgage has been paid off.");
                }

            }

            choice = null;
        }
        if ("3".equals(choice)) {
            //calculation code
                }
                if (j < 29) {
                    System.out.println("Press enter to get the mortgage information for year " + (j + 2));
                    choice = next.nextLine();
                } else {
                    System.out.println("Congratulations, your mortgage has been paid off.");
                }

            }

            choice = null;
        }
    }
    choose.close();
}

}

Upvotes: 2

Views: 20799

Answers (4)

sandeep shukla
sandeep shukla

Reputation: 11

import java.util.Scanner;

class Main

{

public static void main(String [] args)

  {

    Scanner sc=new Scanner(System.in);

    System.out.println("Enter no.1:");

    int a=sc.nextInt();

    System.out.println("Enter no.2:");

    int b=sc.nextInt();

    boolean exit=false;

    do
    {
      System.out.println("1.addition");
      System.out.println("2.subtraction");
      System.out.println("3.multiplication");
      System.out.println("4.division");
      System.out.println("5.exit");
      System.out.println("choose one!");
      Scanner sd=new Scanner(System.in);
      System.out.println("enter your choice");
      int num=sd.nextInt();
      switch(num)
     {
       case 1:
       int add=a+b;
       System.out.println("addition="+add);
       System.out.println("\n");
       break;

       case 2:
       int sub=a-b;
       System.out.println("subtraction="+sub);
       System.out.println("\n");
       break;

       case 3:
       int mul=a*b;
       System.out.println("multilpication="+mul);
       System.out.println("\n");
       break;

       case 4:
       int div=a/b;
       System.out.println("division="+div);
       System.out.println("\n");
       break;

       case 5:
       exit=true;
       break;
      }
    }while(!exit);
}

}

Upvotes: 1

Yogendra Singh
Yogendra Singh

Reputation: 34387

I can see three issues upfront:

  1. You don't need two scanners for System.in stream. Remove this statement Scanner next = new Scanner(System.in); and use the choose instance.

  2. If you close your input scanner as next.close();, it closes your input stream System.in as well and you may not be able read the stream again. Make sure you close the stream only when you are completely done with your program.

  3. Use equals method to compare the condition in while as while(!"end".equals(choice)). Put the literal "end" as first argument will take care of the null value of choice.

EDIT:

Your modified code at high level:

    Scanner choose = new Scanner(System.in);
    String choice= null;
    int j = 0;
    while (!"end".equals(choice)) {
        System.out.println("\nType 1, 2, or 3 and press enter to see the monthly payment information for the respective loan. To end the program type \"end\".");
        choice = choose.nextLine();
        if ("1".equals(choice)) {
           //calculation code
               if (j < 6) {
                    System.out.println("Press enter to get the mortgage information for year " + (j + 2));
                    choice = choose.nextLine();
                } else {
                    System.out.println("Congratulations, your mortgage has been paid off.");
                }
            choice = null;
        }
        if ("2".equals(choice)) {
                if (j < 14) {
                    System.out.println("Press enter to get the mortgage information for year " + (j + 2));
                    choice = choose.nextLine();
                } else {
                    System.out.println("Congratulations, your mortgage has been paid off.");
                }
            choice = null;
        }
        if ("3".equals(choice)) {
                if (j < 29) {
                    System.out.println("Press enter to get the mortgage information for year " + (j + 2));
                    choice = choose.nextLine();
                } else {
                    System.out.println("Congratulations, your mortgage has been paid off.");
                }
            choice = null;
        }
    }
    choose.close();

Upvotes: 5

user228534
user228534

Reputation:

You're setting choice to null, so choice != "end" is always true. Move the next.close(); choice = null to outside the while loop. Also, what weolfe91 said.

Upvotes: 1

awolfe91
awolfe91

Reputation: 1647

One bug is that String equality isn't the same as ==. You should use .equals():

while (!choice.equals("end")) {

Hope that helps!

Upvotes: 1

Related Questions