Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Place A Word In A 2d Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Place A Word In A 2d Array" Watch "Place A Word In A 2d Array" New topic
Author

Place A Word In A 2d Array

mary desi
Greenhorn

Joined: Dec 17, 2011
Posts: 13
Is there a way to place a word in a 2d array in a specific position? For example,i want to give the word, choose vertical or horizontal and the position ((3,3) or (3,4) or (5,6) etc) and the word will be placed in that position.This is my code for the array...

the * means that the word cant be placed there.
I have tried to do the array a string array but when i do this "String[][]" and place a word all then letters of the word are placed in the same position(e.g (2,3) ).i want the first letter to be placed in (2,3) the second in (2,4) etc so i thing char array is the right one but i dont know how to continue..any idea?
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
It appears that you want the individual letters of your string to go into the array positions. You can do this with String.charAt(int i).

If you have a variable named 'word' that contains your string, then a loop using word.charAt(i) that runs i from 0 to (word.length() - 1), i.e.

will return each letter of the string held in word. You can use that character to put into your 2d array of characters.

Is that enough to get you going on it? It wasn't clear to me what your question was.

rc>
mary desi
Greenhorn

Joined: Dec 17, 2011
Posts: 13
based on what you said i wrote this code but it doesnt work
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
We need more information than "it doesn't work". I don't know what you're trying to do, and you don't tell me what happens when you try to do whatever it is.
mary desi
Greenhorn

Joined: Dec 17, 2011
Posts: 13
I want for example if i say placeWord(board,"foo",3,4,horizontal) I would end up with board[3][4] = 'f', board[3][5] = 'o' etc.
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
so what doesn't work?
mary desi
Greenhorn

