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: Create 100 integers?

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Create 100 integers?

    Hi all.

    This program is meant to do the following:

    1: create a new txt file
    2: write 100 random integers to it
    3: read it back into an array
    4: sort the array
    5: print contents of array

    I have everything working apart from part 2. I can't get it to create 100 numbers. Sometimes it does 50, other times 62, 25, 99 etc.
    Not sure on how to fix this. Any help much appreciated.

    My code below.

     
    import java.io.*;
    import java.util.*;
     
    public class Q3
    {
    	public static void main(String[] args)
    	//Throw any exceptions
    	throws Exception
    	{
    		// Create file called Q3.txt
    		FileWriter newFileWriter = new FileWriter("Q3.txt");
    		BufferedWriter newFileOut = new BufferedWriter(newFileWriter);
     
    		//Random Number Generator
    		Random numberGenerator = new Random();
     
    		//Write 100 random numbers to Q3.txt
    		for (int i = 0; i < 100; i++)
    		{
    			//while number of numbers is less than 100, increment the counter
    			//and enter a random number to the file followed by a space
    			int randomNumber = numberGenerator.nextInt(100);
    			newFileOut.write(randomNumber + " ");
    		}
    		//Close the file writer
    		newFileOut.close();
     
    		//crate file scanner to read file
    		Scanner input = new Scanner(new File("Q3.txt"));
    		//create array with contents of the file
    		int[] array = new int[input.nextInt()];
    		//every next number is sent to next position on the array
    		for (int i = 0; i < array.length; i++)
    		    array[i] = input.nextInt();
    		//sort the array in ascending order
    		Arrays.sort(array);
    		//parse the array contents to a string and print the string
    		System.out.println(Arrays.toString(array));
    		//close the file reader
    		input.close();
    	}
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Create 100 integers?

    How are you determining that the correct number of integers is NOT being written to the file? Try inspecting the file, paying particular attention to the first number in the file.

    Then, consider the reason you're seeing variable results is because of this line:

    int[] array = new int[input.nextInt()];

    Put the two findings together to determine the root cause and design a fix.

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Create 100 integers?

    That helped, solved and working.

    Thank you sir.

Similar Threads

  1. calcuate and display the sum of even integers between 1 and 100.
    By mano in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 26th, 2013, 08:41 AM
  2. Replies: 2
    Last Post: October 25th, 2013, 06:31 AM
  3. How do I create a if statement that accepts only Integers?
    By Mnelson in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 19th, 2012, 04:00 PM
  4. Replies: 2
    Last Post: April 21st, 2011, 10:29 AM
  5. [SOLVED] Writing Integers to .txt File; Returning Random Characters Instead of Integers
    By verbicidalmaniac in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 8th, 2011, 09:42 PM