Results 1 to 3 of 3
  1. #1
    rsun is offline Member
    Join Date
    Jan 2009
    Posts
    1
    Rep Power
    0

    Default Java random names

    Java Code:
    import java.util.Scanner;
    import java.io.*;
    import java.util.Random;
    
    public class RandomName
    {
    	public static void main(String[] args) throws IOException
    	{
    
    		Scanner key = new Scanner(System.in);
    		Random randomNumbers = new Random();
    
    		int n = 0;
    		String[] names= new String[50];
    		int index = 0;
    		int count =0;
    
    
    		//Opens the file from user
    		//
    
    		File file = new File("name.txt");
    		Scanner outputFile = new Scanner(file);
    
    		while(outputFile.hasNext())
    		{
    			names[index] = outputFile.nextLine();
    
    			index++;
    
    			n++;
    		}
    
    
    		//prints out the number of people in the file
    		//Store into an array
    		//prints out a random name from the file
    
    		System.out.println("Number of people: " + n );
    		int x = randomNumbers.nextInt(n);
    
    		System.out.println(names[x]);
    		System.out.print("Command? ");
    		String command = key.nextLine();
    
    		//contine to loop the program
    		//Write to the file
    		//
    		while(!command.equals("exit"))
    		{
    			if(command.equals("n"))
    			{
    				x = randomNumbers.nextInt(n);
    				System.out.println(names[x]);
    				
    				System.out.print("Command? ");
    				command = key.nextLine();
    				count++;
    			}
    
    			else if(command.equals("help"))
    			{
    				System.out.println(" n    Next random name");
    				System.out.println(" exit Exit the program");	
    				System.out.print(" list List all the unique names that have been");
    				System.out.println(" called as well the number of times");
    				System.out.println(" help Display this message");
    				System.out.print("Command? ");
    				command = key.nextLine();
    			}
    			else if(command.equals("list"))
    			{
    				System.out.println(count);
    				System.out.print("Command? ");
    				command = key.nextLine();
    			}
    			
    	
    		}
    		outputFile.close();
    
    		//Exit the program if user enter exit
    		//
    
    		if(command.equals("exit"))
    		{
    			System.out.println("The program has generated " + count + " name(s) with " + "" + "repetition(s)");
    			System.exit(0);
    		}
    	}
    }
    Im trying to make it how many times a name comes up.
    for example everytime a person press n and enter:
    john
    alex
    john

    and when a person type list it will display
    john(2)
    alex(1)

    how will i do this?

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    you'll prob want to use hashmaps.

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Another comment...

    Remember, when using the Random class, the span of randomness is 0-(n-1). For example,

    Java Code:
    int x = randomNumbers.nextInt(10);
    ... wiil give you random numbers between 0-9 (10 is excluded).

    "Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)... ". For more info:
    Random (Java Platform SE 6)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Names pipes in java
    By Zept in forum Networking
    Replies: 2
    Last Post: 08-06-2011, 08:20 PM
  2. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  3. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM
  4. random numbers without using java funtions
    By carlos123 in forum New To Java
    Replies: 8
    Last Post: 11-16-2007, 10:13 AM
  5. How to generate random number in java
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 07:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •