I have a .data file that looks like this:
I want my main method to be able to read the file and store it into a 2-dimensional int array, there is a "?" in the data I want it to be converted to a 0.
I have this code so far, but it doesn't work and I get a null pointer exception (probably from initializing my 2-d array to null). The problem is if I don't initialize my 2-d array to null it gives me errors EDIT (this answers part of the problem, but how can I get java to get the size of the number of rows in the data and then initialize dataSet to int[number of rows][numbers in each row], I can't even get java to read the file in the first place)
public static void main(String[] args){
int[][] dataSet = null;
try {
Scanner inFile = new Scanner(new File("C:\\Data Folder\\file.data"));
while (inFile.hasNextInt()){
for (int i = 0; i < dataSet.length; i++){
for (int j = 0; j < dataSet[i].length;){
dataSet[i][j] = inFile.nextInt();
}
System.out.println(dataSet[i]);
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I have never tinkered around with files on Java so this is all new to me.
Any advice is appreciated
EDIT: 3:11 pm
So I changed my 2-d array to array list instead, thanks Masud:
public static void main(String[] args){
List<Integer> dataSet;
try {
Scanner inFile = new Scanner(new File("C:\\Data Folder\\file.data"));
while (inFile.hasNextInt()){
for (int i = 0; i < dataSet.size(); i++){
dataSet.add(inFile.nextInt());
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Eclipse is still asking me to set dataSet = null. How would I get the size of the number of rows and number of numbers in each row from the .data file?
EDIT: 3:23 pm
Thank you Merlin for implementing Masud's suggestion, I have updated my main method to represent your code just to see if it works. I keep getting this error:
Exception in thread "main" java.lang.NumberFormatException: For input string: "1 15943882 63 1 -9 -9 -9"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
Here is the code that I used:
public static void main(String[] args){
List<int[]> dataSet = new ArrayList<int[]>();
try {
Scanner inFile = new Scanner(new File("C:\\Data Folder\\file.data"));
while (inFile.hasNextLine()){
String line = inFile.nextLine();
String[] cols = line.split(",");
int[] rows = new int[cols.length];
for (int i = 0; i < cols.length; i++){
if (!cols[i].equals("?")){
rows[i] = Integer.parseInt(cols[i]);
}
else {
rows[i] = 0;
}
}
dataSet.add(rows);
}
System.out.println(dataSet.size());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
It also tell me add "@suppress warning" to inFile.
EDIT
I fixed the error by changing the file path to a processed data file, sorry about that that's my fault. Merlin and Masud thank you for the guidance!
If your code has to tackle data with different sizes, then using a fixed-size array is not the right answer. However, you can ask Java to figure out the number of columns for you.
List<int[]> dataSet = new ArrayList<int[]>();
while (inFile.hasNextLine()){
String line = inFile.nextLine();
String[] cols = line.split(",");
int[] row = new int[cols.length];
for (int i = 0; i < cols.length; i++)
if (!cols[i].equals("?")) row[i] = Integer.parseInt(cols[i]);
else row[i] = 0;
dataSet.add(row);
}
You've answered your question. The problem is that dataSet is null therefore the NullPointerException. You have to initialize it before using
int[][] dataSet = new int[size1][size2]
where size1 and size2 are the known sizes of the array. And here comes the second problem, would you know about the sizes before reading the file. First I would change 2 dimension array to list of list, but there could be better data structures as well.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.