문제

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.

도움이 되었습니까?

해결책

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");
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top