0

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]);
            }
        }
    }

}
3
  • 3
    What is your problem? Is this your homework? Commented Dec 12, 2014 at 3:31
  • @Fumu7 This definitely seems like homework to me. SMH. Vote Down! Commented Dec 12, 2014 at 4:12
  • 1
    It was Homework, I didn't realize that was a no-no here. In any case, I ended up solving the problem in a much simpler way than anything here, I just use a single for loop. I was making it much too complicated. I'll post some code in a bit. Commented Dec 14, 2014 at 18:11

3 Answers 3

0

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);
    }}
Sign up to request clarification or add additional context in comments.

1 Comment

You should test what you wrote, and properly format it.
0

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);
    }
}

Comments

0

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;
        }

    }
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.