writeInt problem within the array.

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 323 Views - Last Post: 14 January 2013 - 05:16 AM Rate Topic: -----

#1 DaiToBu  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 17-March 12

writeInt problem within the array.

Posted 13 January 2013 - 05:51 PM

I having problem trying to write 3d array into the file. I don't understand why if I put in the exact Int then it able to write into my text file, but when I put in the array elements it doesn't. Please help thank you.

	private static void writeData(int[][][] data, String string) throws Exception {
		FileOutputStream os = new FileOutputStream("data.txt");
		DataOutputStream out = new DataOutputStream(os);
		int elements;
		

		for (int f = 0; f < data.length; f++) {
			for (int r = 0; r < data[f].length; r++) {
				for (int c = 0; c < data[f][r].length; c++) {
					elements = data[f][r][c];
					out.writeInt(99); // THIS WORK
					out.writeInt(elements); // THIS NOT WORK
					System.out.print(elements + " ");
				}
			}
		}

		os.close();
		out.close();
	}



Is This A Good Question/Topic? 0
  • +

Replies To: writeInt problem within the array.

#2 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • Joined: 19-December 11

Re: writeInt problem within the array.

Posted 13 January 2013 - 05:57 PM

My guess is the 3d array isn't correctly initialized, also, did you happen to get an error? If so, please provide the stack trace.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: writeInt problem within the array.

Posted 13 January 2013 - 06:03 PM

Should work... array initialized or not
If not initialized it will write 0

You will have to describe what to qualify as "not work"
Was This Post Helpful? 0
  • +
  • -

#4 DaiToBu  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 17-March 12

Re: writeInt problem within the array.

Posted 13 January 2013 - 06:12 PM

Well, when I tried to test print the element on the screen using System.out. It print the elements correctly. The WORK mean it print the results into the textfile, and the NOT WORK mean it doesn't print anything into the text file.

It write on the data.txt when I put the number 99 or any number. When I changed it to the elements, then it doesn't write anything.

Oh, and no error during the run.
Was This Post Helpful? 0
  • +
  • -

#5 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • Joined: 19-December 11

Re: writeInt problem within the array.

Posted 13 January 2013 - 06:35 PM

Use a BufferedWriter instead of a DataOutputStream.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: writeInt problem within the array.

Posted 13 January 2013 - 06:37 PM

View PostDaiToBu, on 13 January 2013 - 09:12 PM, said:

It write on the data.txt when I put the number 99 or any number. When I changed it to the elements, then it doesn't write anything.

How can you claim that ? What is the size of data.txt ?
How do you verify that it is not in the file ? Do you write a program to read the data back ? If not how do you check that the array is effectively there or not ?
Was This Post Helpful? 0
  • +
  • -

#7 DaiToBu  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 17-March 12

Re: writeInt problem within the array.

Posted 13 January 2013 - 06:44 PM

View Postpbl, on 13 January 2013 - 06:37 PM, said:

View PostDaiToBu, on 13 January 2013 - 09:12 PM, said:

It write on the data.txt when I put the number 99 or any number. When I changed it to the elements, then it doesn't write anything.

How can you claim that ? What is the size of data.txt ?
How do you verify that it is not in the file ? Do you write a program to read the data back ? If not how do you check that the array is effectively there or not ?


The file is 276 bytes

When I opened the data.txt it display ccccccccccccccccccccccccccccccc, because i made the bottom code to test it. I want to read it back later, and put the results into another 3d array, but I want to make sure the write method working first.
		for (int f = 0; f < data.length; f++) {
			for (int r = 0; r < data[f].length; r++) {
				for (int c = 0; c < data[f][r].length; c++) {
					elements = data[f][r][c];
					out.writeInt(99);
					out.writeInt(999);
					out.writeInt(elements);
					System.out.print(elements + " ");

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: writeInt problem within the array.

Posted 13 January 2013 - 06:51 PM

Sure, the writeInt() method writes the 4 bytes of a int to the file

hardcode:

array[0][0][0] = 0x44434241;

before your for() loops and you should see "ABCD" in the file because the ASCII value of A B C and D are in hex 41 42 43 44
Was This Post Helpful? 0
  • +
  • -

#9 DaiToBu  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 17-March 12

Re: writeInt problem within the array.

Posted 13 January 2013 - 07:03 PM

View Postpbl, on 13 January 2013 - 06:51 PM, said:

Sure, the writeInt() method writes the 4 bytes of a int to the file

hardcode:

array[0][0][0] = 0x44434241;

before your for() loops and you should see "ABCD" in the file because the ASCII value of A B C and D are in hex 41 42 43 44


I don't quite understand this. Can you please help me what I should do to fix it?
Was This Post Helpful? 0
  • +
  • -

#10 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • Joined: 19-December 11

Re: writeInt problem within the array.

Posted 13 January 2013 - 07:07 PM

An output stream writes the bytes to the file, a writer on the other hand writes the actual character to the file. Like I said before, use a BufferedWriter
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: writeInt problem within the array.

Posted 13 January 2013 - 07:09 PM

The int are written as binary in the file, you cannot see the ASCII representation of these numbers using an editor.

So you need a program to read back these value.
As the ASCII value of A is 0x41

if you do

array[0][0][0] = 0x41414141; // store ASCII AAAA in the first int

when you will open your file using Notepad or an other editor you should see "AAAA" at the beginning of the file
Was This Post Helpful? 0
  • +
  • -

#12 DaiToBu  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 17-March 12

Re: writeInt problem within the array.

Posted 13 January 2013 - 07:21 PM

I think I get what you mean. The best way to test the results is to read it back instead of reading it in the file. I will try to test some more before posting any further question. Thanks you.
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: writeInt problem within the array.

Posted 13 January 2013 - 07:56 PM

If I were you I would be using an ObjectOutputStream and an ObjetInputStream to write/read the whole array (with all its dimensions) in a single read/write statement
Was This Post Helpful? 0
  • +
  • -

#14 DaiToBu  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 17-March 12

Re: writeInt problem within the array.

Posted 13 January 2013 - 09:49 PM

I wouldn't mind testing your idea, but my teacher want me to follow the way she layout the homework.
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: writeInt problem within the array.

Posted 14 January 2013 - 04:54 AM

I don't know how you will read back your array as you just save the data in it on alinear fashion. When you will read back you will get let says 1000 int to read but how will you lay them on a 3D array. An array[10][10][10] or [1000][1][1] or [1][100][10] or ... ?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2