使用 StringTokenizer 输入 5 个整数

发布于 2024-11-30 10:25:20 字数 671 浏览 5 评论 0原文

下面是部分代码。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟若柳尘 2024-12-07 10:25:20

您可以使用 StringTokenizer 使用指定的分隔符将字符串分解为多个字符串(或标记)。

例如:

String sample = "1,2,3,4,5";
StringTokenizer tokenizer = new StringTokenizer(sample,",");

while(tokenizer.hasMoreTokens()) {
  int convertedToInt = Integer.parseInt(tokenizer.nextToken());
}

注意:没有运行这个,可能包含语法错误,但你明白了。

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:

String sample = "1,2,3,4,5";
StringTokenizer tokenizer = new StringTokenizer(sample,",");

while(tokenizer.hasMoreTokens()) {
  int convertedToInt = Integer.parseInt(tokenizer.nextToken());
}

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

べ繥欢鉨o。 2024-12-07 10:25:20

您可能会发现使用字符串的 split() 方法更容易。

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();
     String[] nums = str.split(" "); //returns an array of strings split based on the parameter
     i = Integer.parseInt(nums[0]);
     j = Integer.parseint(nums[1]);
}
}

这条线
j=Integer.parseInt(tokenizer);
无法编译,StringTokenizer 不是字符串。

j=Integer.parseInt(tokenizer.nextToken());
会编译,因为这是一个字符串,因此请随意使用它,而不是上面使用 String 类中的 split() 方法的代码。

You may find it easier to use a string's split() method.

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();
     String[] nums = str.split(" "); //returns an array of strings split based on the parameter
     i = Integer.parseInt(nums[0]);
     j = Integer.parseint(nums[1]);
}
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文