I was doing a projecteuler problem.

I made a program that would find all the prime numbers under 1000000 and write them all in a file.

Why is that if I use the file name as "primes.txt", the result is gibberish from beginning to end, but when I use "primes.dat", the result is what I want.

Please explain. Thanks :D

How do you know that primes.txt file contains gibberish,
and that primes.dat file contains the results you want?
Are you creating a binary file or a text file?
Are you sure that they are both opening (so you can see the results)
with the same application?
Whatever you name the files would NOT have anything to do with the contents.

On Windows the file's extension controls what program opens the file.
Did the same program open and display both the .txt and the.dat file?

Create a .txt file and rename it to .dat and see what happens.
Also rename a .dat to a .txt and see.

The file is supposed to contain something like this

2 3 5 7 11 13 and so on.

I open both files with good ol' notepad, but it contains different things.
the txt file contain weird characters Ive never seen before, and the dat file shows what I want.

I also tried changing the txt file extension to dat. but when I open it, it still displays the same thing.

if I use the file name as "primes.txt", the result is gibberish from beginning to end, but when I use "primes.dat", the result is what I want.

Are you saying that the exact same code, classes and methods, write different data when you change part of contents of a String from .dat to .txt ? And that is the ONLY change you make to the program.

Can you post the code where you define and use the file name?

import java.io.*;

public class test {
	public static void main(String[] args){
		int[] array = new int[10000];
		int total = 2;		//counted index 0 and index 1
		for(int i=2; i<array.length; i++) array[i] = i;	//fill the array with the numbers (number = index)
		
		for(int i=2; i<array.length; i++)
			for(int j=i+1; j<array.length; j++)
				if(array[j] % i == 0 && array[j] != 0){
					array[j] = 0;
					total++;
				}
		
		int[] primes = new int[array.length-total];
		for(int i=2, counter=0; i<array.length; i++)
			if(array[i] != 0) {
				primes[counter] = array[i];
				counter++;
			}
		
		try{
			BufferedWriter out = new BufferedWriter(new FileWriter("primes.txt"));
			for(int i=0; i<primes.length; i++) out.write(primes[i] + " ");
			out.close();
		}catch(IOException e){System.out.println("Unable to comply!");}
		
		
		
		//for(int i=0; i<primes.length; i++) System.out.println(primes[i]);
	}
}

Nevermind... now it doesnt work for .txt or .dat

Can anybody point out the problem please. Thanks.

I already made an alternate code that is much more efficient than what Im doing right here. But im just curious why its giving me weird characters.

addition: It give me the output I want if I change the array size to only 100

Edited 5 Years Ago by efronefron: adding

Have you read the API doc for the write() method?

Try running your program and printing out the numbers as well as writing them to a file and compare what was printed with what is in the file.

The magic is what the compiler does with the number when you concatenate a String to it.
The int is converted to a String.

Edited 5 Years Ago by NormR1: Corrected

now it doesnt work for .txt or .dat

How do you get it to fail?
It works fine for me. I get all readable numbers

yes it gives me the same thing. I just read the API doc. I see that I cant write a string. But it works..... This is my new code(works perfectly)

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

public class problem35 {
	public static void main(String[] args){
		long start = System.currentTimeMillis();
		int[] array = new int[500000];
		int odd = 3;
		array[0] = 2;	//the only prime that is even
		
		//Fill the rest of the array with odd numbers
		for(int i=1; i<array.length; i++){
			array[i] = odd;
			odd+=2;
		}
		
		//eliminate the non-prime
		for(int i=0; i<array.length; i++){
			if(array[i] < array[array.length-1]/2){
				for(int j=i+1; j<array.length; j++)
					if(array[j] != 0 && array[j] % array[i] == 0){
						array[j] = 0;
					}
			}
			else break;
		}
		//eliminate if the any of the number is even
		String fnum;
		for(int i=1; i<array.length; i++){	//skip 2, because its the only allowed one
			if(containEven(array[i])){
				array[i] = 0;
			}
		}
		long total = 0;
		//write primes to file
		try{
			BufferedWriter out = new BufferedWriter(new FileWriter("primes.txt"));
			for(int i=2; i<array.length; i++) 
				if(array[i] != 0){
					out.write(array[i] + " ");
					total++;
				}
			out.close();
		}catch(IOException e){System.out.println("Unable to comply!");}
		
		
		long end = System.currentTimeMillis();
		
		System.out.println("\nThe program runs for" + (end-start) + "ms" + "\nPrimes -> " + total);
		//for(int i=0; i<primes.length; i++) System.out.println(primes[i]);
	}
	
	public static boolean containEven(int num){
		String numString = Integer.toString(num);
		int stringLength = numString.length();
		for(int i=0; i<stringLength; i++){
			if(Integer.parseInt(Character.toString(numString.charAt(i))) % 2 == 0){
				return true;
			}
		}
		return false;
	}
}

There must have been some other change you made when changing the .dat to .txt. Or the other way.
That would NOT have any effect on what was written.

Comments
http://stackoverflow.com/questions/6878881/moving-an-image-via-buttons/6879101#comment-8186167
This article has been dead for over six months. Start a new discussion instead.