Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!

Johnny Peterson

Greenhorn
+ Follow
since Sep 11, 2012
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Johnny Peterson

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?
8 years ago

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?
8 years ago
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
8 years ago

fred rosenberger wrote:System.out.println() is your best friend. I would stick some in just before line 65, so you have something like this:



i suppose you could print height and width before entering the loop on line 60, which would reduce the noise a little.



Yes, that was a good tip of you. I got the height and the width right, but after it shows that i is 0 and j is 1, it displays the same error, which is weird, it does not store anything into the dimensions array. I think that maybe I am not saving the characters in the file correctly?
8 years ago

John Jai wrote:Welcome to Javaranch, Johnny.

Yes the issue in the for loop. Note that array indices start from [0] to [array's length - 1]. So the char [height][width][depth] array can hold char[0][0][0] to char [height-1][width-1][depth-1] items (since index starts from 0).

Since you are not looping from 0 to length - 1, but directly try to assign to array's length, dimensions[height][width] you receive the ArrayIndexOutOfBoundsException.

I shall move the thread to Beginning Java forum.



Thanks for the help, but when I do the following :



I still get the same error, which confuses me.
8 years ago
Hi all,

I have the following task : I am supposed to create a maze from a text file, the maze can be multiple levels. A # is a wall, a . is a space, a @ is start, a * is an exit, and a ^ signifies a stair, which leads to a different level of the maze. I am supposed to determine the dimensions of the maze and store them into an array, as well as populate the array with the character at the given location. I am having trouble with the last part, and I would appreciate any help. Here is what I have so far :



Here is what I get when I compile :

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
at Maze.main(Maze.java:65)

So my problem lies in the for loop, but I am unsure why it is giving me that error. I checked the height, width, and depth numbers, and they all are correct. Any hints or tips will be appreciated, I seem to be stuck on this. Also, here is the text file I am using :

#######
#...#@#
#^#...#
#######
-------
#######
#...###
#^#.*##
#######

Thanks in advance!

8 years ago