简体   繁体   中英

Reading data from a text file in Java

I have a problem in reading data from a text file and put it in 2 dimensional array. The sample of dataset is:

1,2,3,4,5,6

1.2,2.3,4.5,5.67,7.43,8

The problem of this code is that it just read the first line and does not read the next lines. Any suggestion is appreciated.

package test1;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Test1{ 

public static void main(String args[])throws FileNotFoundException, IOException{
try{    

double[][] X = new double[2][6];
BufferedReader input = new BufferedReader(new FileReader(file));

String [] temp;
String line = input.readLine();
String delims = ",";
temp = line.split(delims);
int rowCounter = 0;
while ((line = input.readLine())!= null) {
for(int i = 0; i<6; i++){
X[rowCounter][i] = Double.parseDouble(temp[i]);
}

rowCounter++;
} 

}catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
}finally{
}
}
}

Have you tried the Array utilities? Something like this:

while ((line = input.readLine())!= null) {  
  List<String> someList = Arrays.asList(line.split(","));
  //do your conversion to double here
  rowCounter++;
}

I think the blank line might be throwing your for loop off

Try:

int rowCounter = 0;
while ((line = input.readLine())!= null) {
String [] temp;
String line = input.readLine();
String delims = ",";
temp = line.split(delims);
for(int i = 0; i<6; i++){
X[rowCounter][i] = Double.parseDouble(temp[i]);
}
...

The only place that your temp array is being assigned is before your while loop. You need to assign your temp array inside the loop, and don't read from the BufferedReader until the loop.

String[] temp;
String line;
String delims = ",";
int rowCounter = 0;
while ((line = input.readLine())!= null) {
    temp = line.split(delims);  // Moved inside the loop.
    for(int i = 0; i<6; i++){
    X[rowCounter][i] = Double.parseDouble(temp[i]);
}

readLine expects a new line character at the end of the line. You should put a blank line to read the last line or use read instead.

I couldn't run the code, but one of your problems is that you were only splitting the first text line.

package Test1;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Test1 {

    public static void main(String args[]) {
        try {
            double[][] X = new double[2][];
            BufferedReader input = new BufferedReader(new FileReader(file));

            String line = null;
            String delims = ",";

            int rowCounter = 0;
            while ((line = input.readLine()) != null) {
                String[] temp = line.split(delims);
                for (int i = 0; i < temp.length; i++) {
                    X[rowCounter][i] = Double.parseDouble(temp[i]);
                }
                rowCounter++;
            }

        } catch (Exception e) {// Catch exception if any
            System.err.println("Error: " + e.getMessage());
            e.printStackTrace();
        } finally {
        }
    }
}

I formatted your code to make it more readable.

I deferred setting the size of the second element of the two dimensional array until I knew how many numbers were on a line.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM