[JAVA] Variable might not have been initialized?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Crashfan3
    FFR Player
    • Nov 2006
    • 2937

    #1

    [JAVA] Variable might not have been initialized?

    Hi,

    I'm trying to write a program that reads a list of golf scores from a text file, and then stores each golfer's score to memory.
    The scores are in the text file like this:
    3 4 3 2 5 (18 lines like this, where the first number is par, the others are each golfer's score)
    My program reads each hole just fine but I can't figure out how to properly store each golfer's score and add them up to get a final score so that I can determine who wins.

    My loop looks like this:
    Code:
    // Print each golfer's score individiually
    while (holeScan.hasNext())
    {
    	int par;
    	int g1score, g2score, g3score, g4score;
    	System.out.println("PAR    : " + holeScan.next());
    	par = Integer.parseInt(holeScan.next());
    	System.out.println("Golfer1: " + holeScan.next());
    	g1score = g1score + Integer.parseInt(holeScan.next());
    	System.out.println("Golfer2: " + holeScan.next());
    	g2score = g2score + Integer.parseInt(holeScan.next());
    	System.out.println("Golfer3: " + holeScan.next());
    	g3score = g3score + Integer.parseInt(holeScan.next());
    	System.out.println("Golfer4: " + holeScan.next());
    	g4score = g4score + Integer.parseInt(holeScan.next());
    }
    When I compile, I'm being told that each of the variables above "might not have been initialized." When I set them all equal to zero, I get a short set of incorrect results followed by an exception.


    I'm not begging for someone to write my code for me, but I can't get my head around this little piece so I thought I'd come here for help since this is due tomorrow morning.

    If you need the rest of my code, just ask.
    Thanks!!
  • rushyrulz
    Digital Dancing!
    FFR Simfile Author
    FFR Music Producer
    • Feb 2006
    • 12985

    #2
    Re: [JAVA] Variable might not have been initialized?

    I'm no expert, but have you considered storing each set of information in a separate array and looping through them to obtain the finals?


    Comment

    • Crashfan3
      FFR Player
      • Nov 2006
      • 2937

      #3
      Re: [JAVA] Variable might not have been initialized?

      We haven't covered arrays yet (I think that's next week actually), so I don't know how to do that, and it's probably not how the instructor wants me to write the code.

      Comment

      • rushyrulz
        Digital Dancing!
        FFR Simfile Author
        FFR Music Producer
        • Feb 2006
        • 12985

        #4
        Re: [JAVA] Variable might not have been initialized?

        you're reading text files before you've started arrays? what in the ****? I guess you could just use separate while loops for each player and total the scores as you go along

        should be like, while hole <=18, get par and scores, add current par and scores to totalpar and totalscore variables and you should be able to print that out in the end.
        Last edited by rushyrulz; 02-28-2012, 03:57 PM.


        Comment

        • cry4eternity
          ~ added for cuteness
          FFR Simfile Author
          • Jan 2007
          • 979

          #5
          Re: [JAVA] Variable might not have been initialized?

          Code:
          System.out.println("PAR    : " + holeScan.next());
          This line reads in the first value, "3", and prints it.

          Code:
          par = Integer.parseInt(holeScan.next());
          This line reads in the next value, "4", and stores it in par.

          Code:
          System.out.println("Golfer1: " + holeScan.next());
          This line prints the next value, "3".

          Code:
          g1score = g1score + Integer.parseInt(holeScan.next());
          This line reads the next value, "2", and adds it to the uninitialized variable, g1score. This line and others like it is where your warnings come from.

          Code:
          System.out.println("Golfer2: " + holeScan.next());
          This line reads the next value, "5" and prints it.

          Code:
          g2score = g2score + Integer.parseInt(holeScan.next());
          Assuming this scanner is only reading a single line, there is no next token and the scanner throws an error.

          From this, you can probably see what you're doing wrong. When you call the scanner's next() method, it moves the current pointer forward in the file to the next token. Don't try to read the variable twice from the file. Do either one of these:

          Code:
          gxscore = Integer.parseInt(holeScan.next());
          System.out.println("Golferx: " + gxscore);
          Code:
          System.out.println("Golferx: " + holeScan.next());

          I'm retired

          Comment

          • megamon88
            FFR Simfile Author
            FFR Simfile Author
            FFR Music Producer
            • Feb 2006
            • 2567

            #6
            Re: [JAVA] Variable might not have been initialized?

            Also, make sure you initialize your variables first (like you said, set them to 0 in your declaration). When you do g1score = g1score + Integer.parseInt(holeScan.next());, then g1 score already has to have a value since it is being referenced, even though it may be 0 at the time. The reason you were getting the wrong total/errors was because of what cry4eternity posted above.

            Comment

            • trumaestro
              I don't get no respect
              FFR Simfile Author
              • Jun 2006
              • 1332

              #7
              Re: [JAVA] Variable might not have been initialized?

              Originally posted by megamon88
              Also, make sure you initialize your variables first (like you said, set them to 0 in your declaration).
              And ya gotta do this outside of your loop or else you're just redefining the variables every time the loop executes

              Comment

              • megamon88
                FFR Simfile Author
                FFR Simfile Author
                FFR Music Producer
                • Feb 2006
                • 2567

                #8
                Re: [JAVA] Variable might not have been initialized?

                Oh yeah, forgot to mention that :P

                Comment

                • Crashfan3
                  FFR Player
                  • Nov 2006
                  • 2937

                  #9
                  Re: [JAVA] Variable might not have been initialized?

                  Posting from CS161 class. My code met the expectations. Thanks guys! ^^

                  Comment

                  Working...