Joined: Dec 17, 2011
Posts: 13
n main i create the board as i wrote in my fisrt post,i create a class which contains the public void placeWordHorizontally ...then i put values in main to test it (these are the values "String s="abc"; int k=2; int m=3; b.placeWordHorizontally(Board, s, m, m); System.out.println(Arrays.deepToString(Board));" and it returns two arrays--the fisrt is the one with the stars-and the second is an empty one with the values i put..i also tried this to print the board for(int i=0;i<Board.length;i++) { System.out.println(Board[i]); } but it returns a similar thing like the last one ...i am confused –
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
Perhaps you had better just post the whole program, unless it has a lot more in it than I'm imagining. I just don't understand what is happening from your description, or even what you expect to happen.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
And don’t talk about 2D arrays. There is no such thing. What you showed is an array of arrays.
mary desi
Greenhorn

Joined: Dec 17, 2011
Posts: 13
Ralph Cook wrote:Perhaps you had better just post the whole program, unless it has a lot more in it than I'm imagining. I just don't understand what is happening from your description, or even what you expect to happen.

i want each letter of the word to be in a differenn position of the array...otherwise the whole word is in the same position...
mary desi
Greenhorn

Joined: Dec 17, 2011
Posts: 13
Campbell Ritchie wrote:And don’t talk about 2D arrays. There is no such thing. What you showed is an array of arrays.

so what should i change?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
You should say array of arrays. You should not change any code.
mary desi
Greenhorn

Joined: Dec 17, 2011
Posts: 13
Campbell Ritchie wrote:You should say array of arrays. You should not change any code.

i dont understand what you want to say....i would apreicie it if you help me understand..
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7081
    
  16

mary desi wrote:
Campbell Ritchie wrote:You should say array of arrays. You should not change any code.

i dont understand what you want to say....i would apreicie it if you help me understand..

It's Campbell's 'cause célèbre' - Java implements 2D arrays as an array of arrays, not as a matrix.

Therefore, if you define a 2D array:
int[][] my2DArray = new int[10][10];
the 3rd row (my2DArray[2]) is actually an int[] (ie, an int array).

For that reason, you can use things like my2DArrray[2].length to return the number of elements in the 3rd row (in this case: 10), and the correct way to write a loop for a 2D array is:
HIH

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
When someone trying to help you asks you to post the whole program please post the whole program. What you posted was most of it, but I had to fiddle with the source because it is not a class file, figure out what the name of the class file had to be, and in general futz around with it to get it to compile, so that I could run it, so that I could THEN analyze your code and try to answer your question.

If you will put this statement in your method placeWordHorizontally(), immediately after "board[x][y+i] = word.charAt(i);":

you will see that the program is, in fact, putting each letter of "abc" into a different position in the array - 3,3 ; 3,4 ; 3,5.

I can make guesses about why you think it isn't doing that, but I'd rather not. (And you might have meant to start at 2,3, but when you call placeWordHorizontal you don't use k.)

I cannot tell how much effort you are putting into describing your problem, but it is difficult for me to understand what it is (and you aren't getting other answers to it, I notice.) It is common for less experienced programmers (heck, *experienced* programmers) to mistake what the problem actually is, and so a skill you need to develop as you learn about programming is how to analyze what you have, how to debug to test why something is going wrong, etc. In addition, you need to be able to describe in precise and concise terms just what it is you are trying to do and what your program is doing instead. I'm afraid it is taking more than usual effort to try to help you.

rc
mary desi
Greenhorn

Joined: Dec 17, 2011
Posts: 13
i am trying to get better...I am gonna explain what i want with more details..i gine you a screenshot of my initial board,the second screenshot is the result i want to take when i put values to in public void placeWordHorizontally(char[][] board, String word, int x, int y)---at "char[][] board" i want to put the initial board, at 'word' the 'abc',at 'x' the row and at'y' the column.At third screenshot is what i get with the code i wrote so far...hope that helps...



[Thumbnail for initial.JPG]


[Thumbnail for whatiwant.jpg]


[Thumbnail for sofar.JPG]

Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
As I said before, your letters are getting into the board array; they go on row 3 ins of row 2 because of how you call your method.

So it appears to me that this little test of putting the letters of a word in different char cells in an array of arrays (or a 2D array -- most people know what you mean by that, Mr. Ritchie just thinks it's the wrong term to use) is working. I don't know what other help you want.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
Winston Gutkowski wrote: . . . It's Campbell's 'cause célèbre' . . .
You mean one of them

An array of arrays is better than a 2D array, because you can easily replace one of its elements with an array of a different size. If you had a 2D array it would be like this:
1 2 3
4 5 6
7 8 9
In that case, you are stuck with a 3 × 3 array.

But an array of arrays is like this:If I haven’t got lots of sppelling errors in that code, you will see it in action. You can see the zero-element array as a blank line.
mary desi
Greenhorn

Joined: Dec 17, 2011
Posts: 13
Ralph Cook wrote:As I said before, your letters are getting into the board array; they go on row 3 ins of row 2 because of how you call your method.

So it appears to me that this little test of putting the letters of a word in different char cells in an array of arrays (or a 2D array -- most people know what you mean by that, Mr. Ritchie just thinks it's the wrong term to use) is working. I don't know what other help you want.

The promblem is that with the code i have written so far it returns 2 array...I want 1 as the one at the piscture i posted(picture'what_I-Want')Do you know why this happens?
mary desi
Greenhorn

Joined: Dec 17, 2011
Posts: 13
Campbell Ritchie wrote:
Winston Gutkowski wrote: . . . It's Campbell's 'cause célèbre' . . .
You mean one of them

An array of arrays is better than a 2D array, because you can easily replace one of its elements with an array of a different size. If you had a 2D array it would be like this:
1 2 3
4 5 6
7 8 9
In that case, you are stuck with a 3 × 3 array.

But an array of arrays is like this:If I haven’t got lots of sppelling errors in that code, you will see it in action. You can see the zero-element array as a blank line.


I think its very interesting that and gives you the ability to do lot of thinks with the arrays...I havent seen it in action but when i find time i will look at it.Thanks a lot!
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
mary desi wrote:The promblem is that with the code i have written so far it returns 2 array...

"returns", in a programming context, usually means that a subprogram has calcualted, generated, or found some value and returns it to its calling subprogram. So it's a bit confusing in this case, because you don't have anything that is returning 1 array, much less 2.

If you mean it is printing 2 arrays, that is easily explained -- the loop in the main method prints out the array in its larger format, with an underscore "_" for each letter and * for blocked positions. The loop in the placeWordHorizontally() method prints the array in its smaller format, where, for instance, "abc" is printed as three individual letters next to each other, which does make it look like they are a string.

So if your question is "why is it printing 2 arrays", the answer is that your program is written to do that. If your question is "why is my second array a smaller format than my first one", the answer is "the way you are printing it in the placeWordHorizontally() method is just printing out each vertical array, one letter after the other, with no spaces in between.
mary desi wrote:
I want 1 as the one at the piscture i posted(picture'what_I-Want')Do you know why this happens?

If you want one array, get rid of one of the array-printing sequences. If you want the bigger format to show the letters you are putting in the array, in the bigger format, then invoke that printing code after you have put the characters in the array.
mary desi
Greenhorn

Joined: Dec 17, 2011
Posts: 13
Ralph Cook wrote:
mary desi wrote:The promblem is that with the code i have written so far it returns 2 array...

"returns", in a programming context, usually means that a subprogram has calcualted, generated, or found some value and returns it to its calling subprogram. So it's a bit confusing in this case, because you don't have anything that is returning 1 array, much less 2.

If you mean it is printing 2 arrays, that is easily explained -- the loop in the main method prints out the array in its larger format, with an underscore "_" for each letter and * for blocked positions. The loop in the placeWordHorizontally() method prints the array in its smaller format, where, for instance, "abc" is printed as three individual letters next to each other, which does make it look like they are a string.

So if your question is "why is it printing 2 arrays", the answer is that your program is written to do that. If your question is "why is my second array a smaller format than my first one", the answer is "the way you are printing it in the placeWordHorizontally() method is just printing out each vertical array, one letter after the other, with no spaces in between.
mary desi wrote:
I want 1 as the one at the piscture i posted(picture'what_I-Want')Do you know why this happens?

If you want one array, get rid of one of the array-printing sequences. If you want the bigger format to show the letters you are putting in the array, in the bigger format, then invoke that printing code after you have put the characters in the array.



thanks for your answer it helps a lot!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Place A Word In A 2d Array
 
Similar Threads
Creating simple GUI interface for BattleShips
Creating simple GUI interface for BattleShips
Battleships - guys please help me out!
cleaner code
Just wanted to share my code SudokuSolver !