Results 1 to 7 of 7

Thread: Scanner to int?

  1. #1
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default 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++;
    		}
    	}
    }
    This is where I am getting the error:
    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!

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

    Default

    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.

  3. #3
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    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.
    Sorry, total noob... how?

    (Which also translates to, how do I get my original program working?)

  4. #4
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    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 ;(

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

    Default

    Quote Originally Posted by N00Bie View Post
    Sorry, total noob... how?

    (Which also translates to, how do I get my original program working?)
    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

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    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.)

  7. #7
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

Similar Threads

  1. Using scanner for CSV
    By getName() in forum Advanced Java
    Replies: 7
    Last Post: 06-20-2010, 05:33 PM
  2. Help With Scanner
    By jtmoney0511 in forum New To Java
    Replies: 10
    Last Post: 10-13-2009, 12:24 AM
  3. Need help with scanner.
    By mainy in forum New To Java
    Replies: 3
    Last Post: 07-28-2009, 03:11 PM
  4. need help with scanner
    By whiterex in forum New To Java
    Replies: 1
    Last Post: 04-22-2008, 02:41 PM
  5. help with IP scanner
    By tommy in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 09:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •