0

Hi everybody.

I'm working on my program. And have this output storred in the object puzzle:

Puzzle is:
   1   5   9  13   2   6  10  14   3   7  11  15   4   8  12  16
   2   6  10  14   3   7  11  15   4   8  12  16   5   9  13   1
   3   7  11  15   4   8  12  16   5   9  13   1   6  10  14   2
   4   8  12  16   5   9  13   1   6  10  14   2   7  11  15   3
   5   9  13   1   6  10  14   2   7  11  15   3   8  12  16   4
   6  10  14   2   7  11  15   3   8  12  16   4   9  13   1   5
   7  11  15   3   8  12  16   4   9  13   1   5  10  14   2   6
   8  12  16   4   9  13   1   5  10  14   2   6  11  15   3   7
   9  13   1   5  10  14   2   6  11  15   3   7  12  16   4   8
  10  14   2   6  11  15   3   7  12  16   4   8  13   1   5   9
  11  15   3   7  12  16   4   8  13   1   5   9  14   2   6  10
  12  16   4   8  13   1   5   9  14   2   6  10  15   3   7  11
  13   1   5   9  14   2   6  10  15   3   7  11  16   4   8  12
  14   2   6  10  15   3   7  11  16   4   8  12   1   5   9  13
  15   3   7  11  16   4   8  12   1   5   9  13   2   6  10  14
  16   4   8  12   1   5   9  13   2   6  10  14   3   7  11  14

What I would like to knowis how to get every single number and store them in the array, or even better in 2D array.

4
Contributors
7
Replies
8
Views
5 Years
Discussion Span
Last Post by DavidKroukamp
0

Hi everybody.

I'm working on my program. And have this output storred in the object puzzle:

Puzzle is:
   1   5   9  13   2   6  10  14   3   7  11  15   4   8  12  16
   2   6  10  14   3   7  11  15   4   8  12  16   5   9  13   1
   3   7  11  15   4   8  12  16   5   9  13   1   6  10  14   2
   4   8  12  16   5   9  13   1   6  10  14   2   7  11  15   3
   5   9  13   1   6  10  14   2   7  11  15   3   8  12  16   4
   6  10  14   2   7  11  15   3   8  12  16   4   9  13   1   5
   7  11  15   3   8  12  16   4   9  13   1   5  10  14   2   6
   8  12  16   4   9  13   1   5  10  14   2   6  11  15   3   7
   9  13   1   5  10  14   2   6  11  15   3   7  12  16   4   8
  10  14   2   6  11  15   3   7  12  16   4   8  13   1   5   9
  11  15   3   7  12  16   4   8  13   1   5   9  14   2   6  10
  12  16   4   8  13   1   5   9  14   2   6  10  15   3   7  11
  13   1   5   9  14   2   6  10  15   3   7  11  16   4   8  12
  14   2   6  10  15   3   7  11  16   4   8  12   1   5   9  13
  15   3   7  11  16   4   8  12   1   5   9  13   2   6  10  14
  16   4   8  12   1   5   9  13   2   6  10  14   3   7  11  14

What I would like to knowis how to get every single number and store them in the array, or even better in 2D array.

Might this help:http://www.leepoint.net/notes-java/data/arrays/arrays-2D.html
[edit] answering the titles question
try this:

Object object...... 
int i =Integer.parseInt( object.toString() );

Edited by DavidKroukamp: n/a

0

Might this help:http://www.leepoint.net/notes-java/data/arrays/arrays-2D.html
[edit] answering the titles question
try this:

Object object...... 
int i =Integer.parseInt( object.toString() );

This actually gives me an error.

atjava.lang.NumberFormatException.forInputString(NumberFormatException.java:65)/CODE]

[code]
int i = Integer.parseInt(puzzle.toString().substring(11));
	  	System.out.println(i);

I think it because puzzle.toString also have spaces. is there any way to fix it?
or to store every single number in the array

Edited by shean1488: n/a

0

puzzle.toString().substring(11)

What is the String that is returned by that call to substring?
For debugging, get that String into a separate String variable and print it out so you can see what the program is doing.
Be sure to add delimiters on each side of the String so you can see if it has any spaces in it. ">" + theString + "<"

0

I assume he's reading the lines instead of int's.
just use the split method of the String class, you'll get an array of Strings which you can easily convert

0

This actually gives me an error.
atjava.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
int i = Integer.parseInt(puzzle.toString().substring(11));
System.out.println(i);

I think it because puzzle.toString also have spaces. is there any way to fix it?
or to store every single number in the array

Also could you not use the trim method on your string to take away all white/blank spaces?

Edited by mike_2000_17: Fixed formatting

1

trim only removes the leading and tailing spaces, not the ones in between.
if he uses split with space as separator, I don't think he'll need to go that far anyway.

0

trim only removes the leading and tailing spaces, not the ones in between.
if he uses split with space as separator, I don't think he'll need to go that far anyway.

oooh wow learnt something new :D havent used trim much...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.