• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Multidimensional Array Help

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Johnny,
Please put if loop as shown and check.

 
Johnny Peterson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Johnny. It should go inside second for loop since that is where you are getting the exception.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Johnny Peterson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will find a little in the Java Tutorials, but it really is only a little.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic