aspose file tools*
The moose likes Beginning Java and the fly likes Multidimensional Array Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Multidimensional Array Help" Watch "Multidimensional Array Help" New topic
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
Ranch Hand

Joined: Aug 31, 2012
Posts: 30
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
Ranch Hand

Joined: Aug 31, 2012
Posts: 30
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: 11479
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8223
    
  23

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?
Articles by Winston can be found here
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: 8223
    
  23

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: 39834
    
  28
You will find a little in the Java Tutorials, but it really is only a little.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multidimensional Array Help