1

I can't see where I'm going wrong with this one, I want it to display the frequencies of the survey's result but all I get is zeros, for example if I enter 1,1,2,2,5 I want it to output:

Displaying response frequencies...
1   2
2   2
3   0
4   0
5   1

but instead I get this:

Displaying response frequencies...
1   0
2   0
3   0
4   0
5   0

here is my main method:

import java.util.Scanner;
public class SurveyTester {

    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);

        System.out.println("How many people took the survey?");
        int numPeople = input.nextInt();
        Survey s = new Survey(numPeople);
        int nextResponse;
        for (int i = 0; i < numPeople; i++)
        {
            System.out.print("Input the next survey response (values 1-5):");

            nextResponse = input.nextInt();
            s.addResponse(nextResponse);
        }

        System.out.println("Displaying all responses...");
        s.displayAllResponses();
        System.out.println("Displaying response frequencies...");
        s.displayResponseFrequencies();
     }

}

here is my class:

public class Survey
{
    private int numResponses;
    private int maxResponses;
    private int[] responses;
    private int[] frequencies;
    private final int NUM_RESPONSES = 5;

    public Survey(int nResponses)
    {
        maxResponses = nResponses;
        numResponses = 0;
        responses = new int[nResponses];
        frequencies = new int[NUM_RESPONSES]; // There are 5 possible responses to the survey
    }

    public void addResponse(int response)
    {
        // This method stores the response
        // passed in the parameter 'response'
        // to the next free space in the array

        {
            responses[numResponses]= response;
        }

        numResponses++;
    }


    public void displayAllResponses()
    {
        // This method displays on the screen
        // all the responses to the survey

        for (int i=0; i<maxResponses; i++)
        {
        System.out.print(responses[i] + " ");
        }

        System.out.println("");
    }


    public void calculateResponseFrequencies()
    {
        // This method calculates the number of
        // responses for each possible answer
        // to the survey, 1, 2, 3, 4 or 5
        // It stores the frequencies in the
        // array 'frequencies'

        for (int i=1; i<=maxResponses; i++)
        {
            if (responses[i] == 1)
            {
                frequencies[1]++;
            }

            else if (responses[i] == 2)
            {
                frequencies[2]++;
            }

            else if (responses[i] == 3)
            {
                frequencies[3]++;
            }

            else if (responses[i] == 4)
            {
                frequencies[4]++;
            }

            else if (responses[i] == 5)
            {
                frequencies[5]++;
            }
        }
    }

    public void displayResponseFrequencies()
    {
        // This method displays the response
        // frequences for each of the possible
        // response, 1, 2, 3, 4 or 5

        for (int i=0; i<frequencies.length; i++)
        {
        System.out.println((i+1) + "\t" + frequencies[i]);
        }
    }

}
5
  • 1
    frequencies[3]++; can be frequencies[responses[i]]++;, eliminating the if statements (see also: switch statements). Jan 8, 2013 at 22:11
  • How are you writing and compiling your code? If you are using an IDE like NetBeans or Eclipse, I strongly encourage you to learn how to use the built-in debugger. Using it, you can step through your code one line at a time and view the values of variables. Alternatively you can add System.out.println() calls to your code to print out tracing information and values of variables. Debugging is a critical skill for any programmer. Jan 8, 2013 at 22:14
  • I'm using notepad++ and a internal compiler inside notepad++.
    – Waj
    Jan 8, 2013 at 22:16
  • @Code-Guru I will take your advice on board and as a result switch to Eclipse, thanks.
    – Waj
    Jan 8, 2013 at 22:33
  • @Kingboom4 Notepad++ is a great editor because it is so simple. If you want to stick with it, you can use my later suggestion of adding System.out.println() calls throughout your code. Some can be simple messages such as "I am here" just to tell you what line of code has executed. Others can be print out values of variables. Sometimes a quick SOP is much simpler than firing up a debugger, so knowing how to do both will make you a better programmer. Jan 8, 2013 at 22:51

2 Answers 2

3

You are not doing anything with your frequencies array. In you your addResponse method, you need to get the appropriate value and increment it. Add a statement like

frequencies[response-1] = frequencies[response-1] + 1;

Note you can do this with 2 arrays, as you are, especially for learning, but a lot of Java developers would use a Map implementation for this sort of thing, where the keys are the entered values and the values are the frequencies. Keeps all the data in one data structure.

0
0
public class DuplicatesInArray {

  public static void main(String[] args) {
    int count = 0;
    int[] arr = new int[6];
    int[] frequencyArr = new int[6];
       arr[0] = 4;
       arr[1] = 1;
       arr[2] = 1;
       arr[3] = 1;
       arr[4] = 5;
       arr[5] = 4;

      for(int i = 0;i < arr.length;i++){

       frequencyArr[arr[i]] =  frequencyArr[arr[i]] + 1;

      }
       for(int x = 0;x<frequencyArr.length;x++){
            if(frequencyArr[x] > 0){
               System.out.println(x + " " + frequencyArr[x] + " times." );

             }
       }
    }
}

Default value in an integer array will be zero. We increment it by one for every occurrence. Example:If integer 4 occurs 2 times,4th index at the frequencyArr is incremented twice.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.