Frage

In Java, can I do

StringTokenizer stk = new StringTokenizer(System.in);

?

I am wondering if I can expect to collect the input stream as a sting like that or is there a better way of getting the user input converted to a string before I can use it as an argument for my string tokenizer?

Thanks!

UPDATE: I am working from a school computer that won't let me test my code right now. I will need to get to my own computer or one that has admin privileges for me to try my code. So, please bear with me.

War es hilfreich?

Lösung

You cannot do that because the constructor for StringTokenizer() requires an argument of type java.lang.String, but System.in is of type java.io.InputStream.

A better way of converting user input to a String would be to use a Scanner

Scanner sc = new Scanner(System.in);
System.out.println("Enter name: ");
String name = sc.nextLine();
System.out.println("Enter age: ");
String age = sc.nextLine();
System.out.println(name + " is " + age + "years old");
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top