java - Program goes funny when letter + space + letter is entered -


when enter value "adam j" first name, following outputted:

please enter student first name: adam j please enter student surname: please select subject unit:  

i reject value, if number entered. ideally, don't want accept if has space in "please enter valid first name".

 case 1:                 system.out.print("please enter student first name: ");                 firstname = scan.next();                   while(!firstname.matches("[-a-za-z]*"))                 {                     system.out.print("please enter valid first name: ");                     firstname = scan.next();                  }                 firstcap = firstname.substring(0,1).touppercase() + firstname.substring(1);      system.out.print("please enter student surname: ");         lastname = scan.next();          while(!lastname.matches("[-a-za-z]*"))         {             system.out.print("please enter valid surname: ");             lastname = scan.next();         }         surcap = lastname.substring(0,1).touppercase() + lastname.substring(1);          system.out.print("please select subject unit: ");          unit = scan.next();  

i confused why: "please enter student surname: please select subject unit:" on same line well.

can help? thanks

the next() method retrieves next token stream, default delimited whitespace. call next() first name consumes adam. next call next() consumes j, leaving @ third prompt.

you should call nextline() instead, retrieve entire line, adam j consumed @ once firstname.


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -