Java ask String input with multiple words -
hi having problem 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 enter information below:
id = 100 name = brandon sullano
it gives me result
id 100 name brandon specializtion sullano
i want name dynamic can input 2 words how it?
in advance..
try 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 integer value */ system.out.print("enter id number: "); idnumber = sc.nextint(); sc.nextline(); // important, clearing 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
Comments
Post a Comment