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;
   }



}

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.

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

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.

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.

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.

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.

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);
    }   
}

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.

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...

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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.