Results 1 to 4 of 4
  1. #1
    kev670 is offline Member
    Join Date
    Oct 2010
    Posts
    20
    Rep Power
    0

    Default Scanner Problems

    I'm having trouble using the scanner class to simple read in values. I've simplified my program to basically just read in values and for some reason it still wont read in the title2 variable or actor variable... it just skips them. No doubt I'm making some schoolboy error in my code but could someone take a look and see if they can spot something, thanks.


    Java Code:
    import java.util.Scanner;
    public class scanStuff
    {
        public static void main(String[] args)
        {
            String title, title2, artist, director, actor;
            int playTime, numOfTracks;
            Scanner scan = new Scanner(System.in);
            
    
    
                System.out.print("Title: ");
                title = scan.nextLine();
            
                System.out.print("Artist: ");
                artist = scan.nextLine();
            
                System.out.print("Length: ");
                playTime = scan.nextInt();
        
                System.out.print("Number of tracks: ");
                numOfTracks = scan.nextInt();
                 
                
                
                System.out.print("Title: ");
                title2 = scan.nextLine(); 
            
                System.out.print("Director: ");
                director = scan.nextLine();
            
                System.out.print("Length: ");
                playTime = scan.nextInt();
        
                System.out.print("Actor: ");
                actor = scan.nextLine();
                 
     
        }
    
    }

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

    Default

    Your scan.nextInt() doesn't swallow the end of line token and you'll have to do this yourself. So every where you call scan.nextInt() call scan.nextLine() and discard the result. e.g., change this:

    Java Code:
                System.out.print("Number of tracks: ");
                numOfTracks = scan.nextInt();


    to this:
    Java Code:
                System.out.print("Number of tracks: ");
                numOfTracks = scan.nextInt();
                scan.nextLine(); // call this but don't extract the returned value.

  3. #3
    kev670 is offline Member
    Join Date
    Oct 2010
    Posts
    20
    Rep Power
    0

    Default

    Thanks a lot for the help

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

Similar Threads

  1. Scanner to int?
    By N00Bie in forum New To Java
    Replies: 6
    Last Post: 02-12-2011, 01:14 AM
  2. InputStream/Jar Problems/File IO Problems
    By rdjava in forum Advanced Java
    Replies: 31
    Last Post: 01-17-2011, 12:12 PM
  3. Scanner.next() problems!
    By ngc0202 in forum New To Java
    Replies: 3
    Last Post: 08-11-2010, 02:50 AM
  4. Need help with scanner.
    By mainy in forum New To Java
    Replies: 3
    Last Post: 07-28-2009, 03:11 PM
  5. Scanner class problems
    By sypherz in forum New To Java
    Replies: 6
    Last Post: 06-09-2008, 10:46 AM

Posting Permissions

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