| Author |
Multidimensional Array Help
|
Johnny Peterson
Greenhorn
Joined: Sep 11, 2012
Posts: 6
|
|
Hi all,
I am having the following problem : I am reading a file, and then storing it into a 3D char array. The problem compiles fine, but when I choose a file using JFile Chooser, it gives me a NoSuchElementException. It is probably something simple I am forgetting, but it is driving me crazy. Here is the portion of the code that is troubling me, any help will be appreciated :
It reads the whole file perfectly, but then right after the last line of the file, it gives me this :
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1585)
at Maze.main(Maze.java:87)
And it leads me to line 75 when I click on the error. Any help will be gladly appreciated, thanks in advance
|
 |
changu mani
Greenhorn
Joined: Aug 31, 2012
Posts: 25
|
|
Hi Johnny,
Please put if loop as shown and check.
|
 |
Johnny Peterson
Greenhorn
Joined: Sep 11, 2012
Posts: 6
|
|
changu mani wrote:Hi Johnny,
Please put if loop as shown and check.
I am assuming this is supposed to go inside the second for loop?
|
 |
changu mani
Greenhorn
Joined: Aug 31, 2012
Posts: 25
|
|
|
Yes Johnny. It should go inside second for loop since that is where you are getting the exception.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9939
|
|
Johnny Peterson wrote:I am reading a file, and then storing it into a 3D char array.
just to be pedantic...No, you are not.
Java does not have multi-dimensional arrays. Java only had one dimensional arrays, but they can hold just about anything...including arrays.
So what you have is an array that holds arrays that hold arrays that hold chars.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4725
|
|
fred rosenberger wrote:just to be pedantic...No, you are not.
Java does not have multi-dimensional arrays. Java only had one dimensional arrays, but they can hold just about anything...including arrays.
Just to be complete here, Fred is absolutely right, BUT the language also offers:
(a) Semantics to initialize a 3D (or almost any-D) matrix in a single statement.
(b) The ability to access a particular cell in an any-D matrix directly by the use of indexes.
However, (b) requires (a); so if you don't set up your matrix in a single statement, you may well run into trouble.
Also, generic values (such as length) should still be accessed the way they're intended (ie, as the property of a basic array).
Winston
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
Johnny Peterson
Greenhorn
Joined: Sep 11, 2012
Posts: 6
|
|
Winston Gutkowski wrote:
fred rosenberger wrote:just to be pedantic...No, you are not.
Java does not have multi-dimensional arrays. Java only had one dimensional arrays, but they can hold just about anything...including arrays.
Just to be complete here, Fred is absolutely right, BUT the language also offers:
(a) Semantics to initialize a 3D (or almost any-D) matrix in a single statement.
(b) The ability to access a particular cell in an any-D matrix directly by the use of indexes.
However, (b) requires (a); so if you don't set up your matrix in a single statement, you may well run into trouble.
Also, generic values (such as length) should still be accessed the way they're intended (ie, as the property of a basic array).
Winston
That sounds very interesting Winston... Where can I read more about that?
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4725
|
|
Johnny Peterson wrote:That sounds very interesting Winston... Where can I read more about that? 
The JLS I guess; it's a basic part of the language.
To set up a rectanglar matrix:
int [][] matrix = new int[3][4];
To access a cell:
int cell = matrix[1][2];
To get the number of rows:
int rows = matrix.length;
To get the number of columns:
int cols = matrix[0].length;
but obviously the latter is only going to work once 'matrix' is initialized.
The main problem for beginners is that an array set up as above looks and acts like a 2D array, but it's actually an array of arrays.
And that last statement really only works if you've set up the array as above, because it guarantees that every row is the same length.
However, there's nothing to stop you from initializing (or even changing) each row to have different lengths if you want, so when you're traversing a "2D" array, you should generally use the row length (matrix[r].length).
Luckily there's a simpler alternative for basic traversals: the for-each loop, viz:and it's guaranteed to work, providing the array is fully initialized, even if rows aren't the same length.
Winston
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
|
|
|
You will find a little in the Java Tutorials, but it really is only a little.
|
 |
 |
|
|
subject: Multidimensional Array Help
|
|
|