Results 1 to 5 of 5
  1. #1
    jktexas1 is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Default String and Integer input bug

    Hey guys, I'm quickly trying to convert my c++ knowledge to Java and I've come across a weird bug that just wouldn't happen in C.

    In my current project whenever I try to input a String value right after inputting an Int value, the system just skips over letting the user enter the String.
    Heres the code I have just to reference:

    Scanner keyboard1 = new Scanner(System.in);

    System.out.println("Enter Int: ");
    int x = keyboard1.nextInt();

    System.out.println("Enter String: ");
    String temp = keyboard1.nextLine();

    System.out.println("Done.");

    Once I've entered the value for temp, I just get output "Enter String: " and output "done" without allowance to input the String value.
    Does anyone know why?

    Thanks,
    jktexas1

  2. #2
    Zyrion is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: String and Integer input bug

    Try adding the Keyboard1.nextLine(); method so the scanner can move down a line effectively.

    Java Code:
    import java.util.Scanner;
    
    public class Test{
        public static void main( String [] args){
            
    Scanner keyboard1 = new Scanner(System.in);
    
    System.out.println("Enter int: ");
    int x = keyboard1.nextInt();
    
    System.out.println("Enter String: ");
    
    String temp = keyboard1.nextLine();
    keyboard1.nextLine();  //add this line
    System.out.println("Done!");
    
    }
    }

  3. #3
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: String and Integer input bug

    Hi jktexas1, welcome to the forums.

    Please check the Scanner API

    The method 'nextLine()' "Advances this scanner past the current line and returns the input that was skipped".
    The method you want is simply 'next()'.

    Regards.

  4. #4
    jktexas1 is offline Member
    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Default Re: String and Integer input bug

    Thanks so much for the help guys, the next() function fixed the problem. My data structures teacher did say that the difference between all OO languages is IO, so I'm just trying to get through that. I finally relearned file IO for java so I think I'm making a pretty smooth transition so far. Again thanks for the help, this is an awesome forum!

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default Re: String and Integer input bug

    FileIO is based on the stuff in stdlib for C, at least the original IO.
    Scanner, however, is a different beast.
    You'd probably grasp the various Streams and Readers better.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. how can check input is double or integer?
    By HearT.Hunt3r in forum New To Java
    Replies: 6
    Last Post: 10-15-2011, 07:12 AM
  2. Testing if an input is an integer
    By Mayday in forum New To Java
    Replies: 1
    Last Post: 05-08-2010, 05:14 PM
  3. Replies: 3
    Last Post: 12-04-2009, 05:21 PM
  4. how to know the input value of integer
    By ran830421 in forum New To Java
    Replies: 15
    Last Post: 11-18-2009, 09:01 PM
  5. input to an integer (simply)
    By chitwood in forum Advanced Java
    Replies: 3
    Last Post: 03-18-2009, 06:34 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
  •