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

Thread: Hello, I'm new and I need help. . .

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    1
    My Mood
    Nerdy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile Hello, I'm new and I need help. . .

    I want my program to be able to read an array of integer type and count the frequency of each integer in the array. For example, if my array consist of 10 components = {1,1,2,3,1,2,1,1,5,4}, I need my program to be able to say that there are five 1's, two 2's, one 3, one 4, and one 5. I could only go as far as the first occurring integer, something is wrong with my second outer for loop, I think

    Here's my code so far:
    import java.util.*;
     
    public class Yellow
    {
    	static Scanner console = new Scanner(System.in);
    	public static void main(String[] args)
    	{
    		int numOfElem;
    		System.out.println("Enter how many integers you want to input into the array: ");
    		numOfElem = console.nextInt();
    		System.out.println();
    		int[] list = new int[numOfElem];
    	//	int i;
    		int freq = 1;
    		System.out.println("Enter " + numOfElem + " integers: ");
    		for (int i = 0; i < numOfElem; i++)
    			list[i] = console.nextInt();
    		System.out.println();
     
    		for (int i = 0; i < numOfElem; i++)
    		{
    			System.out.print(list[i] + " ");
    			int searchItem = list[i];
    			for (i = i + 1; i < numOfElem; i++)
    			{
    				if (list[i] == searchItem)
    					freq++;
    				else
    					freq = freq;
    			}
    			System.out.print("   " + freq);
    			System.out.println();
    		}
    		System.out.println();
    	}
    }

    Thanks for any help.

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Hello, I'm new and I need help. . .

    something is wrong with my second outer for loop, I think
    For future reference it helps to describe a bit more about what is wrong...for example the input you give and the output you get. That being said, there is an issue with your second loop...mainly it is using the same int variable as the first - once its finished it will go back to the first loop, which then exits (i will be equal to numOfElem). A quick fix is to use a different int variable in the second loop.

  3. The Following User Says Thank You to copeg For This Useful Post:

    YellowCanary (March 6th, 2011)