Question

The program is supposed to calculate Prime Numbers using vectors and write the results out to a file. When I run it, it calculates the Prime Numbers from 2-64 and prints it out onto the screen. But when I check the text file it's creating, instead of the results, it writes out some other data into the file. What am I not doing right here?

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

public class PrimeByVector
{
        public static void main(String[] args)
        {
        try
        {
                ObjectOutputStream OutputS = new ObjectOutputStream(new FileOutputStream("PrimeNumbers.txt"));
                DataOutputStream DataOS = new DataOutputStream(OutputS);
                final int NUMBER_PER_LINE = 10;
                int count = 0;
        //      java.util.Vector vector = new java.util.Vector();
                Vector v = new Vector();
                System.out.println("The primes before 64 are: \n");

                for (int n = 2; n < 64; n++)
                {
                        boolean isPrime = true;
                        for (int i = 0; i < v.size(); i++)
                        {
                                int primeNumber =
                                        ((Integer)(v.elementAt(i))).intValue();
                                if (primeNumber > Math.sqrt(n)) break;

                                if (n % primeNumber == 0)
                                {
                                        isPrime = false;
                                        break;
                                }
                        }
                        if (isPrime)
                        {
                                count ++;
                                v.addElement(new Integer(n));
                                if (count % NUMBER_PER_LINE == 0)
                                {
                                        System.out.println(n);
                                }
                                else
                                        System.out.print(n + " ");
                        }
                        DataOS.write(n);
                }
                DataOS.close();

                }
                catch(IOException e)
                {
                        System.out.println("Error!!!!!");
                }
        }
}
Was it helpful?

Solution

You're using a DataOutputSteam which is generally used to write out binary data. If you want the data written into a file in plan text form, then instantiate your output like so:

PrintWriter out = new PrintWriter(new FileWriter("PrimeNumbers.txt"));

Then, when it comes time to output, do this:

out.println(n);
out.flush();

And then close it when done like so:

out.close();

This will print one per line; not sure if this is exactly what you want.

Also, I noticed in your code that your writing to DataOS outside of the if statement that checks if a number is prime, meaning that you will be writing all numbers to the file and not just prime numbers.

If you need to follow the same convention in the file as you do for System.out, then change the code to look like this:

if (count % NUMBER_PER_LINE == 0)
{
    System.out.println(n);
    out.println(n);
}
else{
    System.out.print(n + " ");
    out.print(n + " ");
}
out.flush();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top