0

input name and section

what is my mistakeimport java.util.*; public class ScannerClassExample1 { public static void main(String args[]){ String s = "Hello."; Scanner scan = new Scanner(s); System.out.println("Boolean Result: " + scan.hasNext()); System.out.println("String: " +scan.nextLine()); scan.close(); System.out.println("--------Enter Your Details-------- "); Scanner in = new Scanner(System.in); System.out.print("Enter your name: "); String name = in.next(); System.out.println(name); System.out.print("Enter your section: "); String section = in.next(); System.out.println(section); in.close(); } }

24th Jan 2019, 12:06 PM
Richweine Kyle Torres Evaristo
Richweine Kyle Torres Evaristo - avatar
1 Odpowiedź
+ 1
Your using of the Scanner is wrong. Scanner scan = new Scanner (System.in); scan has to read all your inputs. for example: String details = scan.nextLine (); String name = scan.nextLine (); String section = scan.nextLine(); At the end of your program you close this scanner with scan.close(). If you close it before you can not use it anymore. If you want only one digit/word as String you can choose next () Input hello: scan.next () --> hello Input hello world: scan.next () --> hello (because of the whitespace) nextLine () takes the whole line
24th Jan 2019, 2:30 PM
Denise Roßberg
Denise Roßberg - avatar