We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,820 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Help with Java Menu Program (infinite loop?)

Hello All, this is my first post, so bear with me... My problem is with the following code. I have a simple menu setup to do some number conversions, and everything compiles properly. When i run the code, i end up with the Java VM (in bluej) just running endlessly, and according to the debug, this is happening before any code is processed. the classes Decimal, Binary, Hexadecimal and Menu run properly on their own, however when i try and run them from the driver, I encounter this problem. Please help!

import java.io.*;
import java.lang.*;
import java.util.Scanner;


public class Driver
{

    public static void main(String[] args)throws IOException {

            LineWriter lw = new LineWriter("csis.txt");
            int selection;

            Decimal dec = new Decimal();
            Binary bin = new Binary();
            Hexadecimal hex = new Hexadecimal();
            Menu menu = new Menu();

        do{ 

             menu.display();
             selection=menu.GetSelection();


             switch (selection){

            case '1': dec.getDec();
                      {break;}
            case '2': dec.getHex();
                      {break;}
            case '3': bin.getBin();
                      {break;}
            case '4': bin.getHex();
                      {break;}
            case '5': hex.getHex();
                      {break;}
            case '6': hex.getDec();
                      {break;}       
            case '0': System.out.println("Goodbye");
                      {break;}

           }
        }while (selection !=0);


    }   
}




import java.io.IOException;
import java.io.*;
import java.util.Scanner;

public class Menu {
    Scanner scan = new Scanner(System.in); 
    int selection = scan.nextInt();

    public void display()
    {



          System.out.println("Please choose an option from the following:"); 
          System.out.println("[1] Convert Decimal to Binary"); 
          System.out.println("[2] Convert Decimal to Hexadecimal"); 
          System.out.println("[3] Convert Binary to Decimal"); 
          System.out.println("[4] Convert Binary to Hexadecimal"); 
          System.out.println("[5] Convert Hexadecimal to Decimal"); 
          System.out.println("[6] Convert Hexadecimal to Binary"); 
          System.out.println("[0] Exit");

          System.out.println("\n");
           System.out.println("You entered: " + selection);
   }

   public int GetSelection()
   {
       return selection;
   }



}
2
Contributors
12
Replies
3 Hours
Discussion Span
8 Months Ago
Last Updated
13
Views
Question
Answered
mobility42
Newbie Poster
10 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Where is the infinte loop happening? In what method in what class?
Add some println statements that shows the execution flow so you can see where the code is executing and where the loop is.

For solid code and better bug detection you should add a default case to the switch and have it print out the value of selection to see if there is a problem there.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

*correction The Menu class does not run on its own. It pauses at "creating object"

mobility42
Newbie Poster
10 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

the loop is happening in the menu class from what i can tell, because when i attempt to run the menu on its own, the VM just runs endlessly on "Creating Object".

i've added the default to the switch, and the result is the same.

mobility42
Newbie Poster
10 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

What prints out from the println statements that you added? The printed output should show you where the code is executing.

Is the program looping or is it waiting for input from the user? Type in a number.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

After execution, the terminal is blank, presumably waiting for me to input my selection, however there are no options displayed to choose from. If I enter 1, for example, despite not being able to see the options, the menu displays, but loops and has to be closed.

mobility42
Newbie Poster
10 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

So now you know what the program is doing: its waiting for you to enter a number. Look at the code in the Menu class and see where there is code trying to read from the user. Hint: the Scanner class has methods for reading from the user.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

i managed to get my menu to display properly, however it still will only display after I input a #. I think my problem lies somewhere in the GetSelection() code, when my menu reads the selection, it reads it properly, but my switch(selection) is not functioning properly, and directing my selection to the proper method.

import java.io.IOException;
import java.io.*;
import java.util.Scanner;

public class Menu {
    Scanner scan = new Scanner(System.in); 
    int selection;

    public int GetSelection()
   {
       selection = scan.nextInt();
       return selection;
   }

    public void display()
    {

          System.out.println("Please choose an option from the following:"); 
          System.out.println("[1] Convert Decimal to Binary"); 
          System.out.println("[2] Convert Decimal to Hexadecimal"); 
          System.out.println("[3] Convert Binary to Decimal"); 
          System.out.println("[4] Convert Binary to Hexadecimal"); 
          System.out.println("[5] Convert Hexadecimal to Decimal"); 
          System.out.println("[6] Convert Hexadecimal to Binary"); 
          System.out.println("[0] Exit");

          System.out.println("\n");
          System.out.println("You entered: " + selection);
   }

}


import java.io.*;
import java.lang.*;
import java.util.Scanner;


public class Driver
{

    public static void main(String[] args)throws IOException {

            LineWriter lw = new LineWriter("csis.txt");
            int selection;

            Decimal dec = new Decimal();
            Binary bin = new Binary();
            Hexadecimal hex = new Hexadecimal();
            Menu menu = new Menu();


        do{ 
            menu.display();

            selection=menu.GetSelection();

            switch (selection){

            case '1':{ dec.getDec();
                      break;}
            case '2':{ dec.getHex();
                      break;}
            case '3':{ bin.getBin();
                      break;}
            case '4':{ bin.getHex();
                      break;}
            case '5':{ hex.getHex();
                      break;}
            case '6': { hex.getDec();
                      break;  }     
            //default: System.out.println("Error: Unrecognized Selection");
            //          break;

           }
        }while (selection !=0);
    }   
}
mobility42
Newbie Poster
10 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

my switch(selection) is not functioning properly

What did the println in the default: case print out for the value of the variable: selection?
You should not comment out the default case. It should not execute unless there is a problem which you want to know about.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

the println in the default prints regardless of input (so if i input 1, it says "Unrecognized Selection"), which is why i had it commented out. i assume that this is because switch(selection) cannot read my GetSelection properly. I am a novice in java, and am unclear how to relate the GetSelection and selection in my case statement properly...

mobility42
Newbie Poster
10 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

You need to print out the value of selection so you know what the computer saw when it executed the switch statement.

System.out.println("Error: Unrecognized selection="+selection);  // show value with message
NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
Question Answered as of 8 Months Ago by NormR1

The main portion of my problem was solved by simply removing the '' from my case. so instead of Case '1' i used Case 1 .

mobility42
Newbie Poster
10 posts since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Glad you found it. The char '1' is not the same as an int with the value of 1.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1143 seconds using 2.79MB