Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Writing Integers to .txt File; Returning Random Characters Instead of Integers

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Writing Integers to .txt File; Returning Random Characters Instead of Integers

    Hi everybody,

    This project is a bit beyond the scope of my class right now, but it's relevant to what we are working on and I was interested so I figured I'd give it a shot. I'm trying to generate 100 random numbers and write them to a .txt file (to be used in another project I've written). Here is my code:

    import java.io.*;
    import java.util.Random;
     
    public class intToFile_main {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
    		generateFile("C:\\CSEclipseWorkspace\\intToFile\\numbers.txt");
    	}
     
    private static void generateFile(String fileName) {
     
    	try{
     
    		FileOutputStream fos = new FileOutputStream(fileName);
    		DataOutputStream dos = new DataOutputStream(fos);
     
    		Random generator = new Random();
     
    		for (int i = 0; i <= 99; i++) {
    			int num = generator.nextInt(99) + 1; //generate a random number
    			dos.writeInt(num); //write the number to the file
    		}
     
    		dos.close();
     
    		}
     
    		catch (IOException e)
    		{
    			e.printStackTrace();
    		} 
     
    	}
    }

    As far as I can tell, it's working, except that rather than displaying numbers, the document reads something like this (it's different every time because of the random number generator):

    ? > 8 0 S ( " _ Y 9 ( P R P ( @ W % W ? ? ; = C - K > S O 2 a \ L O U " X a
    Y $ ( + \ [ 7 # ? & # W c a X ; @ / 5 V / b I : V b G O $ Z 6 2 9 S -
    Does anyone have any idea as to what's going on and how I can fix it? Thanks very much!


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Writing Integers to .txt File; Returning Random Characters Instead of Integers

    Do you want to write out the string representation of the characters, or the actual bit values of the integer? What you have right now is writing out the actual bit values (which result in a file which looks like garbledy goop).

    Try creating a PrintWriter object instead of a DataOutputStream to your file and you can write out the string representation of the integers/other numbers.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    verbicidalmaniac (March 8th, 2011)

  4. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Writing Integers to .txt File; Returning Random Characters Instead of Integers

    It worked! Thank you so much!!!

Similar Threads

  1. Returning the equilibrium index in a sequence of integers
    By thanasisk in forum Algorithms & Recursion
    Replies: 4
    Last Post: September 3rd, 2013, 02:20 PM
  2. Determine the two smallest integers from a set of user input integers
    By bpontin in forum Loops & Control Statements
    Replies: 4
    Last Post: October 17th, 2010, 06:38 PM
  3. java integers
    By timeline in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 23rd, 2010, 02:54 AM
  4. [SOLVED] Writing integers to a file
    By dubois.ford in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 15th, 2010, 06:18 PM
  5. [SOLVED] Java program to convert and compare integers
    By luke in forum What's Wrong With My Code?
    Replies: 9
    Last Post: May 18th, 2009, 06:26 PM