Reputation: 21
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();
}
Upvotes: 0
Views: 502
Reputation: 221
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.
Upvotes: 1
Reputation: 4582
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);
}
Upvotes: 1