Question:
I have imported a text file into my program that contains several scores from a gamer. I am in need of adding the scores from each gamer – while attempting to do so, I realized that those scores are coming from a text file, therefore, they are actually strings, not numbers. I am wanting to convert the string number into an integer but have not been able to do so by using the parseInt() method, I have attempted to use the following method in my code:String myString = "1234";
int foo = Integer.parseInt(myString);
But have not been able to do it successfully. Here is my current work:
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.BufferedReader;
public class luis_ramirez_GamesReport {
private static String gamer;
public static void main(String[] args) throws IOException
{
File fileName = new File("/Users/luisramirez/eclipse-workspace/GameScores.txt");
if (fileName.exists())
{
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
int recordCount = 0;
String number ="167";
int result = Integer.parseInt(number);
br = new BufferedReader(new FileReader(fileName));
System.out.println("-----------------------------------------------------------------------------------------------");
System.out.println("Games Report");
System.out.println("-----------------------------------------------------------------------------------------------");
System.out.println("Gamer 1 2 3 4 5 6 7 8 9 10 Total");
System.out.println("-----------------------------------------------------------------------------------------------");
while ((line = br.readLine()) != null)
{
String[] record = line.split(cvsSplitBy);
System.out.println(record[0] + "\t"
+ record[1] + (record[2].length() > 7 ? "\t" : "\t")
+ record[2] + (record[2].length() > 7 ? "\t" : "\t")
+ record[3] + (record[3].length() > 7 ? "\t" : "\t")
+ record[4] + (record[4].length() > 7 ? "\t" : "\t")
+ record[5] + (record[5].length() > 7 ? "\t" : "\t")
+ record[6] + (record[6].length() > 7 ? "\t" : "\t")
+ record[7] + (record[7].length() > 7 ? "\t" : "\t")
+ record[8] + (record[8].length() > 7 ? "\t" : "\t")
+ record[9] + (record[9].length() > 7 ? "\t" : "\t")
+ record[10] + (record[10].length() > 7 ? "\t" : "\t")
+ record[1] + record[2] + record[3] + record[4] + record[5] + record[6] + record[7] + record[8] + record[9] + record[10]);
recordCount++;
}
System.out.println("----------------------------------------------------------------------------------------------");
System.out.printf("# of Gamers: %d%n",recordCount);
System.out.println("Top Gamer: Adelie");
System.out.println("----------------------------------------------------------------------------------------------");
br.close();
}
This is my output:-----------------------------------------------------------------------------------------------
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 167123159102102189183173197148
Sally 189 130 138 113 159 116 134 196 150 144 189130138113159116134196150144
Mario 104 106 120 188 143 189 149 174 163 100 104106120188143189149174163100
Lev 152 159 195 140 154 176 107 128 166 181 152159195140154176107128166181
Carden 158 200 175 114 117 150 176 181 131 132 158200175114117150176181131132
Adelie 175 199 122 104 198 182 175 153 120 165 175199122104198182175153120165
Lada 161 108 102 193 151 197 115 137 126 186 161108102193151197115137126186
Xavier 178 171 147 113 107 129 128 189 165 195 178171147113107129128189165195
Raffi 176 144 151 124 149 112 158 159 119 177 176144151124149112158159119177
Chang 135 144 177 153 143 125 145 140 117 158 135144177153143125145140117158
Mich 156 105 178 137 165 180 128 115 139 157 156105178137165180128115139157
Mason 162 185 108 106 113 135 139 135 197 160 162185108106113135139135197160
Cora 186 115 106 126 135 108 157 156 187 120 186115106126135108157156187120
Sergio 117 105 115 116 193 200 176 134 122 153 117105115116193200176134122153
Jonas 132 163 196 101 134 159 131 104 135 168 132163196101134159131104135168
----------------------------------------------------------------------------------------------
# of Gamers: 15
Top Gamer: Adelie
----------------------------------------------------------------------------------------------
Please note that the output of my scores appears correctly when the program is ran.In addition, here is the information from the text file:
Bob,167,123,159,102,102,189,183,173,197,148 Sally,189,130,138,113,159,116,134,196,150,144 Mario,104,106,120,188,143,189,149,174,163,100 Lev,152,159,195,140,154,176,107,128,166,181 Carden,158,200,175,114,117,150,176,181,131,132 Adelie,175,199,122,104,198,182,175,153,120,165 Lada,161,108,102,193,151,197,115,137,126,186 Xavier,178,171,147,113,107,129,128,189,165,195 Raffi,176,144,151,124,149,112,158,159,119,177 Chang,135,144,177,153,143,125,145,140,117,158 Mich,156,105,178,137,165,180,128,115,139,157 Mason,162,185,108,106,113,135,139,135,197,160 Cora,186,115,106,126,135,108,157,156,187,120 Sergio,117,105,115,116,193,200,176,134,122,153 Jonas,132,163,196,101,134,159,131,104,135,168
Any insight you can kindly provide would be greatly appreciated.
Answer:
I’m assuming from your original code that you want to parseInt because you want the final column to display the sum properly.To accomplish this, I remove the first element from your String array record (since this is the name of the person), and then I use Java’s Streams API in order to convert each element into an integer and get the sum. There was also some whitespace around some of the integers that were Strings, so I stripped the whitespace before using parseInt.
Here is my implementation:
package com.sandbox;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class ScoresDisplay {
public static void main(String[] args) throws IOException
{
File fileName = new File("path/to/scores.txt");
if (fileName.exists())
{
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
int recordCount = 0;
String number ="167";
int result = Integer.parseInt(number);
br = new BufferedReader(new FileReader(fileName));
System.out.println("-----------------------------------------------------------------------------------------------");
System.out.println("Games Report");
System.out.println("-----------------------------------------------------------------------------------------------");
System.out.println("Gamer 1 2 3 4 5 6 7 8 9 10 Total");
System.out.println("-----------------------------------------------------------------------------------------------");
while ((line = br.readLine()) != null)
{
String[] record = line.split(cvsSplitBy);
String[] ints_only = Arrays.copyOfRange(record, 1, record.length);
List<Integer> recordAsInts = Arrays.stream(ints_only)
.map(str -> str.strip())
.map(Integer::parseInt)
.collect(Collectors.toList());
int sum = recordAsInts.stream().reduce(Integer::sum).orElse(0);
System.out.println(record[0] + "\t"
+ record[1] + (record[2].length() > 7 ? "\t" : "\t")
+ record[2] + (record[2].length() > 7 ? "\t" : "\t")
+ record[3] + (record[3].length() > 7 ? "\t" : "\t")
+ record[4] + (record[4].length() > 7 ? "\t" : "\t")
+ record[5] + (record[5].length() > 7 ? "\t" : "\t")
+ record[6] + (record[6].length() > 7 ? "\t" : "\t")
+ record[7] + (record[7].length() > 7 ? "\t" : "\t")
+ record[8] + (record[8].length() > 7 ? "\t" : "\t")
+ record[9] + (record[9].length() > 7 ? "\t" : "\t")
+ record[10] + (record[10].length() > 7 ? "\t" : "\t")
+ sum);
recordCount++;
}
System.out.println("----------------------------------------------------------------------------------------------");
System.out.printf("# of Gamers: %d%n",recordCount);
System.out.println("Top Gamer: Adelie");
System.out.println("----------------------------------------------------------------------------------------------");
br.close();
}
}
}
Output:I’m using a picture instead here because the formatting looks weird in text.

If you have better answer, please add a comment about this, thank you!
Source: Stackoverflow.com
Leave a Review