I don't know how to handle java strings and ArrayList.

Asked 2 years ago, Updated 2 years ago, 109 views

Hi, nice to meet you.I have been studying java for about 3 months.
The text file printed with the following code will never be the ideal value.

Specifically, two text files are taken for input.Each text file is formatted like a comma-separated CSV file, for example,

1,2,3,4,5
2,,5

1,3,5,7,9

It looks like .Some lines are empty or empty.

What I want to do with this code is to add the two text files that match each other and output them to a new text file.Apart from the previous example,

1,1,1,1
2,2,2,2
3,3,3
4,4
5

When prepared, the output is as follows:

2,3,4,5,6
4,2,7,2
3,3,3
5,7,5,7,9
5

What doesn't work is that after the second line, the calculation doesn't work, the empty line doesn't work, and I don't know if the output method is correct. I'm sure there are other things that don't work, but please let me know.

Also, I think there is definitely a good alternative, so I would appreciate it if you could suggest it as an alternative solution.

Thank you for your cooperation.

import java.io.*;
import java.util.*;

public classNum_Array{
    public static void main(String[]args) {
        ArrayList<ArrayList<Integer>>list=new ArrayList<ArrayList<Integer>(); 
        ArrayList<Integer>row;

        try{
            FileReader fr=new FileReader("1041.txt");
            FileReader fr2 = new FileReader ("1042.txt");
            BufferedReader br = new BufferedReader (fr);
            BufferedReader br2 = new BufferedReader (fr2);
            File f = new File ("output104.txt");
            FileWriter filewriter = new FileWriter(f);
            String str1 = null;
            str1 = br.readLine();
            String str2 = null;
            str2 = br2.readLine();
            
            while(str1!=null&str2!=null){
                int num = 0;
                int add_num = 0;
 
                String [ ] nstr1 = str1.split(", ");
                String [ ] nstr2 = str2.split(", ");
                row=new ArrayList<Integer>();
           
                for(inti=0;i<nstr1.length||i<nstr2.length;i++){
                    if(i<nstr1.length&i<nstr2.length){
                        if(nstr1[i].equals(""){
                            nstr1[i] = "0";
                        }
                        if(nstr2[i].equals(""){
                            nstr2[i] = "0";
                        }
                        // System.out.println(nstr2[i]);
                        num = Integer.valueOf(nstr1[i]);
                        add_num = Integer.valueOf(nstr2[i]);
                        num+=add_num;
                        row.add(num);
                    }
                
                    if(i>=nstr1.length){
                        row.add(Integer.valueOf(nstr2[i]));
                    } else if(i>=nstr2.length) {
                        row.add(Integer.valueOf(nstr1[i]));
                    }
                }
                list.add(row);
                str1 = br.readLine();
                str2 = br.readLine();
            }
            for(inti=0;i<list.size();i++){
                for(int j=0;j<list.get(i).size();j++){
                    filewriter.write(String.valueOf(list.get(i).get(j)));
                    if(j!=list.get(i).size()-1){
                        filewriter.write(", ");
                    } else {
                        filewriter.write("\n");
                    }
                }
            }
            br.close();
            br2.close();
            filewriter.close();
            fr.close();
            fr2.close();
        }catch(IOExceptione){
            System.out.println(e.getMessage());
        }
    }
}

java text-file

2022-09-30 19:26

1 Answers

There are two problems.

The first is to confuse the two BufferedReaders when reading the next line.

str1=br.readLine();
str2 = br.readLine();

The second line of the quote above should use br2.

The second condition is to finish reading the file.

while(str1!=null&str2!=null){

The longer line file is not processed until the end because either end will exit the loop.

Will both continue until null (where null is set to a dummy)?

while(str1!=null||str2!=null){
    // ...
    String [ ] nstr1 = str1! = null?str1.split(", "): new String [0];
    String [ ] nstr2 = str2! = null?str2.split(", "): new String [0];

Also, this is not a direct problem, but

if(i<nstr1.length&i<nstr2.length){
    // ...
}

if(i>=nstr1.length){
    // ...
} else if(i>=nstr2.length) {
    // ...
}

The conditions of are in the same column, so

if(i<nstr1.length&i<nstr2.length){
    // ...
} else if(i>=nstr1.length){
    // ...
} else if(i>=nstr2.length){
    // ...
}

I think it would be better if

"As for ""another method is good"", it is difficult to answer because of the wide range of questions, but regarding this question,

"
  • Two file operations often overlap, so methodize to improve visibility
  • The line is a little special, so you can use it a little simpler from the outside by classifying it.

I think there are things like that.
If you class the lines, for example, I think it will look like this.


2022-09-30 19:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.