0
public static void main(String[] args){     
    String buf;

    buf = JOptionPane.showInputDialog("1, 2 or 3");

    if (buf == 1) {
        Begin2();}//if a
    if (buf == 2) {
        Main.Begin6();}//if b
    if (buf == 3) {
        Main.Begin7();}//if b
}

I have this code, but if cant invoke any of Begin methods...

DaemonGod
  • 19
  • 3

5 Answers5

2

To convert to an integer, use Integer.valueOf(String):

if (Integer.valueOf(buf) == 1) {

A better option is to use the method from JOptionPane that asks for input with given selections. This way you would force the user to input 1, 2 or 3, and at the same there would be no need to convert to an integer.

Integer result = (Integer) JOptionPane.showInputDialog(null, "1, 2 or 3", "title", JOptionPane.QUESTION_MESSAGE, null, new Integer[]{1,2,3}, 1);

if (result == 1) {
  ... 
} else if (result == 2) {
  ...
} else if (result == 3) {
  ...
}
M A
  • 71,713
  • 13
  • 134
  • 174
1
  1. Convert it to an Integer using Integer.parseInt(buf) (best way)
  2. Use "3".equals(buf) (ugly).
  3. Keep as string and use switch (if you are in Java 7 >=) or if using equals method

Some says that you can just keep it as String and use a equals or switch. Yes it works, the fact to convert it to int maybe here is a bit excessive and it will avoids you a possible exception in case of wrong input

Marco Acierno
  • 14,682
  • 8
  • 43
  • 53
  • OP could also use a `switch` statement, which would be the same for int and string, so no `.equals` needed, and no `parseInt` needed. – BackSlash Dec 27 '14 at 13:36
  • Yes, but only with Java 7 >=. Most of time i prefer to convert the values in their correct type – Marco Acierno Dec 27 '14 at 13:36
  • If the user enters a string option 1 would give some ugly errors. I prefer option 2 or `buf.equals("3")` – Charlie Dec 27 '14 at 13:37
  • 1
    It is already a String, just use `.equals()`. In reality, I doubt the numbers 1, 2, and 3 mean anything in the context of this program. They should probably be replaced with constants that say that the options do. (e.g. `String DISPLAY_MENU = 1;`,`String ADD_ITEM = 2`, etc.. – Hunter McMillen Dec 27 '14 at 13:37
  • Because I don't like when String (or any other type) is used when other type could be used. If it's an Integer it should be an int in the code too. But it depends of course – Marco Acierno Dec 27 '14 at 13:38
  • @Charlie `"3".equals(buf)` avoids `NullPointerException` if `buf` is `null`, that's why you see `"3".equals(buf)` and not `buf.equals("3")` – BackSlash Dec 27 '14 at 13:38
1
int value = Integer.parseInt(buff);
if(value == 1){...}

else if(value == 2){...}

else if(value == 3){...}
mosemos
  • 71
  • 3
0

Different ways for doing this task:

Integer.parseInt("3") // This will return an integer value

or you can use the .equals() of class String

Abhishek
  • 878
  • 12
  • 28
0

Either

if("1".equals(buf)) {
    Begin2();
} else if ("2".equals(buf)) {
    Main.Begin6();
} else if ("3".equals(buf)) {
    Main.Begin7();
}

or

switch(buf) {
    case "1": Begin2(); break;
    case "2": Main.Begin6(); break;
    case "3": Main.Begin7(); break;
    default: break;
}

would do the job.

Charlie
  • 978
  • 1
  • 7
  • 27