1

The code that I'm writing has two classes: writeInts and readInts. I wrote writeInts to randomly generate 100 numbers between 0 and 1000 and output them to a data.dat file.

readInts is supposed to open a DataInputStream object and read in the "raw" data from the data.dat file and store the 100 integers in an array. My problem is that I can't seem to read the data correctly. Any help with this would be greatly appreciated. Thanks!

writeInts:

import java.io.*;


public class WriteInts {
    public static void main(String[] args) throws IOException {
        DataOutputStream output = new DataOutputStream(new FileOutputStream("data.dat"));
        int num = 0 + (int)(Math.random());
        int[] counts = new int[100];
        for(int i=0; i<100; i++) {
            output.writeInt(num);
            counts[i] += num;

            System.out.println(num);
        }
        output.close();
    }

}

readInts:

import java.io.*;
import java.util.*;

public class ReadInts {
    public static void main(String[] args) throws IOException {

        // call the file to read
        Scanner scanner = new Scanner(new File("data.dat"));
        int[] data = new int[100];
        int i = 0;
        while (scanner.hasNextInt()) {
            data[i++] = scanner.nextInt();

            System.out.println(data[i]);

            scanner.close();
        }

    }

}
7
  • My problem is that I can't seem to read the data correctly What happens? Commented Apr 17, 2014 at 19:31
  • You didnt provide ReadInt class that is problematic. How we are supposed to help you ? Commented Apr 17, 2014 at 19:33
  • writeInt() saves 4 bytes to the output file. Are you reading in ASCII or binary? Commented Apr 17, 2014 at 19:34
  • Sorry, added. I'm reading in binary. Commented Apr 17, 2014 at 19:36
  • 1
    int [] counts has no purpose in this example Commented Apr 17, 2014 at 19:50

3 Answers 3

4

If you want to write binary data, use DataInputStream/DataOutputStream. Scanner is for text data and you can't mix it.

WriteInts:

import java.io.*;

public class WriteInts {
    public static void main(String[] args) throws IOException {
        DataOutputStream output = new DataOutputStream(new FileOutputStream(
                "data.dat"));

        for (int i = 0; i < 100; i++) {
            output.writeInt(i);
            System.out.println(i);
        }

        output.close();
    }

}

ReadInts:

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class ReadInts {
    public static void main(String[] args) throws IOException {
        DataInputStream input = new DataInputStream(new FileInputStream(
                "data.dat"));

        while (input.available() > 0) {
            int x = input.readInt();
            System.out.println(x);
        }

        input.close();
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

This makes a lot more sense than what I was doing. Thank you!
You are welcome! At least you give it a try yourself and it's enough for me.
1

More. If you want to generate a random number in range from 0 to 1000 (both inclusive), you use this statement:

int rndNum = (int) (Math.random() * 1001);

It works that way: Math.random() generates a double in range from 0 to 1 (exclusive), which you then should map to integer range and floor. If you want you maximal value to be 1000, you multiply it by 1001 - 1001 itself is excluded.

Yep, like that:

import java.io.*;

public class WriteRandomInts {
    public static void main(String[] args) throws IOException {
        DataOutputStream output = new DataOutputStream(new FileOutputStream(
                "data.dat"));

        for (int i = 0; i < 100; i++) {
            int rndNum = (int) (Math.random() * 1001);
            output.writeInt(rndNum);
            System.out.println(rndNum);
        }

        output.close();
    }
}

3 Comments

Would this need to be declared before the 'for' statement?
Inside it! Because on every loop step you have to generate a new random number.
Oh, I see. Then I would just need to change System.out.println(i); to print rndNum instead.
0

I'd recommend that you abandon DataInputStream and DataOutputStream.

Write the ints one to a line using FileWriter and read them using a BufferedReader, one per line. This is an easy problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.