java - How can I get different data types from the keyboard in just one line? -
i need keyboard in 1 line this:
create 1000 john 4000 (for cinema program, include function create users)
where "create" command create new user (create class, , contains client information), "1000" id of user (int), "john" name(string), , "4000" points(int)
how can different data types in 1 line stored in different attributes client?
i use scanner object based on system.in read line in, using if (myscanner.hasnextline()) , myscanner.nextline() method pairs.
then line need parsed, , here i'd offer 2 possible ways:
- consider using new scanner based on line read in, string variable called
line, "line scanner" speak,scanner linescanner = new scanner(line), , tokens vialinescanner.next(),linescanner.nextint(),linescanner.next(), ,linescanner.nextint(), in order. don't forgetclose()linescanner when done using it. - or use
string#split(" ")split string array of 4 substrings separated space.
then in loop parse line, create object needed based on data obtained.
Comments
Post a Comment