I am trying to output the split contents of a .txt
file.
Here is my code so far:
import java.io.FileReader;
import java.io.BufferedReader;
public class PassFail {
public static void main(String[] args) {
String path = "C:\\new_java\\Final_Project\\src\\student.txt";
try {
FileReader file = new FileReader(path);
BufferedReader reader = new BufferedReader(file);
String line = reader.readLine();
reader.close();
String[] values = line.split(" ");
int nums[] = new int[values.length];
for (int x = 0; x < values.length; x++) {
nums[x] = Integer.parseInt(values[x]);
}
} catch (Exception e) {
System.out.println("Error:" + e);
}
System.out.println(nums[1]);
}
}
Question: Why am I getting the error "nums cannot be resolved to a variable
" when trying to output num[2]
? Furthermore, how can I fix this? To my knowledge I have already declared nums[]
as a an int data type just before the for
loop.