mrjigglejam
mrjigglejam

Reputation: 11

Problems Sorting a two column array and outputting value frequency in Java

Here is the problem I have, I spent a long time toying with for loops and arrays and temp variables, and now my output is just a couple numbers.

/* Write a program that reads numbers from the keyboard into an array of type int[]. You may assume that there will be 50 or fewer entries in the array. Your program allows any number of numbers to be entered, up to 50. The output is to be a two-column list. The first column is a list of the distinct array elements; the second is the count of the number of occurrences of each element. The list should be sorted on entries in the first column, largest to smallest.

For the array: -12, 3, -12, 4, 1, 1, -12, 1, -1, 1, 2, 3, 4, 2, 3, -12 the output should be: N Count 4 2 3 3 2 2 1 4 -1 1 -12 4 */

import java.util.Scanner;

public class Project2C {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int[][] twoColumn = new int[2][50];
        int[] inputValues = new int[50];
        int temp = 0;
        int valueFrequency = 0;
        int lastUsedSpace = 0;

        //gather user input to fill an array (up to 50 values);
        System.out.println("Input up to 50 values.");
        for (int i = 0; i < 50; i++) {
            System.out.println("value #" + (i + 1) + ":");
            inputValues[i] = keyboard.nextInt();
            /*System.out.println("Press 0 to stop, or 1 to continue.");
             if (keyboard.nextInt() == 0) {
             break;

             } 
             else if (keyboard.nextInt() == 1){
             continue;
             }
             else if (keyboard.nextInt() != 0 && keyboard.nextInt() != 1) {
             System.out.println("You must enter 0 or 1. Now you must re-enter the value.");
             i--;
             }*/
        }

        // checking if each value occurs more than once, and assigning it a place
        // in the two column array if it is unique.
        for (int i = 0; i < inputValues.length; i++) {
            for (int j = 0; j < inputValues.length; j++) {
                if (i == 0 && inputValues[i] != inputValues[j]) {
                    twoColumn[0][lastUsedSpace] = inputValues[i];
                } else if (i > 0 && inputValues[i] != inputValues[j]) {
                    twoColumn[0][lastUsedSpace + 1] = inputValues[i];
                }
            }
        }
        lastUsedSpace = -1;
        //Sorting the first column of the two column array
        for (int i = 0; i < twoColumn.length; i++) {
            for (int j = 0; j < twoColumn.length; j++) {
                if (twoColumn[0][i] < twoColumn[0][j + 1]) {
                    temp = twoColumn[0][j + 1];
                    twoColumn[0][j + 1] = twoColumn[0][i];
                    twoColumn[0][i] = temp;
                }
            }
        }
        // filling in the frequency column of the array
        for (int i = 0; i < inputValues.length; i++) {
            for (int j = 0; j < inputValues.length; j++) {
                if (inputValues[i] == inputValues[j]) {
                    valueFrequency = valueFrequency + 1;
                }
                if (j <= inputValues.length - 1 && lastUsedSpace == -1) {
                    lastUsedSpace = 0;
                    twoColumn[1][0] = valueFrequency;
                    valueFrequency = 0;
                } else if (j <= inputValues.length - 1 && lastUsedSpace > -1) {
                    twoColumn[1][lastUsedSpace + 1] = valueFrequency;
                    valueFrequency = 0;
                }

            }

        }
        //printing output
        for (int i = 0; i < twoColumn.length; i++) {
            System.out.println("Input    Frequency");
            System.out.println(twoColumn[0][i]+"    "+twoColumn[1][i]);
            }
        }
    }

}

Upvotes: 0

Views: 1493

Answers (3)

mrjigglejam
mrjigglejam

Reputation: 11

package project2c;

import java.util.Scanner;

public class Project2C {

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    int valueAmount = 0;
    int temp = 0;
    int valueFrequency = 1;

