0

I am having trouble getting my menu below to run from my driver. The program will execute, however it will not display my menu until I enter a number. After that it will display properly, and it reads selection properly, but will not call the methods I have listed in the case statement.

For instance, if I input a '1', the menu will recognize I input a 1 and will display the menu again with "You entered 1". instead of calling dec.getDec(), as it should according to the case statement. Any helpful hints or advice would be appreciated. This is a homework assignment, and I am not trying to get someone to write the code for me or anything. I just need to be pointed in the right direction please.

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);
    }   
}
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
user1660948
  • 59
  • 1
  • 5

2 Answers2

1

Don't use case 'n':, just use case n. You don't need the single quote. Also have a look at this tutorial on Switch Statements in Java to get an idea of how to use this in your code.

The problem with your current implementation is because you're trying to compare an int value (that you've in your selection variable) with char (which is internally getting converted to its corresponding int value i.e. int value of '1' is not the same as 1).

You can see the difference with the following code:

switch(selection){

case '1':
    System.out.println("Hello World from char");
    break;

case 1:
    System.out.println("Hello World from int");
    break;
}

So when you set selection = 1, you would find the output from the int block however if you set selection = '1', you would find the output from the char block

Note that I'm assuming that you're not running in Java 7


Note: There's another issue with your code. @Shaded has given you the perfect hint. Think about it with reference to how your control flows through your logic for setting the value of selection variable.
Sujay
  • 6,753
  • 2
  • 30
  • 49
  • The single quotes aren't the real problem, he's getting `int` from menu.GetSelection() so it's just comparing `int` and `char`. This doesn't address why his menu isn't displaying on the initial load. – Shaded Sep 10 '12 at 19:05
  • Thanks! I must have stared at that for 3 hours now and not even thought of the '' being an issue. Phew! – user1660948 Sep 10 '12 at 19:15
  • Shaded - Also true. It reads my input now thanks to the removal of the quotes. but it still does not display my menu initially – user1660948 Sep 10 '12 at 19:17
  • For the other issue, @Shaded is correct and I think he has given a proper hint to help you get started! +1 for that! – Sujay Sep 10 '12 at 19:18
  • @user1660948: Updated my answer. Hope you can figure it out with the hints :) – Sujay Sep 10 '12 at 19:26
1

As this is homework, I won't give you the whole solution, but I'll help get you there...

Your problem is coming from your use of Scanner, the helpful part of this page is A scanning operation may block waiting for input.

Using that you should be able to see where the problem is, if you need more help comment on this answer and I'll see if there's more I can do.

Shaded
  • 17,276
  • 8
  • 37
  • 62
  • is there a way to ignore the initial whitespace? i assume this is the issue, it is holding a value of nothing until it sees i input a #...@Shaded – user1660948 Sep 10 '12 at 19:31
  • @user1660948 I don't think so, you might want to check out [this questions](http://stackoverflow.com/questions/4644415/how-to-get-input-from-console-class-in-java) answers to get a better idea of reading the console input. – Shaded Sep 10 '12 at 19:35
  • forgive my continuing ignorance. but basically my issue is that i am using Scanner instead of BufferedReader ? if i switch to BufferedReader i will avoid the initial pause before the menu? – user1660948 Sep 10 '12 at 19:58
  • @user1660948 I believe so, the Scanner is used more for reading input that you already have, not reading input as it comes in. With the buffer it shouldn't have any problems handling this. – Shaded Sep 10 '12 at 20:30
  • ok Thanks for the input @Shaded. After i swap the code for BufferedReader i will update with my success/failures. – user1660948 Sep 10 '12 at 21:12
  • Thanks all for the insight. I managed to get everything working using BufferedReader instead of Scanner, as well as moving around a few lines of code! You guys saved me! – user1660948 Sep 10 '12 at 21:45
  • @user1660948 not a problem! If you need any other help feel free to post another question, SO is full of helpful people. – Shaded Sep 11 '12 at 15:10