3

HI I am having problem with asking Direct Whole name using Java.

System.out.println("DOCTOR");
System.out.println("Enter ID Number:");
idnumber = scan.next();
System.out.println("Enter Name");
name = scan.next();
System.out.println("Enter Field of Specialization:");
field = scan.next();

System.out.println("ID               " + idnumber);
System.out.println("Name             " + name);
System.out.println("Specializtion    " + field );

and When I enter this information below:

ID = 100 Name = Brandon Sullano

it gives me this result

ID               100    
Name             Brandon    
Specializtion    Sullano

I want Name to be dynamic so I can Input even two words how to do it?
Thanks in Advance..

6
  • Have you looked at the documentation for Scanners methods? Commented Jul 18, 2015 at 2:04
  • I search code but I cant find will look on that documentation Commented Jul 18, 2015 at 2:05
  • Try oracles documentation. Like or oracles website. Or like, use the google Commented Jul 18, 2015 at 2:06
  • But do you know how to do it? I saw instead of scan.next will use scan.nextLine() but doesn't work Commented Jul 18, 2015 at 2:09
  • Explain "does not work." Here at SO, we are a little touchy about that phrase. Commented Jul 18, 2015 at 2:11

3 Answers 3

5

Try this code:

import java.util.Scanner;

public class DoctorDoctor {

    public static void main (String [] args) {

        int idnumber;
        String name, field;

        Scanner sc = new Scanner(System.in);

        System.out.println("DOCTOR");

        /** Assuming ID number is an integer value */
        System.out.print("Enter ID Number: ");
        idnumber = sc.nextInt();
        sc.nextLine(); // This is important, for clearing the new line character

        System.out.print("Enter Name: ");
        name = sc.nextLine();

        System.out.print("Enter Field of Specialization: ");
        field = sc.nextLine();

        System.out.println();
        System.out.printf("%-15s: %d%n", "ID Number", idnumber);
        System.out.printf("%-15s: %s%n", "Name", name);
        System.out.printf("%-15s: %s%n", "Specialization", field);

        sc.close();
    }
}

Example input/output:

DOCTOR
Enter ID Number: 100
Enter Name: Brandon
Enter Field of Specialization: Heart Surgeon

ID Number      : 100
Name           : Brandon
Specialization : Heart Surgeon
Sign up to request clarification or add additional context in comments.

Comments

4

Use:

name = scan.nextLine();

Also, if the ID number is an integer, use nextInt() (make sure to also declare idnumber as an int instead of string)

Fixed code:

System.out.println("DOCTOR");
System.out.println("Enter ID Number:");
idnumber = scan.nextInt();
scan.nextLine();

System.out.println("Enter Name");
name = scan.nextLine();
System.out.println("Enter Field of Specialization:");
field = scan.nextLine();

System.out.println("ID               " + idnumber);
System.out.println("Name             " + name);
System.out.println("Specializtion    " + field );

Using just next() will take all input before a space. nextLine() will take all input before return is pressed.

More on Scanner here

Pro tip: In most cases you'll use scan.nextLine() rather than scan.next() so you may want to get into the habit of using it.

5 Comments

it doesn't work when I run System will jump asking me for Name and skip the Id number
Works fine on my end.
it will ask directly to Name and skip the ID number
@UnknownOctopus Pro tip: idnumber = scan.nextInt(); needs to have a scan.nextLine() after it. Else, name = scan.nextLine(); will consume the new line character, causing the program to jump to "Enter field of.."
@Watermel0n Edited. Thanks.
1

Since you say scan.nextLine() "doesn't work", I suggest using BufferedReader.

BufferedReader br=new BufferedReader(new InputStreamReader(System.in);
//in your code:
int idnumber = Integer.parseInt(br.readLine());
String name = br.readLine();
String field = br.readLine(); 

You have to import java.io.BufferedReader and java.io.InputStreamReader

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.