ACSL Golf Code errors

  • (7 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • Last »

102 Replies - 2636 Views - Last Post: 02 October 2011 - 06:36 PM Rate Topic: -----

#16 bhsanchez72  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 01-October 11

Re: ACSL Golf Code errors

Posted 01 October 2011 - 10:28 AM

View PostGregBrannon, on 01 October 2011 - 10:13 AM, said:

Well, you're going to have to expand your knowledge to get 3 ints from the single line of input. There are many ways to do it, but I think the most direct, staying within what you've learned, is to use Scanner's useDelimiter() method.

If you change line 18 to:

Scanner kbd = new Scanner(System.in).useDelimiter( ",|\n" );

That means that you want the input kbd broken or split at the characters ',' or '\n' (new line). That way, the string that kbd gets, e.g. "x,y,z", will be interpreted as 3 items, x, y, and z.

Then change lines 29, 30, and 31 (30 and 31 are new) to:
parValue = kbd.nextInt();
playerA = kbd.nextInt();
playerB = kbd.nextInt();

Those 3 lines get the values x, y, and z entered by the user and stores them as parValue, playerA, and playerB, respectively. I guessed that those were the right variables to use, so change them if I got it wrong.

So now you should be able to get 4 lines of input based on your for loop at line 20. You need to get 9 lines of input. Fix that next.

ooo okay i understand. I add the two things. Here is what it looks like now:

/*
 * Bradley Sanchez
 * Client that obtains data for use by the golfer
 * 9/26/11
 * PD:4B CS2
 */

import java.util.Scanner;
public class ACSLGolfClient {

	
	public static void main(String[] args) 
	{
		//declare variables
		GolfScore myGolfScore;
		int parValue = 0, parTotal = 0, playerA = 0, playerB = 0, totalA = 0, totalB = 0, counter = 0, highScoreA, highScoreB, highScore, aHolesWon = 0, bHolesWon = 0;
		myGolfScore = new GolfScore();
		Scanner kbd = new Scanner(System.in).useDelimiter( ",|\n" );
		//process the data
		for(int holes = 1; holes <= 4; holes++)
		{
			counter++;
			parTotal += parValue;
			totalA += playerA;
			totalB += playerB;
			
			//obtained data
			System.out.println("Please enter par, scoreA, scoreB --> ");
			parValue = kbd.nextInt();
			playerA = kbd.nextInt();
			playerB = kbd.nextInt();
			
			
			//access the class with the data
			myGolfScore.tallyPar(aHolesWon);
			myGolfScore.tallyScores(bHolesWon);
			
			
			
			//output results
			System.out.println(myGolfScore.outcome(aHolesWon, bHolesWon));
			System.out.println(totalA);
			
		}
		totalA = myGolfScore.getScore() - myGolfScore.getpar();
		if(playerA < playerB)
		{
			aHolesWon ++;
		}
		else
		{
			bHolesWon ++;
		}
	
		if(totalA < totalB)
		{
			System.out.println(totalA + " , " + totalB);
		}
		else
		{
			System.out.println(totalB + " , " + totalA);
		}
		
		highScoreA = parTotal - totalA;
		highScoreB = parTotal - totalB;
		
		if (highScoreA >= 0)
		{
			System.out.println(Math.abs(highScoreA) + " under par");
		}
		else
		{
			System.out.println(Math.abs(highScoreA) + " over par");
		}
		
		if (highScoreB < 0)
		{
			System.out.println(Math.abs(highScoreB) + " over par");
		}
		else
		{
			System.out.println(Math.abs(highScoreB) + " under par");
		}
		
		if(aHolesWon > bHolesWon)
		{
			System.out.println(aHolesWon);
		}
		else
		{
			System.out.println(bHolesWon);
		}
}
		
	}


this is what i get when i run it:
Please enter par, scoreA, scoreB --> 
3
Exception in thread "main" java.util.InputMismatchException
	at java.util.Scanner.throwFor(Unknown Source)
	at java.util.Scanner.next(Unknown Source)
	at java.util.Scanner.nextInt(Unknown Source)
	at java.util.Scanner.nextInt(Unknown Source)
	at ACSLGolfClient.main(ACSLGolfClient.java:29)

This post has been edited by bhsanchez72: 01 October 2011 - 10:30 AM

Was This Post Helpful? 0
  • +
  • -

#17 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: ACSL Golf Code errors

Posted 01 October 2011 - 10:36 AM

Remember the assignment specifies the input to be:

INPUT:
1. 3,2,3
2. 4,5,5
3. 5,6,6
4. 4,3,4
5. 4,3,4
6. 4,4,5
7. 5,5,6
8. 3,3,3
9. 4,4,5

Your prompt even asks the user to input data in that format x,y,z. The next time you run it, try typing 5,4,4 at the prompt (return after the second 4).

You have to work with me here. Keep your eye on the goal.
Was This Post Helpful? 0
  • +
  • -

#18 bhsanchez72  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 01-October 11

Re: ACSL Golf Code errors

Posted 01 October 2011 - 10:41 AM

View PostGregBrannon, on 01 October 2011 - 10:36 AM, said:

Remember the assignment specifies the input to be:

INPUT:
1. 3,2,3
2. 4,5,5
3. 5,6,6
4. 4,3,4
5. 4,3,4
6. 4,4,5
7. 5,5,6
8. 3,3,3
9. 4,4,5

Your prompt even asks the user to input data in that format x,y,z. The next time you run it, try typing 5,4,4 at the prompt (return after the second 4).

You have to work with me here. Keep your eye on the goal.

o ya sorry here i enter that and get:
Please enter par, scoreA, scoreB --> 
5,4,4
Exception in thread "main" java.util.InputMismatchException
	at java.util.Scanner.throwFor(Unknown Source)
	at java.util.Scanner.next(Unknown Source)
	at java.util.Scanner.nextInt(Unknown Source)
	at java.util.Scanner.nextInt(Unknown Source)
	at ACSLGolfClient.main(ACSLGolfClient.java:31)


Was This Post Helpful? 0
  • +
  • -

#19 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: ACSL Golf Code errors

Posted 01 October 2011 - 10:51 AM

Well, we've moved you to the playerB score. That's progress. Are you running on Windows? '\n' should work in Windows too, but that might be a minor problem to fix later. It runs fine on my Linux system, by the way. I don't see any typos in what you've changed. In fact, I've cut and pasted your code and it works fine. We'll figure it out.

For now, expand the for loop to read 9 lines of data and store the inputs correctly. You need to keep track of total par, and the total scores for player A and player B. Go ahead and code that and post your updated code. Don't worry so much about running it just yet.
Was This Post Helpful? 0
  • +
  • -

#20 bhsanchez72  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 01-October 11

Re: ACSL Golf Code errors

Posted 01 October 2011 - 10:56 AM

View PostGregBrannon, on 01 October 2011 - 10:51 AM, said:

Well, we've moved you to the playerB score. That's progress. Are you running on Windows? '\n' should work in Windows too, but that might be a minor problem to fix later. It runs fine on my Linux system, by the way. I don't see any typos in what you've changed. In fact, I've cut and pasted your code and it works fine. We'll figure it out.

For now, expand the for loop to read 9 lines of data and store the inputs correctly. You need to keep track of total par, and the total scores for player A and player B. Go ahead and code that and post your updated code. Don't worry so much about running it just yet.

Okay can you show me how to do that because i thought i already did that sorry.
Was This Post Helpful? 0
  • +
  • -

#21 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: ACSL Golf Code errors

Posted 01 October 2011 - 10:58 AM

Explain to me what you did to change the for loop to read 9 lines instead of 4, because I don't see any changes in the for loop you posted above.
Was This Post Helpful? 0
  • +
  • -

#22 bhsanchez72  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 01-October 11

Re: ACSL Golf Code errors

Posted 01 October 2011 - 11:05 AM

View PostGregBrannon, on 01 October 2011 - 10:58 AM, said:

Explain to me what you did to change the for loop to read 9 lines instead of 4, because I don't see any changes in the for loop you posted above.

I did not and i dont now how?

View PostGregBrannon, on 01 October 2011 - 10:58 AM, said:

Explain to me what you did to change the for loop to read 9 lines instead of 4, because I don't see any changes in the for loop you posted above.

Is this what you wanted me to change?
/*
 * Bradley Sanchez
 * Client that obtains data for use by the golfer
 * 9/26/11
 * PD:4B CS2
 */

import java.util.Scanner;
public class ACSLGolfClient {

	
	public static void main(String[] args) 
	{
		//declare variables
		GolfScore myGolfScore;
		int parValue = 0, parTotal = 0, playerA = 0, playerB = 0, totalA = 0, totalB = 0, counter = 0, highScoreA, highScoreB, highScore, aHolesWon = 0, bHolesWon = 0;
		myGolfScore = new GolfScore();
		Scanner kbd = new Scanner(System.in).useDelimiter( ",|\n" );
		//process the data
		for(int holes = 1; holes <= 9; holes++)
		{
			counter++;
			parTotal += parValue;
			totalA += playerA;
			totalB += playerB;
			
			//obtained data
			System.out.println("Please enter par, scoreA, scoreB --> ");
			parValue = kbd.nextInt();
			playerA = kbd.nextInt();
			playerB = kbd.nextInt();
			
			
			//access the class with the data
			myGolfScore.tallyPar(aHolesWon);
			myGolfScore.tallyScores(bHolesWon);
			
			
			
			//output results
			System.out.println(myGolfScore.outcome(aHolesWon, bHolesWon));
			System.out.println(totalA);
			
		}
		totalA = myGolfScore.getScore() - myGolfScore.getpar();
		if(playerA < playerB)
		{
			aHolesWon ++;
		}
		else
		{
			bHolesWon ++;
		}
	
		if(totalA < totalB)
		{
			System.out.println(totalA + " , " + totalB);
		}
		else
		{
			System.out.println(totalB + " , " + totalA);
		}
		
		highScoreA = parTotal - totalA;
		highScoreB = parTotal - totalB;
		
		if (highScoreA >= 0)
		{
			System.out.println(Math.abs(highScoreA) + " under par");
		}
		else
		{
			System.out.println(Math.abs(highScoreA) + " over par");
		}
		
		if (highScoreB < 0)
		{
			System.out.println(Math.abs(highScoreB) + " over par");
		}
		else
		{
			System.out.println(Math.abs(highScoreB) + " under par");
		}
		
		if(aHolesWon > bHolesWon)
		{
			System.out.println(aHolesWon);
		}
		else
		{
			System.out.println(bHolesWon);
		}
}
		
	}

Was This Post Helpful? 0
  • +
  • -

#23 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: ACSL Golf Code errors

Posted 01 October 2011 - 11:13 AM

Exactly.

Now how will you keep track of parTotal, totalA, and totalB? Is it time to start using the other class? I'm not really sure what that's for yet, but it could be used to keep track of par for each hole (do you need that), each player's score on each hole, and each player's total score.

What did you think the other class would do?

Going for a bike ride, about 90 minutes. Someone else can jump in.
Was This Post Helpful? 0
  • +
  • -

#24 bhsanchez72  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 01-October 11

Re: ACSL Golf Code errors

Posted 01 October 2011 - 11:23 AM

View PostGregBrannon, on 01 October 2011 - 11:13 AM, said:

Exactly.

Now how will you keep track of parTotal, totalA, and totalB? Is it time to start using the other class? I'm not really sure what that's for yet, but it could be used to keep track of par for each hole (do you need that), each player's score on each hole, and each player's total score.

What did you think the other class would do?

Going for a bike ride, about 90 minutes. Someone else can jump in.

Okay what i need is the total score for each player, the score in relation to par for the better score, the score in relation to par for the other player, number of holes won by the person with the best score, and the sum of the scores on the hole that is the highest for all the holes played. See that is what i have to figure out how to get and i can wait till you get back from your bike ride because i really need some help. Anyone else who wants to help would be greatly appreciated. Thanks!
Was This Post Helpful? 0
  • +
  • -

#25 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: ACSL Golf Code errors

Posted 01 October 2011 - 01:23 PM

I think all of that can be done with the one class. We'll tackle them shortly.

You never answered if you were using Windows. Which OS are you running?

Try replacing your current line 18 with the following two lines. The new line 18 builds the string to determine where the value z (in user input x,y,z) based on your system's value for new line or end of line. The new line 19 is the old line 18 with the updated string called pattern. Make the substitution (2 lines for 1), rerun it, and let me know if you still get the error you were getting before.
    String pattern = ",|" + System.getProperty( "line.separator" );
    Scanner kbd = new Scanner(System.in).useDelimiter( pattern );

Was This Post Helpful? 0
  • +
  • -

#26 bhsanchez72  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 01-October 11

Re: ACSL Golf Code errors

Posted 01 October 2011 - 01:36 PM

View PostGregBrannon, on 01 October 2011 - 01:23 PM, said:

I think all of that can be done with the one class. We'll tackle them shortly.

You never answered if you were using Windows. Which OS are you running?

Try replacing your current line 18 with the following two lines. The new line 18 builds the string to determine where the value z (in user input x,y,z) based on your system's value for new line or end of line. The new line 19 is the old line 18 with the updated string called pattern. Make the substitution (2 lines for 1), rerun it, and let me know if you still get the error you were getting before.
    String pattern = ",|" + System.getProperty( "line.separator" );
    Scanner kbd = new Scanner(System.in).useDelimiter( pattern );

My teacher wants it in two classes and i am using windows and eclipse. Okay i changed it and no more errors.

This post has been edited by bhsanchez72: 01 October 2011 - 01:38 PM

Was This Post Helpful? 0
  • +
  • -

#27 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: ACSL Golf Code errors

Posted 01 October 2011 - 01:38 PM

Okay. You'll write 2 classes.

Yes, exchange the existing line 18 with the 2 new ones.
Was This Post Helpful? 0
  • +
  • -

#28 bhsanchez72  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 01-October 11

Re: ACSL Golf Code errors

Posted 01 October 2011 - 01:40 PM

View PostGregBrannon, on 01 October 2011 - 01:38 PM, said:

Okay. You'll write 2 classes.

Yes, exchange the existing line 18 with the 2 new ones.

Ok done no more errors.
Was This Post Helpful? 0
  • +
  • -

#29 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: ACSL Golf Code errors

Posted 01 October 2011 - 02:17 PM

Cool! I'm surprised that that was necessary, but I learned something.

This is what I suggest the for loop in the ACSLGolfClient should look like:


        for(int holes = 1; holes <= 9; holes++)
        {
            //obtained data
            System.out.println("Please enter par, scoreA, scoreB --> ");
            parValue = kbd.nextInt();
            playerA = kbd.nextInt();
            playerB = kbd.nextInt();

            // tally the course par
            myGolfScore.tallyPar( parValue );

            // tally score for each player
            myGolfScore.tallyAScores( playerA );
            myGolfScore.tallyAScores( playerB );
            
            // tally holes won
            
            // submit scores for the highest total on any hole
        }

Then the final statement in ACSLGolfClient will be:
        myGolfScore.outputResults();

Can you start making those changes to ACSLGolfClient? Name the methods under the two comments that I left blank. Just show me your completed for loop when you get it done.
Was This Post Helpful? 0
  • +
  • -

#30 bhsanchez72  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 01-October 11

Re: ACSL Golf Code errors

Posted 01 October 2011 - 02:17 PM

View Postbhsanchez72, on 01 October 2011 - 01:40 PM, said:

View PostGregBrannon, on 01 October 2011 - 01:38 PM, said:

Okay. You'll write 2 classes.

Yes, exchange the existing line 18 with the 2 new ones.

Ok done no more errors.

now i need to get the five outputs!
Was This Post Helpful? 0
  • +
  • -

  • (7 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • Last »