9 Replies - 2152 Views - Last Post: 07 April 2007 - 09:16 PM Rate Topic: -----

#1 karadawn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-July 06

Array Usage

Posted 16 July 2006 - 12:43 PM

I'm having a hard time grasping all this, and right now have to write a program using advanced array manipulation. Something here just isn't sinking in, because I can't get this program to run correctly. Could someone possibly explain what I'm doing wrong here?
import javax.swing.*;

public class Golf
{
	public static void main(String args[]) throws Exception
	{
		// Declare variables.
		String golferName;		  // Golfer's name.
		String ageString; 		  // String version of golfer's age.
		int golferAge =0;		  // Golfer's age.
		String scoreString;		  // String version of score.
		int score;					// Golfer's score.
		int numRows = 5;		  // Number of rows in 2D array.
		int numColumns = 9;		  // Number of columns in 2D array.
		int numHoles = 9;		  // Number of holes. 
		int scores[][] = new int[numRows][numColumns]; // 2D array.
		int golferScores[] = new int[numHoles];  // Store nine scores.
		int i; 		// Array index for golferScores array.
		int hole;	   // Hole number.
		int row; 	// Row number in 2D array.

		// Use assignment statements to populate the 2D array here.
		scores[0][0] = 8;
		scores[0][1] = 8;
		scores[0][2] = 9;
		scores[0][3] = 7;
		scores[0][4] = 5;
		scores[0][5] = 7;
		scores[0][6] = 8;
		scores[0][7] = 5;
		scores[0][8] = 8;
		scores[1][0] = 7;
		scores[1][1] = 7;
		scores[1][2] = 8;
		scores[1][3] = 6;
		scores[1][4] = 5;
		scores[1][5] = 6;
		scores[1][6] = 7;
		scores[1][7] = 5;
		scores[1][8] = 6;
		scores[2][0] = 6;
		scores[2][1] = 5;
		scores[2][2] = 6;
		scores[2][3] = 5;
		scores[2][4] = 4;
		scores[2][5] = 5;
		scores[2][6] = 5;
		scores[2][7] = 4;
		scores[2][8] = 5;
		scores[3][0] = 5;
		scores[3][1] = 4;
		scores[3][2] = 4;
		scores[3][3] = 4;
		scores[3][4] = 3;
		scores[3][5] = 4;
		scores[3][6] = 3;
		scores[3][7] = 3;
		scores[3][8] = 4;
		scores[4][0] = 4;
		scores[4][1] = 3;
		scores[4][2] = 3;
		scores[4][3] = 3;
		scores[4][4] = 2;
		scores[4][5] = 3;
		scores[4][6] = 2;
		scores[4][7] = 3;
		scores[4][8] = 3;
		
		// Get user input. 		
		if((golferName = JOptionPane.showInputDialog("Enter golfer's name: ")) != null)
		{
			ageString = JOptionPane.showInputDialog("Enter golfer's age: ");
			golferAge = Integer.parseInt(ageString); 
			for(i = 0; i < numHoles; i++) 
			{
				hole = i+1;
				scoreString = JOptionPane.showInputDialog("Enter score " + hole);
				golferScores[i] = Integer.parseInt(scoreString);
			}
		}
		// Print golfer's name and age here.
			System.out.println("Golfer's Name: " + golferName);
			System.out.println("Age: " + golferAge);

		// Write for loop for each of 9 holes here.
		hole = 0;
		while(hole < 9)
		{
			
			// Calculate row in 2D array based on golfer's age.
			if(golferAge <= 4)
				numRows = 0;

			else if(golferAge <= 7)
				numRows = 1;
			else if(golferAge <= 11)
				numRows = 2;
			else if(golferAge <= 15)
				numRows = 3;
			else
				numRows = 4;
			
			
		
			 
		if(golferScores[numHoles]< scores[numRows][numColumns])
				System.out.println("Hole" + hole + "Score" + golferScores[numHoles - 1] + "Under Par");
	
			else if(golferScores[numHoles] == scores[numRows][numColumns])
						System.out.println("Hole" + hole + "Score" + golferScores[numHoles - 1] + "Par");
			else
				System.out.println("Hole" + hole + "Score" + golferScores[numHoles - 1] + "Over Par");
	hole++;
		}	
			// Print score and decide which phrase to print for each of the nine holes here.
			
			
		
		// Exit the program.
		System.exit(0);

	} // End of main() method.
} // End of Golf class.



It's suppose to get the scores from the user, then print out the golder name, age, scores and if it's under or over par depending on the hole par and golfer age. I've got the arrays set from the table in the book, using 1-1 as 0-0. However, it still kicks out the out of bounds error. I'm stumped here. :( An explination would be helpful.

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Array Usage

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Array Usage

Posted 16 July 2006 - 01:48 PM

Can you post the entire error message you are getting?

It will point to where the problem is making it easier to debug.
Was This Post Helpful? 0
  • +
  • -

#3 karadawn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-July 06

Re: Array Usage

Posted 16 July 2006 - 03:36 PM

Error is:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
at Golf.main(Golf.java:112)

Which means array out of bounds on this line:

if(golferScores[numHoles] < scores[numRows][numColumns])



I know there is something that's not right there, but I'm having a hard time grasping the concept of arrays in general. Well, no so much the concept, but rather how they are written.

Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Array Usage

Posted 16 July 2006 - 03:50 PM

you set numHoles to nine initialy and it is not changed, thus your array with values 0-8 is out of bounds at 9
make it:
if(golferScores[numHoles-1] < scores[numRows][numColumns])
instead

This post has been edited by William_Wilson: 16 July 2006 - 03:51 PM

Was This Post Helpful? 0
  • +
  • -

#5 karadawn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-July 06

Re: Array Usage

Posted 16 July 2006 - 04:24 PM

Same thing I'm afraid - still no dice :(
Was This Post Helpful? 0
  • +
  • -

#6 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Array Usage

Posted 16 July 2006 - 06:07 PM

As William_Wilson stated the problem is that you are using varaibles that are static in value, meaning they don't change.

numHoles & numColumns are both initialized to 9 and that value does not change throughout your program. In this statement scores[numRows][numColumns], numRows & numColumns are the index by which you are accessing values from your array.

Well if you initialize an array to 9 elements then its index is a value 0 - 8 only. An index of 9 will put your array out of bounds. You must get the value inbouds by either subtracting 1 to get an index of 8. Or else reassigning the variables to that they contain an index value between 0 and 8.
Was This Post Helpful? 0
  • +
  • -

#7 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Array Usage

Posted 16 July 2006 - 06:56 PM

To put it as simply as possible, you have intialized the arrays to be 9 elements for one, and 5 by nine elements for the other...with an index start of 0, this means the last elements in each array would be
golferScores[8]
scores[4][8]


Since you are attempting to access
golferScores[9]
scores[5][9]


each attempt at access is out of bounds.
Was This Post Helpful? 0
  • +
  • -

#8 karadawn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-July 06

Re: Array Usage

Posted 16 July 2006 - 07:11 PM

Woo hoo! Alright, I'm beginning to understand this a bit better now, and changed the code to this:

	
	hole = 0;
		while(hole < 8)
		{
			
			// Calculate row in 2D array based on golfer's age.
			if(golferAge <= 4)
				numRows = 0;

			else if(golferAge <= 7)
				numRows = 1;
			else if(golferAge <= 11)
				numRows = 2;
			else if(golferAge <= 15)
				numRows = 3;
			else
				numRows = 4;
			
			
		
			 
		if(golferScores[numHoles - 1] < scores[numRows - 1][numColumns - 1])
				System.out.println("Hole" + hole + "Score" + golferScores[numHoles - 1] + "Under Par");
	
			else if(golferScores[numHoles - 1] == scores[numRows - 1][numColumns - 1])
						System.out.println("Hole" + hole + "Score" + golferScores[numHoles - 1] + "Par");
			else
				System.out.println("Hole" + hole + "Score" + golferScores[numHoles - 1] + "Over Par");
	hole++;
		}	




Now it's not throwing the out of bounds error, but still not working the way it should.

For one, the first thing it prints is "Hole0" when it should be 1.

For two, on my test imput it gives the same score (the last one) for every hole and says "Under Par" when it should say Par.

I am so very lost and the teacher is of VERY little help. About all I've been told is "look in the book at this page"....doesn't matter how many times I read it, I still don't understand. :/
Was This Post Helpful? 0
  • +
  • -

#9 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Array Usage

Posted 17 July 2006 - 05:36 AM

It prints Hole0 to begin with as that is the value that the variable hole is initialized to.

As for the problem with the scores, I suggest you print out both the score and the value to which it is being compared to track the problem...I believe you'll see it immediately.
Was This Post Helpful? 0
  • +
  • -

#10 Coindsm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-April 07

Re: Array Usage

Posted 07 April 2007 - 09:16 PM

View PostAmadeus, on 17 Jul, 2006 - 05:36 AM, said:

It prints Hole0 to begin with as that is the value that the variable hole is initialized to.

As for the problem with the scores, I suggest you print out both the score and the value to which it is being compared to track the problem...I believe you'll see it immediately.



instead of doing that first I would suggest that you change your while loop to a for loop as the variables have already been initalized there should be no changing them which you did in the while loop hole = 1 so first look at the for loop in the get user input statement this should be the exact code you need for the loop then instead of changing the value of numRows to access the age just change the value of row. Then your print stmt is what is making the next problem think of what the [i] is for and what the [row] is for the golferScores need 1 subscript and the scores needs two subscripts by changing these you should have the correct code and the program works perfectly let me know
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1