• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about storing input into array  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi there.
I'm working on a java exercise and I have no idea how to store input into array.
Here's the array
 0 1 2
0 _ _ _
1 _ _ _
2 _ _ _
for example user inputs row number as 2 , column number as 1
then the result'll be:
 0 1 2
0 _ _ _
1 _ _ x
2 _ _ _
Can someone please explains the steps and the coding about this question.
Thanks!
 
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Sorry but I don't understand what you are trying to achieve. Can you post the whole question.

For help on arrays I suggest you read: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
 
poopat pongkari
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:Welcome to the Ranch.

Sorry but I don't understand what you are trying to achieve. Can you post the whole question.

For help on arrays I suggest you read: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Thank you for answering.Sorry for the unclear explanation.
attached is the whole question.
566565.JPG
[Thumbnail for 566565.JPG]
whole question
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First read the link I posted earlier so you understand how arrays work.

You need to use an array of arrays to define the 3 x matrix. To store a value at a particular location you would use something like:
 
poopat pongkari
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:First read the link I posted earlier so you understand how arrays work.

You need to use an array of arrays to define the 3 x matrix. To store a value at a particular location you would use something like:

I see.Thank you.
One more question.
How to display the empty slot as"_"?
For example
row number :1
column number : 2
value : 3
the gameboard should be like
 0 1 2
0 _ _ _
1 _ _ 3
2 _ _ _
For the slots that haven't be selected(e.g[0][0],[0][1]...),how can i display them as a "_"?
Thanks in advance.

 
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
poopat pongkari wrote:For the slots that haven't be selected(e.g[0][0],[0][1]...),how can i display them as a "_"?

I assume you follow your instructions and mark empty slot by value 0.
So check if the value equals 0. If it does print "_". If it doesn't print the value.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:So check if the value equals 0. If it does print "_". If it doesn't print the value.

Or possibly better still, create a Slot class. Then you can declare your array as:
Slot[][] myBoard = new Slot[4][4];
and use its toString() method to display the values accordingly.

Winston
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have an int[][], it will be filled with 0s as a default.An Object[][] would be filled with nulls as a default; if you fail to initialise each of the 1D arrays it will suffer a NullPointerException.
 
poopat pongkari
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you everyone for helping.
But I still can't figure out how to store a value at a particular location.
should I use for loop?
And I use scanners to get row&col,is it correct?
I'm new to Java,sorry if the questions are silly :s
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should go and write out the structure with pencil and paper. Then you will be able to see what it means to put in square (0, 2).
 
poopat pongkari
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Here's something I've done so far of inputting value to the game board.I know the output is ridiculous X).Please let me know where should I correct.
Thanks.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't format your code it makes it extremely difficult to read (for you and us).

Look at how much easier this is to read and understand.

BTW This code doesn't input a value into the game.
You need to do what Campbell has already suggested and write out how to solve this in your native language before writing any more code.
You can't just post code here and ask us to correct it.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!