使用 StringTokenizer 输入 5 个整数
下面是部分代码。
import java.io.*;
import java.util.*;
public class inputting {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int i,j;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter numbers??");
String str = br.readLine();
StringTokenizer tokenizer = new StringTokenizer(str);
j=Integer.parseInt(tokenizer);
}
}
现在我不知道 StringTokenizer
会去哪里,也不知道我到目前为止所做的是否正确。但基本上我希望用户请求 5 个整数,然后打印它。
如何从字符串中取出整数?如果有人能为我完成代码,我将非常感激。
PS这不是作业问题,我正在学习面向对象编程而不是Java。所以我们的教授并没有足够的热情来教我们完整的Java语法。
Below is the partial code.
import java.io.*;
import java.util.*;
public class inputting {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int i,j;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter numbers??");
String str = br.readLine();
StringTokenizer tokenizer = new StringTokenizer(str);
j=Integer.parseInt(tokenizer);
}
}
Now I don't know where would StringTokenizer
go and whether what I have done till now is correct or not. But basically I want user to ask for 5 integers and then let say just print it.
How do I take the integers out of the strings? If someone could complete the code for me, I would really appreciate.
P.S. This is not a homework question, I am learning object oriented programming and not Java. So our professor didn't care enough to teach us the syntax of Java completely.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 StringTokenizer 使用指定的分隔符将字符串分解为多个字符串(或标记)。
例如:
注意:没有运行这个,可能包含语法错误,但你明白了。
StringTokenizer JavaDoc 在这里: http://download.oracle .com/javase/1.4.2/docs/api/java/util/StringTokenizer.html
You would use StringTokenizer to break up a string into multiple strings(or tokens) using a specified delimiter.
For example:
Note: didn't run this, might contain syntax error, but you get the idea.
StringTokenizer JavaDoc here: http://download.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html
您可能会发现使用字符串的 split() 方法更容易。
这条线
j=Integer.parseInt(tokenizer);
无法编译,StringTokenizer 不是字符串。
j=Integer.parseInt(tokenizer.nextToken());
会编译,因为这是一个字符串,因此请随意使用它,而不是上面使用 String 类中的 split() 方法的代码。
You may find it easier to use a string's split() method.
the line
j=Integer.parseInt(tokenizer);
will not compile, a StringTokenizer is not a string.
j=Integer.parseInt(tokenizer.nextToken());
will compile because that is a string, so feel free to use that rather than the code presented above that uses the split() method from the String class.