Java 来自字符串的 parseInt,无法使用数组
Java parseInt from String, not able use array
我正在编写一个程序,它在 .txt 文件中找到最大的数字并将其输出。我正在尝试自己解决它,但最终还是遇到了同样的问题 "The type of the expression must be an array type but it resolved to String"...为了减少混淆,我使用了我学校的 class 文件 (Input/Output)。
public class Zahlenstatistik {
public static void main (String[] args) {
In.open("test.txt");
String numbers = In.readFile();
Integer max = Integer.MIN_VALUE;
int i = 0;
String[] alle_zahlen = numbers.split("\n");
for(i = 0; i < alle_zahlen.length; i++)
if (max < Integer.parseInt(alle_zahlen[i]))
max = Integer.parseInt(alle_zahlen[i]);
System.out.println("Die groeste Zahl ist: " + max);
}
}
错误:
Exception in thread "main" java.lang.NumberFormatException: For input string: "33"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Zahlenstatistik.main(Zahlenstatistik.java:12)
test.txt 文件:
33
123
0
55
800
-55
-1
777
由于编译器 returns 错误:The type of the expression must be an array type but it resolved to String
,它告诉您您正在执行一个需要数组但得到的是字符串的操作。在这种情况下,您使用字符串代替数组。
例如,当您编写 max = Integer.parseInt(numbers[i]);
和 if (max < Integer.parseInt(numbers[i]));
.
时,您正在使用 numbers
作为数组
相反,您应该使用 array
。
因此,您应该将代码更改为
max = Integer.parseInt(array[i]);
和 if (max < Integer.parseInt(array[i]));
此外,您的 for 循环或 if 语句后不应该有 ;
。这改变了语句的使用方式,它只会坐在那里并增加您的变量而不会实际重复任何内容。这最终导致了你的越界异常。
您使用了 numbers
而不是 array
,其中数字是一个字符串。另外,我建议使用比 array
更好的名称来命名数组
你应该使用整数流
s.stream().reduce(Integer::max).get());
您可以根据需要添加 .parallel。
问题有点变化,但这里有一个解决无效数字格式的潜在建议。请注意,文件编码也可能起作用。也可以通过类似
的方式从输入中去除其他字符
String rawValue = alle_zahlen[i].replaceAll("[^-?\d]+", "");
此外,最好的做法是始终使用大括号,所以我添加了大括号
for(i = 0; i < alle_zahlen.length; i++) {
String rawValue = alle_zahlen[i].trim();
try {
int value = Integer.parseInt(rawValue);
max = Math.max(max, value);
// NOTE: instead of Math.max, can also do (which is essentially what
// Math.max() does)
// max = max > value ? max : value;
} //try
catch (NumberFormatException e) {
e.printStackTrace();
}
} //for
System.out.println("Die groeste Zahl ist: " + max);
如果您想使用 Java 8 个流,您也可以这样做:
System.out.println("Die groeste Zahl ist: " +
Arrays.stream(alle_zahlen)
.mapToInt(s -> Integer.parseInt(s.replaceAll("[^-?\d]+", "")))
.max()
.getAsInt());
两种方法都针对以下模拟输入进行了测试:
final String numbers = "33 \n123\n0\n55\n800\n-55\n-1\n777\n";
final String[] alle_zahlen = numbers.split("\n");
这将适用于 Java 8:
package com.sial.workarounds;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.stream.Collectors;
public class Zahlenstatistik {
public static void main(String[] args) throws IOException {
File f = new File("test.txt");
BufferedReader reader = new BufferedReader(new FileReader(f));
String numbers = String.join("\n", reader.lines().collect(Collectors.toList()));
reader.close();
Integer max = Integer.MIN_VALUE;
int i = 0;
String[] alle_zahlen = numbers.split("\n");
for (i = 0; i < alle_zahlen.length; i++)
if (max < Integer.parseInt(alle_zahlen[i]))
max = Integer.parseInt(alle_zahlen[i]);
System.out.println("Die groeste Zahl ist: " + max);
}
}
我正在编写一个程序,它在 .txt 文件中找到最大的数字并将其输出。我正在尝试自己解决它,但最终还是遇到了同样的问题 "The type of the expression must be an array type but it resolved to String"...为了减少混淆,我使用了我学校的 class 文件 (Input/Output)。
public class Zahlenstatistik {
public static void main (String[] args) {
In.open("test.txt");
String numbers = In.readFile();
Integer max = Integer.MIN_VALUE;
int i = 0;
String[] alle_zahlen = numbers.split("\n");
for(i = 0; i < alle_zahlen.length; i++)
if (max < Integer.parseInt(alle_zahlen[i]))
max = Integer.parseInt(alle_zahlen[i]);
System.out.println("Die groeste Zahl ist: " + max);
}
}
错误:
Exception in thread "main" java.lang.NumberFormatException: For input string: "33"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Zahlenstatistik.main(Zahlenstatistik.java:12)
test.txt 文件:
33
123
0
55
800
-55
-1
777
由于编译器 returns 错误:The type of the expression must be an array type but it resolved to String
,它告诉您您正在执行一个需要数组但得到的是字符串的操作。在这种情况下,您使用字符串代替数组。
例如,当您编写 max = Integer.parseInt(numbers[i]);
和 if (max < Integer.parseInt(numbers[i]));
.
numbers
作为数组
相反,您应该使用 array
。
因此,您应该将代码更改为
max = Integer.parseInt(array[i]);
和 if (max < Integer.parseInt(array[i]));
此外,您的 for 循环或 if 语句后不应该有 ;
。这改变了语句的使用方式,它只会坐在那里并增加您的变量而不会实际重复任何内容。这最终导致了你的越界异常。
您使用了 numbers
而不是 array
,其中数字是一个字符串。另外,我建议使用比 array
更好的名称来命名数组
你应该使用整数流
s.stream().reduce(Integer::max).get());
您可以根据需要添加 .parallel。
问题有点变化,但这里有一个解决无效数字格式的潜在建议。请注意,文件编码也可能起作用。也可以通过类似
的方式从输入中去除其他字符String rawValue = alle_zahlen[i].replaceAll("[^-?\d]+", "");
此外,最好的做法是始终使用大括号,所以我添加了大括号
for(i = 0; i < alle_zahlen.length; i++) {
String rawValue = alle_zahlen[i].trim();
try {
int value = Integer.parseInt(rawValue);
max = Math.max(max, value);
// NOTE: instead of Math.max, can also do (which is essentially what
// Math.max() does)
// max = max > value ? max : value;
} //try
catch (NumberFormatException e) {
e.printStackTrace();
}
} //for
System.out.println("Die groeste Zahl ist: " + max);
如果您想使用 Java 8 个流,您也可以这样做:
System.out.println("Die groeste Zahl ist: " +
Arrays.stream(alle_zahlen)
.mapToInt(s -> Integer.parseInt(s.replaceAll("[^-?\d]+", "")))
.max()
.getAsInt());
两种方法都针对以下模拟输入进行了测试:
final String numbers = "33 \n123\n0\n55\n800\n-55\n-1\n777\n";
final String[] alle_zahlen = numbers.split("\n");
这将适用于 Java 8:
package com.sial.workarounds;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.stream.Collectors;
public class Zahlenstatistik {
public static void main(String[] args) throws IOException {
File f = new File("test.txt");
BufferedReader reader = new BufferedReader(new FileReader(f));
String numbers = String.join("\n", reader.lines().collect(Collectors.toList()));
reader.close();
Integer max = Integer.MIN_VALUE;
int i = 0;
String[] alle_zahlen = numbers.split("\n");
for (i = 0; i < alle_zahlen.length; i++)
if (max < Integer.parseInt(alle_zahlen[i]))
max = Integer.parseInt(alle_zahlen[i]);
System.out.println("Die groeste Zahl ist: " + max);
}
}