Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Scanner problem

  1. #1
    Member melki0795's Avatar
    Join Date
    Nov 2013
    Location
    Malta
    Posts
    49
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Scanner problem

     
    import java.util.Scanner;
    public class RecordingCalculating
    {
        public static void main(String[] args){
          Scanner sc = new Scanner(System.in);
     
          System.out.println("Student Number: ");
          String numberEntered = sc.nextInt();
          numberEntered = numberCheck(numberEntered, sc);
     
          System.out.print("Family name: ");
          String familyName = sc.nextLine();
     
          System.out.print("First Name: ");
          String firstName = sc.nextLine();
     
     
     
        }

    I have this code. Any idea why Family name: and First Name are outputted near eachother.. and it only lets me input 1


  2. #2
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default

    Clear the scanners buffer after you read a number.

    // read a number

    sc.nextLine(); // clear buffer

    // read a String

  3. The Following 2 Users Say Thank You to Kewish For This Useful Post:

    Jad_Asmar (November 23rd, 2013), melki0795 (November 22nd, 2013)

  4. #3
    Member melki0795's Avatar
    Join Date
    Nov 2013
    Location
    Malta
    Posts
    49
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Scanner problem

    Thanks mate

  5. #4
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default

    Welcome

Similar Threads

  1. [SOLVED] Scanner problem
    By sentimentGX4 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 30th, 2012, 06:09 PM
  2. Scanner Problem
    By Syahdeini in forum What's Wrong With My Code?
    Replies: 8
    Last Post: August 9th, 2012, 08:37 PM
  3. Problem with Scanner
    By Rafal75 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: May 7th, 2012, 08:34 PM
  4. Scanner problem - need help
    By destructobob in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 23rd, 2011, 02:08 AM
  5. [SOLVED] Problem with Scanner ?
    By derekeverett in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 19th, 2010, 04:34 AM