-2

I am new at java and teacher gave us a project homework. I have to implement read the file line by line, slice the lines at the comma and store the parts at a multidimensional array, change the specific part of the line (I want to change the amount). The given file:

product1,type,amount
product2,type,amount
product3,type,amount
product4,type,amount
product5,type,amount

I tried this code but I couldn't change the specific part.

BufferedReader reader;
        int j=0;
        int i=0;
        try {
            reader = new BufferedReader(new FileReader("file.txt"));
            String line = reader.readLine();


            while (line != null) {
                j++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        String total_length[][]=new String[j][3];

            try {
            reader = new BufferedReader(new FileReader("file.txt"));
            String line = reader.readLine();


            while (line != null) {
                line = reader.readLine();
                String[] item = line.split(",");
                total_length[i][0]=item[0];
                total_length[i][1]=item[0];
                total_length[i][2]=item[0];
                i++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

Thanks a lot!

Umut
  • 5
  • 5
  • What did you try? – John Mar 06 '19 at 21:33
  • I used BufferedReader to read the file and I used for loop to store the value in array but I coulnd't write. – Umut Mar 06 '19 at 21:36
  • Couldn't write? You didn't mention writing at all in your question. Please edit the question to clarify what you need and include the code and any relevant error messages. – John Mar 06 '19 at 21:37
  • I am sorry, I mean "I couldn't change the specific part" – Umut Mar 06 '19 at 21:39
  • So you successfully generated the multidimensional array and are having difficulty modifying an element of that array? – John Mar 06 '19 at 21:40
  • I want to modify the file and my code may be wrong. – Umut Mar 06 '19 at 21:43
  • It would appear that you have an infinite loop. Did you try running your code? – John Mar 07 '19 at 16:14

1 Answers1

0

First, you need to read the file. There are plenty of way to do it, one of them is:

BufferedReader s = new BufferedReader(new FileReader("filename"));

Which allows you to do s.readLine() to read it line by line. You can use a while loop to read it until the end. Note that readLine will return null if you reach the end of the file. Then, for each line, you want to split them with the coma. You can use the split method of Strings:

line.split(",");

Putting it all together, and using a try-catch for IOException, you get:

    List<String[]> result = new ArrayList<>();
    try (BufferedReader s = new BufferedReader(new FileReader("filename"))) {
        String line;
        while ((line = s.readLine()) != null) {
            result.add(line.split(","));
        }
    } catch (IOException e) {
        // Handle IOExceptions here
    }

If you really need a two dimensional array at the end, you can do:

    String[][] array = new String[0][0];
    array = result.toArray(array);

You then have read the file in the format you wanted, you can now modify the data that you parsed.

Paul Renauld
  • 186
  • 6