Results 1 to 11 of 11
  1. #1
    seanzhang is offline Member
    Join Date
    Jan 2009
    Posts
    6
    Rep Power
    0

    Default This code doesn't work. Why? thanks.

    import java.util.ArrayList;

    import java.util.Scanner;

    public class SimpleEventDemo {
    /** Main method */
    public static void main(String[] args) {

    ArrayList<Integer> numbers = new ArrayList<Integer>();

    // Create a Scanner
    Scanner input = new Scanner(System.in);

    // Read all numbers
    while(input.hasNextInt()){
    System.out.print("Enter a number: ");

    // Convert string into integer
    numbers.add(input.nextInt());
    }
    System.out.print(numbers);
    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    What's it supposed to do?
    Please explain what the problem is or why the output of the program is wrong

  3. #3
    seanzhang is offline Member
    Join Date
    Jan 2009
    Posts
    6
    Rep Power
    0

    Default

    It creates an arraylist and then it prompts user for numbers to populate the list.
    And then it prints out the list.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    Ok, you've answered the first question. Now how about this one:

    Please explain what the problem is or why the output of the program is wrong

  5. #5
    seanzhang is offline Member
    Join Date
    Jan 2009
    Posts
    6
    Rep Power
    0

    Default

    The while loop doesn't work. If I remove the while loop. It takes one number and it adds the number to the list and prints it out. But with the while loop the program just hangs.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    Ok, now we know the problem

    Can you copy the console from when you run the code and paste it here?

  7. #7
    seanzhang is offline Member
    Join Date
    Jan 2009
    Posts
    6
    Rep Power
    0

    Default

    it doesn't run, it just freezes

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    Its running and waiting for you.
    Enter a number.

  9. #9
    seanzhang is offline Member
    Join Date
    Jan 2009
    Posts
    6
    Rep Power
    0

    Default

    you are right. for some reason it doesn't print "Enter a number: " first time.
    Thanks.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    That should tell you something about what the hasNextInt() method is doing.

    Read the API doc for an explanation:
    Both hasNext and next methods may block waiting for further input.
    Last edited by Norm; 07-08-2010 at 08:39 PM.

  11. #11
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Converting the while loop to a do-while loop could work. Check it for bugs if the first input isn't actually an integer though... (i.e. I haven't tested this at all)
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. Code does not work
    By scotts in forum New To Java
    Replies: 12
    Last Post: 06-19-2010, 10:37 AM
  2. Why this code don't work?
    By artemff in forum CLDC and MIDP
    Replies: 6
    Last Post: 04-16-2010, 03:57 AM
  3. How does the '*' work in the code?
    By DrMath in forum New To Java
    Replies: 3
    Last Post: 10-28-2009, 10:26 AM
  4. how does this code work...explain me the execution please...
    By vital parsley in forum New To Java
    Replies: 3
    Last Post: 07-25-2008, 05:50 AM
  5. the code dosent work
    By pcman in forum Java 2D
    Replies: 1
    Last Post: 03-20-2008, 09:20 PM

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
  •