Results 1 to 9 of 9
  1. #1
    quinnvanorder is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default 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();
    }
    }
    scores.txt
    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

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default 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.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    quinnvanorder is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default 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;
    and it does the same thing...
    Last edited by quinnvanorder; 02-26-2012 at 03:12 AM. Reason: more info

  6. #6
    quinnvanorder is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default 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();
    }
    }

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Reading file full of numbers, cannot add them together

    I tried using += and my compiler freaks out, so that is out...
    The compiler is your friend. Listen to its messages.

    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.

  8. #8
    quinnvanorder is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default 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();
    }
    }
    Thanks!

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Reading file full of numbers, cannot add them together

    Good work!

Similar Threads

  1. Reading numbers
    By edprog in forum New To Java
    Replies: 10
    Last Post: 04-16-2011, 07:17 AM
  2. Replies: 1
    Last Post: 09-24-2010, 07:08 PM
  3. Specify full location of txt file
    By Dekkon0 in forum New To Java
    Replies: 4
    Last Post: 05-15-2010, 05:17 AM
  4. Replies: 3
    Last Post: 05-06-2010, 07:10 AM
  5. how to get the full file name
    By priyanka3006 in forum JDBC
    Replies: 0
    Last Post: 08-05-2009, 12:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •