Results 1 to 6 of 6
  1. #1
    atiyarose17 is offline Member
    Join Date
    Oct 2012
    Posts
    12
    Rep Power
    0

    Default While loop makes program crash

    I'm having a problem with a while loop like this:

    Java Code:
    //Get user input of the day. 
        dayInput = JOptionPane.showInputDialog(null, "Enter the Day of the Month:");
        scan = new Scanner(dayInput);
        
        //Check if the input is correctly in an integer format. If so, print the value.
        while(!scan.hasNextInt() || scan.nextInt() <1 || scan.nextInt() > 31) {
          dayInput = JOptionPane.showInputDialog(null, "Enter the Day of the Month:");
          scan = new Scanner(dayInput);
        } 
        System.out.println("Enter the Day of the Month:\n" + dayInput);

    I have a feeling it is due to the scan.nextInt() but I have NO clue as to how I need to go about fixing this problem. It compiles just fine, but when run, it crashes and gives an error message (see below) regardless of what value is entered in the input dialog! Help!!!

    java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at Assignment1q3.main(Assignment1q3.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:272)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: While loop makes program crash

    What exactly are you trying to do? What input are you expecting from the user?

  3. #3
    atiyarose17 is offline Member
    Join Date
    Oct 2012
    Posts
    12
    Rep Power
    0

    Default Re: While loop makes program crash

    Quote Originally Posted by Fubarable View Post
    What exactly are you trying to do? What input are you expecting from the user?
    The user will input a number for the day of the month (1 - 31) and I just need the while loop to check the input to make sure it is an integer between 1 and 31 (so that further calculations using the day of the month are valid.)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: While loop makes program crash

    Your use of the Scanner object is a bit off. I would recommend that you not use it at all, and instead simply parse the String obtained from the JOptionPane to an int using Integer.parseInt(...). Store it in an int variable and test that int variable for correctness.

  5. #5
    atiyarose17 is offline Member
    Join Date
    Oct 2012
    Posts
    12
    Rep Power
    0

    Default Re: While loop makes program crash

    Quote Originally Posted by Fubarable View Post
    Your use of the Scanner object is a bit off. I would recommend that you not use it at all, and instead simply parse the String obtained from the JOptionPane to an int using Integer.parseInt(...). Store it in an int variable and test that int variable for correctness.
    If the input contains a decimal place or any characters that are not numbers, it will not parse to an int will it? That was the main purpose of the scan.hasNextInt() so it will make sure that the input is indeed an integer, and only proceed if that is the case.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: While loop makes program crash

    Quote Originally Posted by atiyarose17 View Post
    If the input contains a decimal place or any characters that are not numbers, it will not parse to an int will it? That was the main purpose of the scan.hasNextInt() so it will make sure that the input is indeed an integer, and only proceed if that is the case.
    But you are misusing hasNextInt(). If you were to use this correctly, you'd use a loop that repeatedly requests input until hasNextInt() is true.

    For parsing as I described above, you'd use a try / catch block catching a NumberFormatException.

Similar Threads

  1. JVM crash.
    By werner291 in forum New To Java
    Replies: 4
    Last Post: 12-31-2011, 06:31 PM
  2. My Simple JAR makes Windows 7 crash?
    By PrinceSendai in forum New To Java
    Replies: 2
    Last Post: 01-17-2011, 08:54 PM
  3. JVM crash
    By darsh in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 10-15-2010, 10:55 AM
  4. AWT JVM Crash
    By Java_Zak in forum AWT / Swing
    Replies: 0
    Last Post: 07-29-2010, 11:13 AM
  5. Help Making a program not crash
    By Lifeis2evil in forum New To Java
    Replies: 2
    Last Post: 12-10-2008, 04:10 AM

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
  •