    //gather user input to fill an array (up to 50 values);
    System.out.println("how many values would you like to process?");
    valueAmount = keyboard.nextInt();
    int[] inputValues = new int[valueAmount];
    for (int i = 0; i < valueAmount; i++) {
        System.out.println("value #" + (i + 1) + ":");
        inputValues[i] = keyboard.nextInt();

    }
    //sort values in descending order
    for (int i = 0; i < inputValues.length - 1; i++) {
        for (int j = 0; j < inputValues.length - 1; j++) {
            if (inputValues[j + 1] > inputValues[j]) {
                temp = inputValues[j + 1];
                inputValues[j + 1] = inputValues[j];
                inputValues[j] = temp;
            }
        }
    }
    //print out put
    System.out.println();
    System.out.println("Value  Frequency");
    for (int i = 0; i < inputValues.length - 1; i++) {
        if (inputValues[i] == inputValues[i + 1]) {
            valueFrequency = valueFrequency + 1;
        } else if (inputValues[i] > inputValues[i + 1]) {
            System.out.println(inputValues[i] + "     " + valueFrequency);
            valueFrequency = 1;
        }

    }
}

}

Upvotes: 0

George O.
George O.

Reputation: 64

In case you are not playing with loops and wish to solve the problem on a higher-level, you could use a TreeMap and NavigableMap. See example below.

// ArrayGroupByCount.java
package com.geoloo.array;

import java.util.HashMap;
import java.util.NavigableMap;
import java.util.Scanner;
import java.util.TreeMap;

/*
 * Description: Display occurence of entered numbers in descending order
 * Sample input/output:
        Input up to 50 values. 0 to exit
        value #1:-12
        value #2:3
        value #3:-12
        value #4:4
        value #5:1
        value #6:1
        value #7:-12
        value #8:1
        value #9:-1
        value #10:1
        value #11:2
        value #12:3
        value #13:4
        value #14:2
        value #15:3
        value #16:-12
        value #17:0
        map: {1=4, 2=2, 3=3, 4=2, -12=4, -1=1}
        nmap: {4=2, 3=3, 2=2, 1=4, -1=1, -12=4}
 */

public class ArrayGroupByCount {

    public static void main(String[] args) {

        Integer input = 0;

        Scanner keyboard = new Scanner(System.in);
        HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
        TreeMap<Integer, Integer> treemap = new TreeMap<Integer, Integer>();

        System.out.println("Input up to 50 values. 0 to exit");
        for (int i = 0; i < 50; i++) {
            System.out.print("value #" + (i + 1) + ":");
            input = (int)keyboard.nextInt();

            if(input==0){
                break;
            }
            int content = 0;

            if(map.containsKey(input))
                content = map.get(input);

            map.put(input, content+1);
        } 

        keyboard.close();
        treemap.putAll(map);
        NavigableMap<Integer, Integer> nmap=treemap.descendingMap();

        System.out.println("map: "+map);
        System.out.println("nmap: "+nmap);
    }
}

Upvotes: 0

user4308987
user4308987

Reputation:

there I tested and fixed it you should take out the -999 jazz if you want the user to have to go through the whole 50

import java.util.Arrays;

import java.util.Scanner;


public class swinging {
static Scanner keyboard = new Scanner(System.in);
static int[] inputValues = new int[50];
int temp = 0;
int valueFrequency = 0;
int lastUsedSpace = 0;
public static void main(String[] args){
int j = 0;
 for (; j < 50; j++) {
        System.out.println("value #" + (j + 1) + ":");
        inputValues[j] = keyboard.nextInt();
       if(inputValues[j]==-999)break;
    }
    theValues= bubbleSort(Arrays.copyOf(inputValues, j));

        for (int i = 0; i < theValues.length; i++) {
            System.out.println("Input    Frequency");
            System.out.println(theValues[i]+"    "+howMany[i]);
     }
    }
    static int[] theValues;
    static int[] howMany;
    public static int[] bubbleSort(int[] Is ){
    boolean switchedOne=true;
    int temp;
    howMany=new int[Is.length];
    Arrays.fill(howMany,1);
    int length=Is.length-1;
    while(switchedOne){switchedOne=false;
    for(int i=0;i<length;i++){
    if(Is[i]>Is[i+1]){temp=Is[i];Is[i]=Is[i+1];Is[i+1]=temp;switchedOne=true;
                       temp=howMany[i];howMany[i]=howMany[i+1];howMany[i+1]=temp;}
    if(Is[i]==Is[i+1]){Is=removeElement(Is,i+1);howMany=removeElement(howMany,i+1);howMany[i]++;length--;}
    }
    }
    return Is;
    }

    public static int[] removeElement(int[] Is,int index){
    for(int i=index;i<Is.length-1;i++){Is[i]=Is[i+1];}
    return Arrays.copyOf(Is,Is.length-1);
    }}

Upvotes: 0

Related Questions