Results 1 to 9 of 9
- 02-26-2012, 03:19 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 31
- Rep Power
- 0
Reading file full of numbers, cannot add them together
I have my code below. It uses a file called scores.txt that is also attached below
This is supposed to be a program that interprets this all as golf scores, with the first number on each line being the par, and the remaining 4 numbers being the scores of each golfer. So far, I have successfully made a program that can read each line, however the issue I am having, is that I am attempting to add up each golfer's score. It is not doing so, even though I have a line that is supposed to be totaling up each score.
When runs, it displays first a string containing an entire line, and then the score of golfer one. It repeats this for all 18 lines, with each line representing a hole of golf.
What it is supposed to be doing however, is adding up the golfers score from each hole in the second display item. Instead of displaying the score from the current hole as it is, it should be adding up the scores so that it displays golfer one's current total score.
I would really appreciate any help, thanks.
Lab_7.java
Java Code:import java.util.Scanner; import java.io.*; public class Lab_7 { //reads each line and prints it public static void main (String[] args) throws IOException { String line; Scanner lineScan, scoreScan; lineScan = new Scanner (new File("scores.txt")); // Read and process each line of the file while (lineScan.hasNext()) { line = lineScan.nextLine(); System.out.println ("Score: " + line); scoreScan = new Scanner (line); scoreScan.useDelimiter(","); int par, one, two, three, four; while (scoreScan.hasNext()) { par = Integer.parseInt(scoreScan.next()); one = Integer.parseInt(scoreScan.next()); two = Integer.parseInt(scoreScan.next()); three = Integer.parseInt(scoreScan.next()); four = Integer.parseInt(scoreScan.next()); int One=+one; int Two=+two; int Three=+three; int Four=+four; System.out.println (One); System.out.println (Two); System.out.println (Three); System.out.println (Four); } } System.out.println(); } }
Java Code:4,2,6,8,3 5,5,8,9,1 6,9,8,9,4 4,4,5,6,9 5,9,9,9,9 6,8,8,8,8 4,8,7,8,9 5,9,8,4,9 6,2,4,8,9 4,4,9,1,4 5,1,3,9,6 6,3,5,6,9 4,7,6,5,9 5,9,7,5,4 6,5,5,3,7 4,7,8,9,8 5,9,9,8,5 6,5,6,9,9
- 02-26-2012, 04:06 AM #2
Re: Reading file full of numbers, cannot add them together
If you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 02-26-2012, 04:08 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 17
Re: Reading file full of numbers, cannot add them together
I think you might be confusing += with =+. They mean totally different things. Look them up and make sure you understand the difference.
- 02-26-2012, 04:08 AM #4
Re: Reading file full of numbers, cannot add them together
And when you get your operators sorted out, think about where your variables are declared.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 02-26-2012, 04:11 AM #5
Member
- Join Date
- Feb 2012
- Posts
- 31
- Rep Power
- 0
Re: Reading file full of numbers, cannot add them together
As I understand it, you are saying that =+ does not mean "take the variable and add to itself the following number"
I tried using += and my compiler freaks out, so that is out...
so I modified the code to read
Java Code:int One = 0; One=One+one;
Last edited by quinnvanorder; 02-26-2012 at 04:12 AM. Reason: more info
- 02-26-2012, 04:16 AM #6
Member
- Join Date
- Feb 2012
- Posts
- 31
- Rep Power
- 0
Re: Reading file full of numbers, cannot add them together
Ok i have managed to make it least compile with +=, and i moved my variables a bit.. here is my new code, but I get the same result
Java Code:import java.util.Scanner; import java.io.*; public class Lab_7 { //reads each line and prints it public static void main (String[] args) throws IOException { String line; Scanner lineScan, scoreScan; lineScan = new Scanner (new File("scores.txt")); // Read and process each line of the file while (lineScan.hasNext()) { line = lineScan.nextLine(); System.out.println ("Score: " + line); scoreScan = new Scanner (line); scoreScan.useDelimiter(","); while (scoreScan.hasNext()) { int par, one, two, three, four; par = Integer.parseInt(scoreScan.next()); one = Integer.parseInt(scoreScan.next()); two = Integer.parseInt(scoreScan.next()); three = Integer.parseInt(scoreScan.next()); four = Integer.parseInt(scoreScan.next()); int One=0, Two=0, Three=0, Four=0; One+=one; Two+=two; Three+=three; Four+=four; System.out.println (One); // System.out.println (Two); // System.out.println (Three); // System.out.println (Four); } } System.out.println(); } }
- 02-26-2012, 04:18 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 17
Re: Reading file full of numbers, cannot add them together
I tried using += and my compiler freaks out, so that is out...
You will have got something along the lines of "Expected a =" because when you declare a variable you can assign something to it, but not with +=. (The reason should be obvious: since += means something like "take the variable and add to itself the following number" as you say, you can't add something to it because it does not yet have a value.)
Assigning zero to the variable is no good as that will reset the total. So, as Darryl said, think about where it should be declared, given that you only declare it once. And, for the love of Coding Standards, call it totalOne instead of One.
- 02-26-2012, 04:23 AM #8
Member
- Join Date
- Feb 2012
- Posts
- 31
- Rep Power
- 0
Re: Reading file full of numbers, cannot add them together
Ok, I think I understand now, it works with the following code
Java Code:import java.util.Scanner; import java.io.*; public class Lab_7 { //reads each line and prints it public static void main (String[] args) throws IOException { String line; Scanner lineScan, scoreScan; int totalOne=0, totalTwo=0, totalThree=0, totalFour=0; lineScan = new Scanner (new File("scores.txt")); // Read and process each line of the file while (lineScan.hasNext()) { line = lineScan.nextLine(); System.out.println ("Score: " + line); scoreScan = new Scanner (line); scoreScan.useDelimiter(","); while (scoreScan.hasNext()) { int par, one, two, three, four; par = Integer.parseInt(scoreScan.next()); one = Integer.parseInt(scoreScan.next()); totalOne+=one; two = Integer.parseInt(scoreScan.next()); totalTwo+=two; three = Integer.parseInt(scoreScan.next()); totalThree+=three; four = Integer.parseInt(scoreScan.next()); totalFour+=four; System.out.println (totalOne); // System.out.println (Two); // System.out.println (Three); // System.out.println (Four); } } System.out.println(); } }
- 02-26-2012, 04:28 AM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 17
Similar Threads
-
Reading numbers
By edprog in forum New To JavaReplies: 10Last Post: 04-16-2011, 08:17 AM -
Reading strings from the user but dont want to accept numbers ,!
By javanew in forum New To JavaReplies: 1Last Post: 09-24-2010, 08:08 PM -
Specify full location of txt file
By Dekkon0 in forum New To JavaReplies: 4Last Post: 05-15-2010, 06:17 AM -
can i remove a file from a input? can provided a full code for me?
By reeveliew in forum New To JavaReplies: 3Last Post: 05-06-2010, 08:10 AM -
how to get the full file name
By priyanka3006 in forum JDBCReplies: 0Last Post: 08-05-2009, 01:55 PM
Bookmarks