Home > OS >  How do I read data from a csv file, store it to memory, then write the memory back into the csv file
How do I read data from a csv file, store it to memory, then write the memory back into the csv file

Time:09-25

My code uses a buffered reader to read a file with a list of values, and my FileWriter adds another gamer to the list. Whenever I go to run my code, the new gamer is added repeatedly. How do I read the data into memory and then write the memory back into the file without the last line added?

Input:

package week3assignment;

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

public class Jaret_Dalrymple_GamesReport {

    public static void main(String[] args) throws IOException {
        File gameScores = new File("C:\\Users\\jaret\\OneDrive\\Desktop\\GameScores.csv");
        readWrite(gameScores);
        addGamer(gameScores, "Jimmy", "189", "190", "197", "199", "198", "193", "199", "199", "188", "196");
        readWrite2(gameScores);
    }

    private static void readWrite(File gameScores) throws IOException {

        if (gameScores.exists()) {
            BufferedReader br = null;
            String line = "";
            String csvSplitBy = ",";
            int gamerCount = 0;
            int topScore = 0;
            String topScorer = "";
            br = new BufferedReader(new FileReader(gameScores));
            System.out.println("----------------------------------------------------------");
            System.out.println("-------------------------------------");
            System.out.println("Games Report");
            System.out.println("----------------------------------------------------------");
            System.out.println("-------------------------------------");
            System.out.println("Gamer   1   2   3   4   5   6   7");
            System.out.println("    8   9   10  Total");
            System.out.println("----------------------------------------------------------");
            System.out.println("-------------------------------------");

            while ((line = br.readLine()) != null) {
                String list[] = new String[10];
                list = line.split(csvSplitBy);
                int sum = 0;

                for (String element : list) {
                    try {
                        Integer num = Integer.parseInt(element);
                        sum  = num;
                    } catch (NumberFormatException nfe) {
                        System.out.print(list[0]   "\t"   list[1]   (list[1].length() > 10 ? "\t" : "\t")   list[2]
                                  (list[2].length() > 10 ? "\t" : "\t")   list[3]
                                  (list[3].length() > 10 ? "\t" : "\t")   list[4]
                                  (list[4].length() > 10 ? "\t" : "\t")   list[5]
                                  (list[5].length() > 10 ? "\t" : "\t")   list[6]
                                  (list[6].length() > 10 ? "\t" : "\t")   list[7]
                                  (list[7].length() > 10 ? "\n\t" : "\n\t")   list[8]
                                  (list[8].length() > 10 ? "\t" : "\t")   list[9]
                                  (list[9].length() > 10 ? "\t" : "\t")   list[10]   "\t");
                    }
                }
                if (sum > topScore) {
                    topScore = sum;
                    topScorer = list[0];
                }

                System.out.println(sum);
                gamerCount  ;
            }

            System.out.println("----------------------------------------------------------");
            System.out.println("-------------------------------------");
            System.out.println("# of Gamers:    "   gamerCount);
            System.out.println("Top Gamer:  "   topScorer);
            System.out.println("----------------------------------------------------------");
            System.out.println("-------------------------------------");
            br.close();
        }
    }

    private static void addGamer(File gameScores, String first, String second, String third, String fourth,
            String fifth, String sixth, String seventh, String eighth, String ninth, String tenth, String eleventh)
            throws IOException {
        FileWriter newGamer = new FileWriter(gameScores, true);
        newGamer.write("\n"   first   ","   second   ","   third   ","   fourth   ","   fifth   ","   sixth   ","   
        seventh   ","   eighth   ","   ninth   ","   tenth   ","   eleventh);
        newGamer.close();
        System.out.println("Gamer Jimmy record added.");;
    }

    private static void readWrite2(File gameScores) throws IOException {

        if (gameScores.exists()) {
            BufferedReader br = null;
            String line = "";
            String csvSplitBy = ",";
            int gamerCount = 0;
            int topScore = 0;
            String topScorer = "";
            FileWriter fw = new FileWriter(gameScores, true);
            br = new BufferedReader(new FileReader(gameScores));
            System.out.println("----------------------------------------------------------");
            System.out.println("-------------------------------------");
            System.out.println("Games Report");
            System.out.println("----------------------------------------------------------");
            System.out.println("-------------------------------------");
            System.out.println("Gamer   1   2   3   4   5   6   7");
            System.out.println("    8   9   10  Total");
            System.out.println("----------------------------------------------------------");
            System.out.println("-------------------------------------");

            while ((line = br.readLine()) != null) {
                String list[] = new String[10];
                list = line.split(csvSplitBy);
                int sum = 0;

                for (String element : list) {
                    try {
                        Integer num = Integer.parseInt(element);
                        sum  = num;
                    } catch (NumberFormatException nfe) {
                        System.out.print(list[0]   "\t"   list[1]   (list[1].length() > 10 ? "\t" : "\t")   list[2]
                                  (list[2].length() > 10 ? "\t" : "\t")   list[3]
                                  (list[3].length() > 10 ? "\t" : "\t")   list[4]
                                  (list[4].length() > 10 ? "\t" : "\t")   list[5]
                                  (list[5].length() > 10 ? "\t" : "\t")   list[6]
                                  (list[6].length() > 10 ? "\t" : "\t")   list[7]
                                  (list[7].length() > 10 ? "\n\t" : "\n\t")   list[8]
                                  (list[8].length() > 10 ? "\t" : "\t")   list[9]
                                  (list[9].length() > 10 ? "\t" : "\t")   list[10]   "\t");
                    }
                }
                if (sum > topScore) {
                    topScore = sum;
                    topScorer = list[0];
                }

                System.out.println(sum);
                gamerCount  ;
            }

            System.out.println("----------------------------------------------------------");
            System.out.println("-------------------------------------");
            System.out.println("# of Gamers:    "   gamerCount);
            System.out.println("Top Gamer:  "   topScorer);
            
            br.close();
        }
    }
}

Ouput:

----------------------------------------------------------
-------------------------------------
Games Report
----------------------------------------------------------
-------------------------------------
Gamer   1   2   3   4   5   6   7
    8   9   10  Total
----------------------------------------------------------
-------------------------------------
Bob 167 123 159 102 102 189 183
    173 197 148 1543
Sally   189 130 138 113 159 116 134
    196 150 144 1469
Mario   104 106 120 188 143 189 149
    174 163 100 1436
Lev 152 159 195 140 154 176 107
    128 166 181 1558
Carden  158 200 175 114 117 150 176
    181 131 132 1534
Adelie  175 199 122 104 198 182 175
    153 120 165 1593
Lada    161 108 102 193 151 197 115
    137 126 186 1476
Xavier  178 171 147 113 107 129 128
    189 165 195 1522
Raffi   176 144 151 124 149 112 158
    159 119 177 1469
Chang   135 144 177 153 143 125 145
    140 117 158 1437
Mich    156 105 178 137 165 180 128
    115 139 157 1460
Mason   162 185 108 106 113 135 139
    135 197 160 1440
Cora    186 115 106 126 135 108 157
    156 187 120 1396
Sergio  117 105 115 116 193 200 176
    134 122 153 1431
Jonas   132 163 196 101 134 159 131
    104 135 168 1423
Jimmy   189 190 197 199 198 193 199
    199 188 196 1948
----------------------------------------------------------
-------------------------------------
# of Gamers:    16
Top Gamer:  Jimmy
----------------------------------------------------------
-------------------------------------
Gamer Jimmy record added.
----------------------------------------------------------
-------------------------------------
Games Report
----------------------------------------------------------
-------------------------------------
Gamer   1   2   3   4   5   6   7
    8   9   10  Total
----------------------------------------------------------
-------------------------------------
Bob 167 123 159 102 102 189 183
    173 197 148 1543
Sally   189 130 138 113 159 116 134
    196 150 144 1469
Mario   104 106 120 188 143 189 149
    174 163 100 1436
Lev 152 159 195 140 154 176 107
    128 166 181 1558
Carden  158 200 175 114 117 150 176
    181 131 132 1534
Adelie  175 199 122 104 198 182 175
    153 120 165 1593
Lada    161 108 102 193 151 197 115
    137 126 186 1476
Xavier  178 171 147 113 107 129 128
    189 165 195 1522
Raffi   176 144 151 124 149 112 158
    159 119 177 1469
Chang   135 144 177 153 143 125 145
    140 117 158 1437
Mich    156 105 178 137 165 180 128
    115 139 157 1460
Mason   162 185 108 106 113 135 139
    135 197 160 1440
Cora    186 115 106 126 135 108 157
    156 187 120 1396
Sergio  117 105 115 116 193 200 176
    134 122 153 1431
Jonas   132 163 196 101 134 159 131
    104 135 168 1423
Jimmy   189 190 197 199 198 193 199
    199 188 196 1948
Jimmy   189 190 197 199 198 193 199
    199 188 196 1948
----------------------------------------------------------
-------------------------------------
# of Gamers:    17
Top Gamer:  Jimmy

CodePudding user response:

When you have a while loop with this condition: line = br.readLine() != null You could change it to something like this:

line = br.readLine();
while(line != null){
   your code here...
} // end loop

You could make a temporary variable to store the line you're using on the loop, and in case the code exits finally the loop, get that line and delete it from the csv.

PS: Sorry if I didn't explain myself as good as I would like, just starting on this. Good luck!

CodePudding user response:

A most basic example of writing data from an ArrayList:

public static void main(String[] args) throws Exception
{
    ArrayList<String> lines = new ArrayList<>();
    lines.add("one");
    lines.add("two");
    lines.add("three");

    File file = new File("testing.txt");
    BufferedWriter writer = new BufferedWriter( new FileWriter( file ) );

    for (int i = 0; i < lines.size(); i  )
    {
        if (i > 0)
            writer.newLine();

        writer.write( lines.get(i) );
    }

    writer.close();
}

So you first need to read your data into the ArrayList and delete the last item before writing out the data in the ArrayList.

  • Related