I'm writing a programm which finds the largest number in a .txt file and outputs it. I'm trying to solve it myself but end up back at the same problem "The type of the expression must be an array type but it resolved to String"... For less confusion I'm using class files (Input/Output) from my school.
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);
}
}
Error:
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 file:
33
123
0
55
800
-55
-1
777
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6 at Zahlenstatistik.main(Zahlenstatistik.java:12)when i replace it with array[i]i < array.lengthis correct; usingi < array.length-1would miss the last element.forstatement is wrong. It means the loop does nothing. The followingifis not part of the loop and it should be. Removing the semicolon will make it so.