rsun
01-15-2009, 04:49 PM
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:
john
alex
john
and when a person type list it will display
john(2)
alex(1)
how will i do this?
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:
john
alex
john
and when a person type list it will display
john(2)
alex(1)
how will i do this?