0

I have this text file and I need to extract it to a Java program to determine array sizes and its values I can read each line alone but I can't get the value for lines that contains many numbers. Plus, how to count element in the third line.( 0 2 )

4
5
0 2

0 1 0.6
0 2 0.2
0 3 0.5
1 3 0.8
2 3 0.3

this code that I'm using:

 List<Integer> list = new ArrayList<Integer>();
        File file = new File("D:\\ALGORTHIMS\\MASTER LEVEL\\dr. khaled\\assignment 1\\text.txt");
        BufferedReader reader = null;
         List<Double> ints = new ArrayList<Double>();

        try {
            reader = new BufferedReader(new FileReader(file));
            String text = null;

            while ((text = reader.readLine()) != null) {
                if (text.length() == 1) {
                    list.add(Integer.parseInt(text));
                } else {


                    String[] strs = text.trim().split("\\s+");


                    for (int i = 0; i < strs.length; i++) {
                        ints.add(Double.parseDouble(strs[i]));
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {

            }
        }

//print out the list
        System.out.println(list);

        System.out.println(ints);
SubhiMMM
  • 28
  • 1
  • 7

4 Answers4

0

It looks like you are using doubles mixed with int so Integer.parseInt() will not work.

What you could do is to split the line, separated by a space into an array, and put each of those elements into list. Note that this will print 0 as 0.0.

while ((text = reader.readLine()) != null) {
      String[] numbers = text.split(" ");
      for (String number : numbers) {
           list.add(Double.valueOf(number));
      }
}

Also, did you just copy this answer for your code?

achAmháin
  • 4,176
  • 4
  • 17
  • 40
0

Try something like

public static void main(String[] args) {
        List<Double> list = new ArrayList<>();
        File file = new File("text.txt");
        BufferedReader reader = null;

        try {
            reader = new BufferedReader(new FileReader(file));
            String text = null;

            while ((text = reader.readLine()) != null) {
                String[] str = text.split(" ");
                for (int i = 0; i<str.length; i++) {
                    if(str[i].trim().length() > 0) {
                        list.add(Double.parseDouble(str[i]));
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                System.out.print(e);
            }
        }

//print out the list
        System.out.println(list);
    }
  • `Double.parseDouble(str[i])` is platform specific and do not support `Locale`. Therefore you could have an exception on different laptops. – Oleg Cherednik Nov 13 '17 at 13:40
0

I think that using Scanner is optimal solution. E.g. in case we have file

4 5

 0  1  2  3.1
 4  5  6  7.2
 8  9 10 11.3
12 13 14 15.4
16 17 18 19.5

where we define an array with 4 row and 5 columns. Then to get it in int[][] arr we could use following approach:

public static void main(String... args) throws FileNotFoundException {
    double[][] arr = null;

    try (Scanner scan = new Scanner(new FileInputStream(new File("text.txt")))) {
        scan.useLocale(Locale.US);  // to use '.' as separator
        int rows = scan.nextInt();
        int columns = scan.nextInt();
        scan.nextLine();

        arr = new double[rows][columns];

        for (int i = 0; i < rows; i++)
            for (int j = 0; j < columns; j++)
                arr[i][j] = scan.nextDouble();
    }

    System.out.println(Arrays.toString(arr));
}
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
0

You can use java.util.scanner to parse various data types from a stream relatively painlessly.

For example, suppose you wanted to parse the following:

1 2 3.0
4.0 5 6

You could use the following snippet:

Scanner scan = new Scanner(System.in);
int one = scan.nextInt();
int two = scan.nextInt();
double three = scan.nextDouble();
// Defaultly, it will continue past the line break
double four = scan.nextDouble();

// We can also do this
double five = scan.nextDouble();
long six = scan.nextLong();

An important note is that java.util.Scanner will suppress IOExceptions (although, exceptions from converting text, e.g. NumberFormatExceptions, will still be raised). You can check ioException to see if one has been suppressed.

abernardi597
  • 123
  • 7