BenniMcBeno
BenniMcBeno

Reputation: 2445

Using the arrays index values from one for-loop in another

I am trying to count the number of times a unique number exists in my array, the amount of indexes used is subject to the amount of elements entered. The is mostly operational besides 1.the first value isn't being taken into consideration.The loop is checking arrays.length -1 so the number 3 is showing up as a count of 1 even though I entered 3 twice. I know the best way to fix this is run a loop that doesn't use arrays.length -1 but then I wouldn't be able to compare an entry with the one next to it like if(a[i] == a[i + 1] && a[i] != 0) to see if there is more then one occurrence of a value. I think the best thing would be to store the count values in my count method with its corresponding array value that was counted and then do a for loop outside of the method is this possible ? I can't see a way of doing it as I am rather new to java. may I have some guidance :)

import java.util.Scanner;


public class Prac_ExeOne 
{
        static int count  = 1;
        static int numberUsed = 0; // static its used to show its a class wide variable and there is only one copy.
        static int[] Array =  new int [50]; // the maximum elements inside the array that can be used is 10;
        int size;

              public int fillArray(int[] a)
         {
                 System.out.println("Enter up to " + a.length + " nonnegative numbers.");
                 System.out.println("Mark the end of the list with a negative number.");
                 Scanner keyboard = new Scanner(System.in);

                 int next;
                 int index = 0;
                 next = keyboard.nextInt();
                 while ((next >= 0) && (index < a.length ))  
                 {
                     numberUsed++;
                     a[index] = next;
                     index++;
                     // Print out each value of next
                     System.out.println(next);
                     next = keyboard.nextInt();
                     //System.out.println("Number of indexes used" + numberUsed);
                 }
                 keyboard.close(); // close the keyboard so it can't continue to be used.
                 return index;
        }


            public int[] sort(int[] arrays)
            {

                for(int i = 0;i < arrays.length -1 ;i++ )
                {

                    int store = 0;
                    // Move Larger Values to the right.
                    if (arrays[i + 1 ] < arrays[i])
                    {
                        store = arrays[i];
                        arrays[i] = arrays[i + 1];
                        arrays[i + 1] = store;
                    }
                    // Sort swapped smaller values to the left.
                        for(int j = i; j > 1; j--)
                        {
                           if (arrays[j] < arrays[j - 1])
                           {
                           store = arrays[j];
                           arrays[j] = arrays[j - 1];
                           arrays[j - 1] = store;
                           }
                      }
                }
                return arrays;

            }
            public void count(int[] a)
            {   

                //for each element in array go through if conditons.
                System.out.println("N " + "Count");
                for(int i = 0;i < a.length -1;i++)
                {   
                    if(a[i] == a[i + 1] && a[i] != 0)
                    {
                        count++;

                    }
                    if(a[i] != a[i+1])
                    {
                        count = 1;
                    }
                    if (a[i] !=  0)
                     {
                    System.out.println(a[i] + " " + count);
                     }
                }
            }

            public static void main(String[] args) 
            {
                Prac_ExeOne score = new Prac_ExeOne();
                score.fillArray(Array);
                score.sort(Array);
                score.count(Array);


            }
        }

Input:

Enter up to 50 nonnegative numbers.
Mark the end of the list with a negative number.
3
3
2
2
-2

Output:

N Count
3 1
2 2
2 1

Desired Result:

In a nutshell I want the program to count the values correctly then display the value on the left under N and the amount of times its in the array one the right underneath Count

Upvotes: 0

Views: 1725

Answers (3)

Bernhard Barker
Bernhard Barker

Reputation: 55609

For the sort function,

for (int j = i; j > 1; j--)

should be

for (int j = i; j > 0; j--)

I put System.out.println(Arrays.toString(Array)); after the call to sort and saw a 3 in the beginning, which led me to the fact that you're skipping the first element.

Note that there are also way more efficient sorting algorithms.

For the count function, you're resetting count at the wrong time and printing too often. I changed it as follows:

