Results 1 to 11 of 11
- 07-08-2010, 04:47 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 6
- Rep Power
- 0
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);
}
}
- 07-08-2010, 04:58 PM #2
What's it supposed to do?
Please explain what the problem is or why the output of the program is wrong
- 07-08-2010, 05:28 PM #3
Member
- Join Date
- Jan 2009
- Posts
- 6
- Rep Power
- 0
It creates an arraylist and then it prompts user for numbers to populate the list.
And then it prints out the list.
- 07-08-2010, 05:31 PM #4
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
- 07-08-2010, 05:54 PM #5
Member
- Join Date
- Jan 2009
- Posts
- 6
- Rep Power
- 0
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.
- 07-08-2010, 06:06 PM #6
Ok, now we know the problem
Can you copy the console from when you run the code and paste it here?
- 07-08-2010, 06:08 PM #7
Member
- Join Date
- Jan 2009
- Posts
- 6
- Rep Power
- 0
it doesn't run, it just freezes
- 07-08-2010, 06:16 PM #8
Its running and waiting for you.
Enter a number.
- 07-08-2010, 07:02 PM #9
Member
- Join Date
- Jan 2009
- Posts
- 6
- Rep Power
- 0
you are right. for some reason it doesn't print "Enter a number: " first time.
Thanks.
- 07-08-2010, 07:33 PM #10
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 07:39 PM.
- 07-09-2010, 05:53 AM #11
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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
-
Code does not work
By scotts in forum New To JavaReplies: 12Last Post: 06-19-2010, 09:37 AM -
Why this code don't work?
By artemff in forum CLDC and MIDPReplies: 6Last Post: 04-16-2010, 02:57 AM -
How does the '*' work in the code?
By DrMath in forum New To JavaReplies: 3Last Post: 10-28-2009, 09:26 AM -
how does this code work...explain me the execution please...
By vital parsley in forum New To JavaReplies: 3Last Post: 07-25-2008, 04:50 AM -
the code dosent work
By pcman in forum Java 2DReplies: 1Last Post: 03-20-2008, 08:20 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks