Home > Blockchain >  How do I find the sum of integers stored in a string array along with a string in java? I need the 0
How do I find the sum of integers stored in a string array along with a string in java? I need the 0

Time:09-23

Input: public static void main(String[] args) throws IOException{

    File gameScores = new File("C:\\Users\\jaret\\OneDrive\\Desktop\\GameScores.csv"); 
    if (gameScores.exists()) 
    {
        BufferedReader br = null;
        String line = "";
        String csvSplitBy = ",";
        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.println(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"   sum);
                }
            }
        }
        System.out.println("----------------------------------------------------------");
        System.out.println("----------------------------------------------------------");
        br.close();
    }


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 0 Sally 189 130 138 113 159 116 134 196 150 144 0 Mario 104 106 120 188 143 189 149 174 163 100 0 Lev 152 159 195 140 154 176 107 128 166 181 0 Carden 158 200 175 114 117 150 176 181 131 132 0 Adelie 175 199 122 104 198 182 175 153 120 165 0 Lada 161 108 102 193 151 197 115 137 126 186 0 Xavier 178 171 147 113 107 129 128 189 165 195 0 Raffi 176 144 151 124 149 112 158 159 119 177 0 Chang 135 144 177 153 143 125 145 140 117 158 0 Mich 156 105 178 137 165 180 128 115 139 157 0 Mason 162 185 108 106 113 135 139 135 197 160 0 Cora 186 115 106 126 135 108 157 156 187 120 0 Sergio 117 105 115 116 193 200 176 134 122 153 0 Jonas 132 163 196 101 134 159 131 104 135 168 0


CodePudding user response:

In your code the first element in list is the name, which is not a number and you are getting NumberFormatException for that. And at that instance sum is 0 and catch is printing 0 for you as the sum.

If you want to print right sum, you should remove printing sum in catch and place it outside the for loop. As below:

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.println(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");
                }
            }
            System.out.println(sum)
        }

Although I don't think it's a good Idea to handle it this way to print values in catch exception block.

Better solution could be something like below, where you iterate over list from index 1 to the end. As first index element is name. This way you will avoid unwanted Exception handling. And print outside the for loop.

Also removed that unwanted turnery operator.

while ((line = br.readLine()) != null) 
            {
                String list[] = new String[10];
                list = line.split(csvSplitBy);
                int sum = 0;
                for(int i =1; i< list.length; i  ) {
                    Integer num = Integer.parseInt(list[i]);
                    sum  = num;
                }

                 System.out.println(list[0]   "\t" 
                                  list[1]   "\t"
                                  list[2]   "\t" 
                                  list[3]   "\t"
                                  list[4]   "\t"
                                  list[5]   "\t"
                                  list[6]   "\t"
                                  list[7]   "\t"
                                  list[8]   "\t"
                                  list[9]   "\t"
                                  list[10]   "\t"   sum);
            }

CodePudding user response:

If java 8 is fine, then you could use a more up-to-date approach:

        final Path path = Paths.get("path/to/your/file");
        final List<String> lines = Files.readAllLines(path);

        int sum = 0;

        try {
            sum = lines.stream()
                    .map(line -> line.split(","))
                    .flatMap(Arrays::stream)
                    .mapToInt(Integer::parseInt)
                    .sum();
        } catch (Exception ex) {
            // do something with exception
        }

        lines.forEach(System.out::println);
        System.out.println(sum);

If the file is large, then play with Files.lines(path) method.

  • Related