Results 1 to 7 of 7
Thread: Scanner to int?
- 02-11-2011, 11:28 PM #1
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
Scanner to int?
I'll just put the code out here as its simpler to understand that me explaining it
Java Code:import java.util.Scanner; public class apples4 { public static void main(String args[]){ Scanner bucky = new Scanner(System.in); System.out.println("Enter the loop number"); int counter =0; while(counter < bucky) { System.out.println(counter); counter++; } } }
while(counter < bucky)
and the error is:
The operator < is undefined for the argument type(s) int, Scanner
So I got to change the Scanner to in int? or?
Thanks!
-
You can't compare a Scanner object to an int, and you don't want to try either. Your Scanner object is used to get input from the user and should only be used for that. Then once you've used one of the Scanner's methods to get input from the user, you then test the input received in your while condition.
- 02-12-2011, 12:02 AM #3
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
Then once you've used one of the Scanner's methods to get input from the user, you then test the input received in your while condition.
(Which also translates to, how do I get my original program working?)
- 02-12-2011, 12:15 AM #4
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
I think I got it!
I tried:
int ii = bucky.nextInt();
...
while(counter < ii)
and it worked!
But now I am not very sure why ;(
-
The key is learning how to use a Scanner object to get user input. Google can help you find a tutorial on this, such as:
Scanner Tutorial 1
Scanner Tutorial 2
- 02-12-2011, 12:31 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 18
it worked!
But now I am not very sure why ;(
Don't worry so much about the fact that it worked. Worry, instead, about why the code you wrote did what it did. (In particular why the output line outputted what it did.)
If you can't understand the precise meaning of each line of the code, post it all. (If some line in particular is worrying, indicate that.)
- 02-12-2011, 01:14 AM #7
Senior Member
- Join Date
- Feb 2011
- Posts
- 107
- Rep Power
- 0
Similar Threads
-
Using scanner for CSV
By getName() in forum Advanced JavaReplies: 7Last Post: 06-20-2010, 05:33 PM -
Help With Scanner
By jtmoney0511 in forum New To JavaReplies: 10Last Post: 10-13-2009, 12:24 AM -
Need help with scanner.
By mainy in forum New To JavaReplies: 3Last Post: 07-28-2009, 03:11 PM -
need help with scanner
By whiterex in forum New To JavaReplies: 1Last Post: 04-22-2008, 02:41 PM -
help with IP scanner
By tommy in forum New To JavaReplies: 1Last Post: 08-06-2007, 09:00 PM
Bookmarks