Im trying to make a counter that tells how many times a name appears with array. I am using a scanner that read the names from a file and stores it an array. It then uses random class to pick a random name. I then stored all the names that is chosen into an array. How do i make a counter for each name.
for example:
john
david
carl
john
then it prints out:
john(2)
david(1)
carl(1)
heres my code:

//The goal of this program is to generate random Strings

import java.util.Scanner;
import java.io.*;
import java.util.Arrays;
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 index2 =0;
		int count =0;
		int count2=0;
		int count3=0;
		String [] names2 = new String[5];
		int [] counter = new int[20];


		//Opens the file from user
		//

		File file = new File("name.txt");
		Scanner read = new Scanner(file);

		while(read.hasNext())
		{
			names[index] = read.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]);
		names2[index2] = names[x];
		count++;
		count2++;
		index2++;
		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]);
				names2[index2]=names[x];
				count2++;
				index2++;
										
				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"))
			{
				
				command = key.nextLine();
				System.out.print("Command? ");
				System.out.println(count);
			}
			
	
		}
		read.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);
		}
	}
}

thank you

Recommended Answers

All 4 Replies

My suggestion would be you keep an Integer (say "counter") array with the exact size as the total number of names you read from the file.
And when you select any name at random from that String array (in which you have your names stored originally), just increment the integer value at the corresponding index in the counter array.
For Ex:-

Assume the names in the original names array are :-
names[0] -> Aaragon
names[1] -> Gandalf
names[2] -> Frodo
names[3] -> Sam
.
.
.

And lets say you selected on random "Sam", So in the counter array just increment whatever value is at index -> "3"
So you will know which name has been selected how many times at the corresponding index of the name in the "counter" array.

Note:-
There are better methods available especially using the Collections library in Java, but since you are just starting out I have recommended this one.
Also I haven't really read your code, I have just replied to the question above it.

You could also make an Object that holds a name and an integer. The integer would be timesSeen. Then you'd only need one array. Either way works.

HashMap.

thank you guys for all the response. I did it using array with the counter like what stephen84s said. And it worked thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.