#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    13
    Rep Power
    0

    Java do while loop displaying dialogs when ends only


    I need to display an input dialog that will b present until I click cancel.
    Each option chosen (by user's input) calls a method which prints something.
    The problem is it prints it only after I click on cancel, if I choose different methods it runs them the same order they show in the while regardless of the order they were called.
    If I repeat the same method it runs it only once for some reason...
    Why it happens?
    shouldn't it leave the main when a method is called and then when finished return?
    I tried returning a string or using the scanner via the method but still same result.

    Does it have anything to do with the fact everything is static?
    Nothing to be done about that , everything being static is an instruction
    This is my code (still in skeleton stage :S), main:
    Code:
    String s=new String();
     s+="Please enter yout choice of exe\n";
     s+="1 - Perfect numbers\n";
     s+="2 - Base conversion\n"; 
    s+="3 - Closest pairs\n"; 
    s+="Press Cancel to exit"; 
    String choice="";
     do{ choice=JOptionPane.showInputDialog(s);
     if(choice!=null) { if(choice=="1") Q1(); 
    else if (choice=="2") Q2(); 
    } 
    } while(choice!=null); 
    
    Q1(); 
    Q2();
    Q3();
    These are the methods:

    Code:
    public static void Q1() 
    { final int min=2;
    final int max=10;
    JOptionPane.showMessageDialog(null,"Q1"); }
    
     public static void Q2() {JOptionPane.showMessageDialog(null,"Q2");}
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    13
    Rep Power
    0
    Originally Posted by Lenjaku
    I need to display an input dialog that will b present until I click cancel.
    Each option chosen (by user's input) calls a method which prints something.
    The problem is it prints it only after I click on cancel, if I choose different methods it runs them the same order they show in the while regardless of the order they were called.
    If I repeat the same method it runs it only once for some reason...
    Why it happens?
    shouldn't it leave the main when a method is called and then when finished return?
    I tried returning a string or using the scanner via the method but still same result.

    Does it have anything to do with the fact everything is static?
    Nothing to be done about that , everything being static is an instruction
    This is my code (still in skeleton stage :S), main:
    Code:
    String s=new String();
     s+="Please enter yout choice of exe\n";
     s+="1 - Perfect numbers\n";
     s+="2 - Base conversion\n"; 
    s+="3 - Closest pairs\n"; 
    s+="Press Cancel to exit"; 
    String choice="";
     do{ choice=JOptionPane.showInputDialog(s);
     if(choice!=null) { if(choice=="1") Q1(); 
    else if (choice=="2") Q2(); 
    } 
    } while(choice!=null); 
    
    Q1(); 
    Q2();
    Q3();
    These are the methods:

    Code:
    public static void Q1() 
    { final int min=2;
    final int max=10;
    JOptionPane.showMessageDialog(null,"Q1"); }
    
     public static void Q2() {JOptionPane.showMessageDialog(null,"Q2");}


    forgot to delete the last 3 lines calling the methods after the loop ends -.- they were meant for testing...
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Location
    London, ON
    Posts
    322
    Rep Power
    214
    You're trying to compare strings, which are objects. == is the equality operator for primitives.

    Also,

    String choice = "";
    String choice = null;

    These have two different meanings. In the first one, the object choice contains a String that is empty (length 0). In the second one the object choice is null.

    Someone who better understands the memory allocation should comment explaining the difference more clearly (Please!).

    Basically what it all means is that you can't use "null" for what you're trying to do there. You want something along the lines of:

    if(choice.equals(""))

    Comments on this post

    • stdunbar agrees
    Last edited by Wetmelon; November 29th, 2012 at 08:04 PM.
    <Tetrad> the program I just wrote 1) compiled the first time without any errors and 2) worked like it was supposed to
    <Tetrad> I don't know whether to be proud or scared to death
    Originally Posted by DaWei_M
    That covers a multitude of your sins, right there.

IMN logo majestic logo threadwatch logo seochat tools logo