4

This program is to read from a file. I would like to print out the code and not the address location. What should be added to my program?

        try {
            FileInputStream fileReader = new FileInputStream("Results.txt");
            BufferedReader reader = new BufferedReader(new InputStreamReader(fileReader));
            ArrayList<Result> myList = new ArrayList<>();
            String line = reader.readLine();

            String strLine;
            String[] lines = line
                    .replace("[", "")
                    .replace("]", "")
                    .split(",");
            int flag=1;
            while ((strLine = reader.readLine()) != null) {
                if (flag == 1) {
                    for (String l : lines) {
                        System.out.println(l.trim());
                    }
                }
                System.out.println();
                System.out.println(strLine
                        .replace("[", "")
                        .replace("]", "")
                        .split(","));

                flag++;
            }

        }
        catch (Exception e) {
            e.printStackTrace();
        }

Below are the output of my program. It shows that there are 2 address location that have been printed out.I would like to print the codes not the location

< STIA1113 | Science | Credit Hours: 3 | Grade: A >
< SADN1033 | Math | Credit Hours: 3 | Grade: B+ >
< BWFF1013 | Finance | Credit Hours: 3 | Grade: A- >

[Ljava.lang.String;@45ff54e6

[Ljava.lang.String;@2328c243
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Elbert Jn
  • 195
  • 7
  • You should use `Arrays.toString` for the string arrays retrieved after string `split` method -- then you'll see the contents of the arrays. – Nowhere Man Jul 01 '20 at 23:00

1 Answers1

1

Use java.util.Arrays.toString to print the elements of a one-dimensional array.

System.out.println(Arrays.toString(strLine.split(",")).replace("[", "").replace("]", "").replace(",", ""));
Unmitigated
  • 76,500
  • 11
  • 62
  • 80