Results 1 to 4 of 4
- 03-07-2011, 04:51 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 22
- Rep Power
- 0
End Scanner int input with text value
I have a Scanner object and the values being input are integers. What I would like to do is have the user enter an 's' to stop the input. I have the following code (which of course throws an error). How can I accomplish my goal? Thank you.
int stop = 's';
int number;
Scanner input = new Scanner(System.in);
System.out.println("Enter the first number of the array: ");
number = input.nextInt();
while (input != stop)
{
//process this code
}
- 03-07-2011, 04:57 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
if they enter the number which is used for s it will stop. Try printing out stop
Then use that number as the input, and see what happens.Java Code:System.out.println(stop);
You could also use readLine and try to parse it to an integer with
do this after testing if it's s. You can also catch any exceptions for when integer.parseint doesnt work.Java Code:Integer.parseInt(String)
- 03-07-2011, 04:58 PM #3
You're trying to input a String, but you're using the nextInt() function. Why? If you want to read in Strings, you have to use one of the methods that reads in Strings.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 03-07-2011, 05:13 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,467
- Rep Power
- 16
Similar Threads
-
how to reverse some input from scanner
By moncur in forum New To JavaReplies: 24Last Post: 11-09-2010, 02:59 PM -
how to input unspecified number if ints with a scanner
By ftrengnr in forum New To JavaReplies: 2Last Post: 09-14-2010, 01:24 AM -
Changing Scanner input type
By mlad in forum New To JavaReplies: 13Last Post: 03-18-2010, 04:22 PM -
problem with Scanner in Getting users input
By kliane in forum New To JavaReplies: 8Last Post: 01-17-2010, 04:37 PM -
Scanner input problem
By slayer_azure in forum New To JavaReplies: 3Last Post: 05-26-2008, 10:49 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks