9 Replies Latest reply: Sep 2, 2008 6:15 AM by PhHein

# 2D array question

Hi!

I'm don't know why this part of my code is giving me an error:
``````for(int x=1; x<=numJobs; x++)
System.out.println("Please enter the details for job "+ x + ": ");
for(int y=1; y<=3; y++)
System.out.println("Detail " + y + ": ");
startT = Integer.parseInt(inputString);
jobList[x][y] = startT;``````
It says that it cannot find the variables x and y in the line:
``jobList[x][y] = startT;``
But I thought that they have been declared at the start with the for loop?
I want to assign the user input into the different parts of the 2d array.

Can anybody point me in the right direction please?

Much appreciated!
• ###### 1. Re: 2D array question
Post the exact stack trace, please.
• ###### 2. Re: 2D array question
``````        double jobList[][];
int numJobs;

System.out.println("Please enter the number of jobs: ");
numJobs = Integer.parseInt(inputString);

jobList = new double[numJobs][3];

for(int x=1; x<=numJobs; x++)
System.out.println("Please enter the details for job "+ x + ": ");
for(int y=1; y<=3; y++)
System.out.println("Detail " + y + ": ");
startT = Integer.parseInt(inputString);
jobList[x][y] = startT;``````
Is this what you meant?
• ###### 3. Re: 2D array question
oh... I found the problem but I'd like to know why I can't do it
``````        for(int x=1; x<=numJobs; x++)
//System.out.println("Please enter the details for job "+ x + ": ");
for(int y=1; y<=3; y++)
//System.out.println("Detail " + y + ": ");
//startT = Integer.parseInt(inputString);
jobList[x][y] = startT;``````
When I take these bits out of my code then x and y can be found. But if I want the elements of the array to be defined by the user it does not allow it.

Can anybody tell me why this is please?
• ###### 4. Re: 2D array question
Put some { } at the start and end of your for loops. At the moment, only the line that immediately follows them is actually part of the loops
• ###### 5. Re: 2D array question
Thanks for the help passgodev! It works now! It recognizes the x and the y in the loop.
• ###### 6. Re: 2D array question
Giving array out of bounds message
• ###### 7. Re: 2D array question
Yes. Arrays start at 0.
[http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html]
• ###### 8. Re: 2D array question
silly me... thanks PhHein!
• ###### 9. Re: 2D array question
YW