Hey there guys, first post here :).

The problem I have is regarding 2D arrays ofcourse. So I have a .dat file that is 1000x1000 in size, I then need to find what the int is given a specific x,y co-ordinate. I scarcely know arrays and have no idea on how to read in a file into java. This is part of some group work I have at school but me and the other guy aren't sure what to do :S.

The code I have for this is nothing because I tried the sun website and they don't detail 2D arrays at all :S.

If someone could point me in the right direction and get me started I will spin as much gold as you want and give you my first born(...JK)!!!

Thanks for reading,
Regards: Ryan T

If the data are inside a file then your problem is not with 2D arrays, but with reading a file.
Anyway, in what format are the data inside the file?
Like this for example?

File:

10 20 ..
8 9 ..
-5
.
.

Is your problem reading the data from the file? Have you been asked to read the file inside an array and then print the data or take them directly from the file. The latter will not require a 2D array. For further info about reading files check tutorials about the Scanner class in this forum or other places on the internet


Also about the 2D arrays, and assuming that you are familiar with 1D arrays:
2D arrays don't exist in java. What java actually does in order to "represent" 2D arrays is have a 1D array and put inside it other arrays:

This simple example:

String [][] sArray = new String[3][2];

sArray[0][0] = "1";
.... 
sArray[2][1] = "6";

Is like saying that sArray has length 3, and each of its elements is an array of length 2:

String [][] array = new String[3][];

array[0] = new String[2];
array[1] = new String[2];
array[2] = new String[2];

array[0][0] = "1";
....
array[2][1] = "6";

Meaning that 2D arrays in java don't need to be rectangular. You can have arrays like this:

1 9
2 -4 6 5
1

int [][] ar = new int[3][];

ar[0] = new int[2]; // 1,9
ar[1] = new int[4]; // 2,-4,6,5
ar[2] = new int[1]; // 1

ar[0][0]=1;ar[0][1]=9;
ar[1][0]=2;ar[1][1]=-4;ar[1][2]=6;ar[1][3]=5;
ar[2][0]=1

At the above example:
ar is an array with 3 elements
ar[0] is an array with 2 elements
ar[1] is an array with 4 elements
ar[2] is an array with 1 elements
ar[1][2] is an int

So in order to loop such an array you will do this:

for (int i=0;i<array.length;i++) {
   for (int j=0;j<array[i].length;j++) {
            array[i][j];
   }
}

But the above are very complex. Usually you will have to deal with such arrays.
All your 2D arrays would probably be like this:

float [][] ff = new float[4][5];

Edited 6 Years Ago by javaAddict: n/a

Hey jA -- thanks so much for the above, absorbed thoroughly!

The data is inside a .dat file and when I open it up with notepad I just get lots of random characters, however, each of these is supposed to be an int so I assume if Java reads this file it should see it how it's meant to be I'm just gonna pop open info about Scanner that you mentioned.

Thanks!

If it does not contain pure numbers like this:

1 2 3 4
5 6 7 ......

then the file contains a serialized object.

Can you post part of the file's content?
Who gave you this file?
Where you told how it was created?
What object does it have inside?

When you will learn how to create objects, if you have them implement the java.io.Serializable interface, you can save into files and read them like this:
http://java.sun.com/j2se/1.5.0/docs/api/java/io/ObjectInputStream.html
http://java.sun.com/j2se/1.5.0/docs/api/java/io/ObjectOutputStream.html

try {
        FileInputStream fis = new FileInputStream("filename");
        ObjectInputStream ois = new ObjectInputStream(fis);
   
        Object obj = ois.readObject();

         // OR IN YOUR CASE
         // int [][] array = (int [][])ois.readObject();

        ois.close();
} catch (Exception e) {
  System.out.println("Error: "+e.getMessage());
}

But first you need to answer me the above questions.
What kind of file do you have?

If it does not contain pure numbers like this:

then the file contains a serialized object.

