Java asks for multiple word string input

HI I'm having a problem with the Direct Whole name query 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 the name to be dynamic so I can enter even two words, how to do that?
Thanks in Advance ..

+3


source to share


3 answers


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();
    }
}

      



I / O example:

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

ID Number      : 100
Name           : Brandon
Specialization : Heart Surgeon

      

+2


source


Using:

name = scan.nextLine();

      

Also, if the id is an integer use nextInt()

(remember to also declare idnumber

as 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 );

      

Usage next()

will only accept all data before a space. nextLine()

will accept all input before hitting the return button.

More about Scanner

here

Tip: Most of the time you will use it scan.nextLine()

, not scan.next()

so you can get used to using it.

+4


source


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 need to import java.io.BufferedReader

andjava.io.InputStreamReader

+1


source







All Articles