Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9125637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:51:01+00:00 2026-06-17T06:51:01+00:00

I can’t see where I’m going wrong with this one, I want it to

  • 0

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

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:51:02+00:00Added an answer on June 17, 2026 at 6:51 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can any one tell, how to get the result of LINQ query contains group
Can anyone tell me what i am doing wrong? I am trying to get
Can any one help me in sorting this out in sed/awk/perl Input file Start
Can find why i get this error can someone help? package Android.data; public class
Can someone tell me what I am doing wrong? This is my first time
Can someone tell me where I'm going wrong here? basically I've written some code
Can any one provide me regex for finding all src tag. I am stuck
Can I change the field public virtual ClassOne ClassOne { get; set; } to
Can anyone explain to me why this program: for(float i = -1; i <
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.