public void count(int[] a)
{
    //for each element in array go through if conditions.
    System.out.println("N " + "Count");
    for (int i = 0; i < a.length - 1; i++)
    {   
        if (a[i] != 0)
        {
            if (a[i] == a[i + 1])
            {
                count++;
            }
            // if the next element is different, we already counted all of the
            //   current element, so print it, then reset the count
            else
            {
                System.out.println(a[i] + " " + count);
                count = 1;
            }
        }
    }
    // we haven't processed the last element yet, so do that
    if (a[a.length-1] != 0)
        System.out.println(a[a.length-1] + " " + count);
}

Upvotes: 1

Manitra
Manitra

Reputation: 141

If you really want to use array with a unique counter you can use the code bellow:

public class Prac_ExeOne {
    static int count = 1;
    static int numberUsed = 0; // static its used to show its a class wide variable and there is only one copy.
    static Integer[] Array = new Integer[50]; // the maximum elements inside the array that can be used is 10;
    int size;

    public int fillArray(Integer[] a) {
        System.out.println("Enter up to " + a.length + " nonnegative numbers.");
        System.out.println("Mark the end of the list with a negative number.");
        Scanner keyboard = new Scanner(System.in);

        int next;
        int index = 0;
        next = keyboard.nextInt();
        while ((next >= 0) && (index < a.length)) {
            numberUsed++;
            a[index] = next;
            index++;
            // Print out each value of next
            System.out.println(next);
            next = keyboard.nextInt();
            // System.out.println("Number of indexes used" + numberUsed);
        }
        keyboard.close(); // close the keyboard so it can't continue to be used.
        return index;
    }

    public Integer[] sort(final Integer[] arrays) {
        Arrays.sort(arrays, new Comparator<Integer>() {

            @Override
            public int compare(Integer int1, Integer int2) {
                if (null != int1 && null != int2) {
                    if (int1 < int2) {
                        return -1;
                    } else if (int1 > int2) {
                        return 1;
                    } else  {
                        return 0;
                    }
                }
                return 0;
            }

        }); 
        return arrays;

    }

    public void count(Integer[] a) {

        // for each element in array go through if conditons.
        System.out.println("N " + "Count");
        for (int i = 0; i < a.length - 1; i++) {
            if (i == 0 && a[i] != null) {
                System.out.println(a[i] + " " + count);
            }
            if (i > 0 && (a[i] != null && a[i - 1] != null)) {
                if (a[i] == a[i - 1]) {
                    count++;    
                }
                if (a[i] != a[i - 1]) {
                    count = 1;
                }
                if (a[i] != 0) {
                    System.out.println(a[i] + " " + count);
                }
            } else {
                count = 1;
            }
        }
    }

    public static void main(String[] args) {
        Prac_ExeOne score = new Prac_ExeOne();
        score.fillArray(Array);
        score.sort(Array);
        score.count(Array);        
    }
}

Result:

Enter up to 50 nonnegative numbers.

Mark the end of the list with a negative number.

3

3

2

2

-1

N Count

2 1

2 2

3 1

3 2

your code didn't work because of your sort method. (Sorry for my bad English)

Upvotes: 1

user1907906
user1907906

Reputation:

In a nutshell I want the program to count the values correctly

Count in a Map:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class App {

    public static void main(String[] args) throws Exception {
        List<Integer> ints = new ArrayList<>();
        ints.add(3);
        ints.add(3);
        ints.add(2);
        ints.add(2);
        ints.add(-2);
        ints.add(5);

        Map<Integer, Integer> counts = new HashMap<>();

        for (Integer i : ints) {
            if (i < 0) {
                break;
            }
            if (counts.containsKey(i)) {
                counts.put(i, counts.get(i) + 1);
            } else {
                counts.put(i, 1);
            }
        }

        System.out.println(counts);
    }
}

Output:

{2=2, 3=2}

Upvotes: 1

Related Questions