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 4 of 4

Thread: how to write generated numbers in a file without repeatition

  1. #1
    Junior Member
    Join Date
    Aug 2017
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default how to write generated numbers in a file without repeatition

    import java.util.ArrayList;
    import java.util.Random;
    import java.io.FileWriter;
    import java.io.BufferedWriter;
    import java.io.IOException;
     
    public class Test {
     
        public static void main(String[] args)throws IOException {
            int size = 100;
     
            ArrayList<Integer> list = new ArrayList<Integer>(size);
            for(int i = 1; i <= size; i++) {
                list.add(i);
            }
     
            Random rand = new Random(System.currentTimeMillis());
            while(list.size() > 60) {
                int num = rand.nextInt(list.size());
                BufferedWriter writer = new BufferedWriter(new FileWriter("C:\\Users\\glenn\\Desktop\\trial.txt",true)); // with existing data
                writer.write(list.remove(num)+ " ");
                writer.newLine();
                writer.close();   
           } 
       }
    }

    hi to everyone, im a beginner in java , i need some help of you guys if any,
    i intend to write a generated numbers without repeatition, and my problem is,
    if i write it in file with existing data on it... repeatition of numbers occurs ....

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,381
    Thanks
    53
    Thanked 2,397 Times in 2,350 Posts

    Default Re: how to write generated numbers in a file without repeatition

    Without repetition implies that each newly generated number must be checked against the existing list of numbers to see if it is unique. One way to do that would be to use a Set which would guarantee uniqueness.

    If some of the numbers are in a file, that file would need to be read and saved before continuing.

    Note: Creating/opening and closing a file inside of a while loop is not good practice. The file should be opened before the loop is entered and closed after the loop is exited.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Glenn_ford7 (August 16th, 2017)

  4. #3
    Junior Member
    Join Date
    Aug 2017
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to write generated numbers in a file without repeatition

    Thanks, can u give me code samples on how it look like,? for me to develop this code.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,381
    Thanks
    53
    Thanked 2,397 Times in 2,350 Posts

    Default Re: how to write generated numbers in a file without repeatition

    Sorry I don't have any sample code.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. I need help to write encrypt and decrypt 4 digit numbers.
    By shin777 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 29th, 2013, 01:07 PM
  2. I need code to write the following Repetition Numbers Using Java
    By Esmael in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 23rd, 2012, 07:07 AM
  3. thought i had it, then i sneezed and lost it (randomly generated numbers)
    By fakeClassy in forum Java Theory & Questions
    Replies: 13
    Last Post: July 17th, 2011, 04:16 PM
  4. Trying to write Factoring Numbers Code--Please Help.
    By uks2h in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 11th, 2010, 07:20 PM
  5. How to write 2 dimensional array of float numbers to binary file?
    By Ghuynh in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: June 17th, 2010, 04:26 PM