Can you post part of the file's content?
Who gave you this file?
Where you told how it was created?
What object does it have inside?

When you will learn how to create objects, if you have them implement the java.io.Serializable interface, you can save into files and read them like this:
http://java.sun.com/j2se/1.5.0/docs/api/java/io/ObjectInputStream.html
http://java.sun.com/j2se/1.5.0/docs/api/java/io/ObjectOutputStream.html

try {
        FileInputStream fis = new FileInputStream("filename");
        ObjectInputStream ois = new ObjectInputStream(fis);
   
        Object obj = ois.readObject();

         // OR IN YOUR CASE
         // int [][] array = (int [][])ois.readObject();

        ois.close();
} catch (Exception e) {
  System.out.println("Error: "+e.getMessage());
}

But first you need to answer me the above questions.
What kind of file do you have?

The contents of the file look like this:
ç # 4 Ç h é Ç + í V Ä Ú " a û < + \ Ò È ç N Ö Ã Ð Þ j œ î ß z ’ j ì Þ ± × Ï $ õ < 0 ½

Apparently it has been written in Java as an integer 2D array. 1000x1000

I tried both types of the above, without the array part it gives a long string of unreadable text. Using the array It gives me a stream error code in the console .

Can you try this and tell me what it prints?

try {
        FileInputStream fis = new FileInputStream("filename");
        ObjectInputStream ois = new ObjectInputStream(fis);
   
        Object obj = ois.readObject();

         System.out.println(obj.getClass());

        ois.close();
} catch (Exception e) {
  System.out.println("Error: "+e.getMessage());
}

Also I would like to know the complete assignment description

I think I did this correct but I'm getting the error, invalid stream header.

import java.io.*;


public class ACW5 {
	   public static void main (String [] args){
	      try {
	   
	      FileInputStream fis = new FileInputStream("/home/users/traceyr/SetiWorkUnitData.dat");
	   
	      ObjectInputStream ois = new ObjectInputStream(fis);
	   
	       
	   
	      Object obj = ois.readObject();
	   
	       
	   
	      System.out.println(obj.getClass());
	   
	       
	   
	      ois.close();
	  
	      } catch (Exception e) {
	  
	      System.out.println("Error: "+e.getMessage());
	  
	      	}
	  }    
}

Can you post your complete assignment?

Usually for these kind of exercises, you read the file into an object (2D array) and then you can access the data, or read them directly from the file.
But the file as you know doesn't have integers inside it. From what I see it contains binary data inside, so maybe that is what your teacher wants. To read the bytes of the file the FileInputStream class is used.

Knowing the complete description of your requirements will help me help you.

If you have read the assigment more carefully you would have seen that it says to use the classes:
FileInputStream and DataInputStream
Check their API.

You will create a FileInputStream instance with the file as argument and the you will create a DataInputStream instance with the previous FileInputStream instance as argument at the constructor. Don't be bothered that DataInputStream takes as parameter a InputStream, FileInputStream extends that class.

Then by using the readInt method of the DataInputStream you can read the int numbers that you want.

Also there is a method that DataInputStream inherites:

public int available()
throws IOException
Returns the number of bytes that can be read from this input stream without blocking.

Every time you call that it returns how many bytes are left to read. Don't worry that you are using the readInt method, the available will do its work.

So inside a while loop, you can read one int after the other with each loop. Use the available method to determine when the while loop will stop:

while (HAS AVAILABLE BYTES) {
   int i = dataInputStream.readInt(); 
}

My problem is that if you use the above, you will read integers in a sequence, but you said that want to know the int that is found at a specific coordinates.
Maybe you will read the enitre file and write its contents into an array so you can reuse it, but for that I didn't find any information.
The assignment said:

each work unit has a stamp of "111" positioned in DataArray[100][110]

But I didn't understand that. Maybe you need to ask your teacher

This article has been dead for over six months. Start a new discussion instead.