Brandon Jake Sullano
Brandon Jake Sullano

Reputation: 335

Java ask String input with multiple words

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

Upvotes: 3

Views: 18047

Answers (3)

UnknownOctopus
UnknownOctopus

Reputation: 2287

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.

Upvotes: 4

Gsquare
Gsquare

Reputation: 700

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

Upvotes: 1

Watermel0n
Watermel0n

Reputation: 241

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

Upvotes: 5

Related Questions