Results 1 to 3 of 3
Thread: Java random names
- 01-15-2009, 06:18 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 1
- Rep Power
- 0
Java random names
Im trying to make it how many times a name comes up.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); } } }
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?
- 01-15-2009, 06:40 PM #2
you'll prob want to use hashmaps.
- 01-15-2009, 09:17 PM #3
Another comment...
Remember, when using the Random class, the span of randomness is 0-(n-1). For example,
... wiil give you random numbers between 0-9 (10 is excluded).Java Code:int x = randomNumbers.nextInt(10);
"Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)... ". For more info:
Random (Java Platform SE 6)
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Similar Threads
-
Names pipes in java
By Zept in forum NetworkingReplies: 2Last Post: 08-06-2011, 08:20 PM -
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 05:50 PM -
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01-17-2008, 10:44 PM -
random numbers without using java funtions
By carlos123 in forum New To JavaReplies: 8Last Post: 11-16-2007, 10:13 AM -
How to generate random number in java
By fernando in forum New To JavaReplies: 1Last Post: 08-01-2007, 07:32 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks