java 拆分字符串并将其转换为 int
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27345834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Splitting and converting String to int
提问by peter87
I have a problem with my code. I read a couple of numbers of a text-file. For example: Textfile.txt
我的代码有问题。我读了几个文本文件的数字。例如:Textfile.txt
1, 21, 333
With my following code I want to split and convert the numbers from String to int.
使用我的以下代码,我想将数字从 String 拆分并转换为 int。
int answer = 0;
int factor = 1;
// Splitting and deleting the "," AND converting String to int.
for (String retval : line.split(",")) {
for (int j = retval.length() - 1; j >= 0; j--) {
answer = answer + (retval.charAt(j) - '0') * factor;
factor *= 1;
}
System.out.println(answer);
answer = (answer - answer);
}
I get the result in my console (int):
我在控制台 (int) 中得到结果:
1 3 9
I see that the number 3 is a result of 2 + 1, and the number 9 is a result of 3 + 3 + 3. What can I do, to receive the following result in my console (int)?
我看到数字 3 是 2 + 1 的结果,数字 9 是 3 + 3 + 3 的结果。我该怎么做才能在我的控制台 (int) 中收到以下结果?
1 21 333
/EDIT: I am only allowed to use Java.lang and Java.IO
/编辑:我只被允许使用 Java.lang 和 Java.IO
回答by Alexis King
Here's a solution using Java 8 streams:
这是使用 Java 8 流的解决方案:
String line = "1,21,33";
List<Integer> ints = Arrays.stream(line.split(","))
.map(Integer::parseInt)
.collect(Collectors.toList());
Alternatively, with a loop, just use parseInt
:
或者,使用循环,只需使用parseInt
:
String line = "1,21,33";
for (String s : line.split(",")) {
System.out.println(Integer.parseInt(s));
}
If you really want to reinvent the wheel, you can do that, too:
如果你真的想重新发明轮子,你也可以这样做:
String line = "1,21,33";
for (String s : line.split(",")) {
char[] chars = s.toCharArray();
int sum = 0;
for (int i = 0; i < chars.length; i++) {
sum += (chars[chars.length - i - 1] - '0') * Math.pow(10, i);
}
System.out.println(sum);
}
回答by Tkachuk_Evgen
You can use Integer.valueOf(retval)
or Integer.parseInt(retval)
您可以使用Integer.valueOf(retval)
或Integer.parseInt(retval)
回答by alfasin
A couple of problems:
几个问题:
factor
should be multiplied by 10 in every loopanswer
andfactor
should be re-initialized between the numbers you're parsing:
factor
应该在每个循环中乘以 10answer
并且factor
应该在您解析的数字之间重新初始化:
String line = "1,21,333";
for (String retval : line.split(",")) {
int answer = 0;
int factor = 1;
for (int j = retval.length() - 1; j >= 0; j--) {
answer = answer + (retval.charAt(j) - '0') * factor;
factor *= 10;
}
System.out.println(answer);
answer = (answer - answer);
}
OUTPUT
输出
1
21
333
回答by Bruno
For folks who come here from a search, this function takes a separated values string and delimiter string and returns an integer array of extracted values.
对于通过搜索来到这里的人,此函数采用分隔值字符串和分隔符字符串,并返回提取值的整数数组。
int[] parseStringArr( String str, String delim ) {
String[] strArr = str.trim().split(delim);
int[] intArr = new int[strArr.length];
for (int i = 0; i < strArr.length; i++) {
String num = strArr[i].trim();
intArr[i] = Integer.parseInt(num);
}
return intArr;
}
回答by Roy Wasse
Well, you could make answer a string, and cast it to an integer after the for loop.
好吧,您可以将 answer 设为字符串,然后在 for 循环后将其转换为整数。