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

Thread: Generating random numbers between 999999 - 1000000 with an alpha number at the end

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Generating random numbers between 999999 - 1000000 with an alpha number at the end

    Hey every I'm stuck with trying to generate 1000 random numbers that have 7 digits my code is here below
    I don't know how to set it to a fix digit number, I know it's simple but it's doing my head in
    can anyone help me? thanks
    I want numbers between 0000001 - 9999999 if possible with a character at the end
    Ok the output is in a txt file

    Random PPS Number : 1911462N
    Random PPS Number : 2714102K
    Random PPS Number : 5070317E
    Random PPS Number : 8030837K
    Random PPS Number : 4358369S
    Random PPS Number : 5350002N

    but my problem is I don't know how to sort it in lexicographical order

    Grot

    import java.io.FileNotFoundException;
    import java.io.PrintWriter;
    import java.util.Random;
     
     
    public class Assignment_Part2{
     
    	/**
    	 * @param args
    	 */
     
    	 private static PrintWriter outputStream;
     
    	 public static void main(String[] args) {
    		 String fileName = "pps.txt";//input the data to a file named text.txt
    		 try {//using the try catch operator 
    		 	outputStream = new PrintWriter(fileName);// using the printwriter method to insert data to the file
    		 	Random r = new Random();
    		    String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    		    for (int i = 0; i < 1; i++) {
     
    		    } // prints  random characters from alphabet
    		 	for (int j = 1; j <= 1000; j++){// using a for loop to gernerate numbers from 9999999 - 1000000
    		 		int myInt = 1000000 + (int)(Math.random() * ((9999999 - 1000000) + 1));	
    		 		outputStream.println("Random PPS Number : "   + myInt + alphabet.charAt(r.nextInt(alphabet.length())) + " ");//output stream to print the numbers to the pps.txt file
    		 	    }  	 		
     
    		 	// stores in Ram First
    		 	outputStream.close(); // flushes the data to the file
     
    		 	System.out.println("PPS Numbers Are Printed Out In A Text File");//outputs to the user that it's done
     
    		 } catch (FileNotFoundException e) {// the catch operator is used to give the user an error 
     
    		 	System.out.println("File Not Found");//outputs this if an error is found
    		 }	
     
     
    	}
     
    }
    Last edited by Grot; November 29th, 2012 at 12:37 PM. Reason: making it more clearer for the viewers


  2. #2
    Junior Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Generating random numbers between 999999 - 1000000 with an alpha number at the end

    I edited it there for you thanks

    Grot

    --- Update ---

    import java.io.FileNotFoundException;
    import java.io.PrintWriter;
    import java.util.Random;
     
     
    public class Assignment_Part2{
     
    	/**
    	 * @param args
    	 */
     
    	 private static PrintWriter outputStream;
     
    	 public static void main(String[] args) {
    		 String fileName = "pps.txt";//input the data to a file named text.txt
    		 try {//using the try catch operator 
    		 	outputStream = new PrintWriter(fileName);// using the printwriter method to insert data to the file
    		 	Random r = new Random();
    		    String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    		    for (int i = 0; i < 1; i++) {
     
    		    } // prints  random characters from alphabet
    		 	for (int j = 1; j <= 1000; j++){// using a for loop to gernerate numbers from 9999999 - 1000000
    		 		int myInt = 1000000 + (int)(Math.random() * ((9999999 - 1000000) + 1));	
    		 		outputStream.println("Random PPS Number : "   + myInt + alphabet.charAt(r.nextInt(alphabet.length())) + " ");//output stream to print the numbers to the pps.txt file
    		 	    }  	 		
     
    		 	// stores in Ram First
    		 	outputStream.close(); // flushes the data to the file
     
    		 	System.out.println("PPS Numbers Are Printed Out In A Text File");//outputs to the user that it's done
     
    		 } catch (FileNotFoundException e) {// the catch operator is used to give the user an error 
     
    		 	System.out.println("File Not Found");//outputs this if an error is found
    		 }	
     
     
    	}
     
    }

    Fixed numbers I mean from 1000000 - 9999999
    I fixed it there.
    my problem is how do I sort it in lexicographical order

    Thanks

    Grot

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Generating random numbers between 999999 - 1000000 with an alpha number at the end

    how to sort it in lexicographical order
    Where are the numbers stored so you can sort them?

    What does the program output when it executes?
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Generating random numbers between 999999 - 1000000 with an alpha number at the end

    The numbers are stored in a txt file e.g
    Random PPS Number : 1911462N
    Random PPS Number : 2714102K
    Random PPS Number : 5070317E
    Random PPS Number : 8030837K
    Random PPS Number : 4358369S

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Generating random numbers between 999999 - 1000000 with an alpha number at the end

    To sort them, they need to be in some kind of data structure in the program like an array or a collection.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Nov 2012
    Location
    Ireland
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Generating random numbers between 999999 - 1000000 with an alpha number at the end

    Ok thanks Norm

Similar Threads

  1. Replies: 2
    Last Post: November 7th, 2012, 10:45 PM
  2. Generating a Random Coprime Number
    By phleep in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 15th, 2011, 12:12 AM
  3. Generating random numbers
    By abelned in forum Object Oriented Programming
    Replies: 1
    Last Post: September 1st, 2010, 07:24 AM
  4. Generating random letters and numbers together
    By newJava in forum Java Theory & Questions
    Replies: 3
    Last Post: March 19th, 2010, 04:08 AM
  5. Generation of Palindrome number in Java
    By tina.goyal in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 26th, 2009, 08:49 AM