Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jul 2012
    Location
    NYC
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    "variable input might not have been initialized" ? Saying that after if statement?

    Code:
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    public class Main {
    static Scanner sc = new Scanner (System.in); 
    public static void main (String [] args) { 
    
    
    int number = 2;
    String input;
    
    while (true)
    {
      JOptionPane.showMessageDialog(null, number + "");
    JOptionPane.showInputDialog(null,"Do you want to keep counting " + " Y or N");
    
    
    if (input.equalsIgnoreCase("N"))
        break;
    number +=2;
            JOptionPane.showMessageDialog(null, "\nWhew! That was close.\n");
            
    }
    
    }
    
    }
    Last edited by dannyboi; 08-16-2012 at 01:27 AM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    if (input.equalsIgnoreCase("N")). Input cannot match this, ever. It is null and never assigned any different value, and the compiler knows this. You can fix it by initializing its value to an empty string.

    JOptionPane.show* messages will return a result for you to work with. Strings for input, and integers for confirmation and option dialogs.

  • #3
    New Coder
    Join Date
    Jul 2012
    Location
    NYC
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts
    ok, I still am running into the variable input problem

  • #4
    New Coder
    Join Date
    Jul 2012
    Location
    NYC
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts
    JOptionPane.show* messages will return a result for you to work with. Strings for input, and integers for confirmation and option dialogs.
    ANd what does this mean?

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by dannyboi View Post
    ANd what does this mean?
    The signature for input dialog off of JOptionPane is:
    Code:
    public static String showInputDialog(Component, Object);
    public static String showInputDialog(Component, Object, Object);
    public static String showInputDialog(Component, Object, String, int);
    Every one of these returns a string.

    The final one:
    Code:
    public static Object showInputDialog(Component, Object, String, int, Icon, Object[], Object);
    Returns an object. It's used for selections though, not direct input.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •