I have a problem with loops and arrays. I need to gather an int response between 1 and 5 and store it in to the however it will accept all ints plus after it reaches question 10 it just repeats.
This where I am having issuesimport java.util.*; public class Survey { Scanner in = new Scanner(System.in); private String surveyTitle; public static int currentID = 0; // holds the current respondent Id private static int respondentID = 0; private int responses[] = new int[10]; private String questions[] = new String[10]; public final static int MAX_RESPONSES = 10; public final static int MAX_QUESTIONS = 10; public final static int PRINT_ALL = -1; Survey() { } Survey(int questionNumber, int respondentId){ } public int getCurrentID() { generateRespondentId(); return currentID; } // end getCurrentID public void setCurrentID(int respondentID) { Survey.currentID = respondentID; } public void setSurveyTitle(String surveyTitle) { this.surveyTitle = surveyTitle; } public String getSurveyTitle() { return surveyTitle; } public final void enterQuestions() { for (int i = 0; i < questions.length; i++) { System.out.println("Please enter a question! "); questions[i] = in.nextLine(); } } public final void displaySurveyResults(int min, int max) { // Random random = new Random(); int[] frequency = new int[11]; int[] responses = new int[10]; System.out.println(surveyTitle + "\n\n"); for (int i = 0; i < 10; i++) { //int randomNum = random.nextInt((max - min) + 1) + min; //responses[i] = randomNum; } for (int answer = 0; answer < responses.length; answer++) { try { ++frequency[responses[answer]]; } // emd try catch (ArrayIndexOutOfBoundsException e) { System.out.println(e); System.out.printf(" responses[%d] = %d\n\n", answer, responses[answer]); } //end catch } // end for System.out.printf("%s%15d\n\n", "Respondent's ID", getCurrentID()); System.out.printf("%10s%10s\n", "Question", "Frequency"); for (int qNumber = 1; qNumber < frequency.length; qNumber++) { System.out.printf("%6d%10d\n", qNumber, frequency[qNumber]); } } public int generateRespondentId() { String surveyTitleInput; int ID; ID = 0; int total = 0; ++ID; System.out.print("Please enter the survey title: "); surveyTitleInput = in.next(); ID = ID++; currentID = ID; return currentID; } // end generateRespondentId() method // Print bar chart for each question public void displayQuestionStats() { System.out.println("Response Distibution:"); int[] frequency = new int[11]; for (int count = 0; count < frequency.length; count++) { if (count == 10) { System.out.printf("%5d: ", 100); } else { System.out.printf("%02d-%02d: ", count * 10, count * 10 + 9); } for (int stars = 0; stars < frequency[count]; stars++) { System.out.print("*"); } System.out.println(); } } public void logResponse() { for(int i = 0; i < MAX_QUESTIONS; i++){ presentQuestion(i); } } public int topRatedQuestion() { int topRated = responses[0]; for (int response : responses) { if (response > topRated) { topRated = response; } } return topRated; } public int lowRatedQuestion() { int lowRated = responses[0]; for (int response : responses) { if (response < lowRated) { lowRated = response; } } return lowRated; }
public void presentQuestion(int questionNumber) { int response = 0; for (int count = 0; count < MAX_QUESTIONS; count++) { System.out.println("Question number: " + (count + 1)); System.out.println(questions[count]); response = in.nextInt(); if(response >= 5 && response >= 1){ responses[count] = response; } else if(response > 5 && response < 0){ System.out.print("Please enter a number between 1 and 5."); } } } public void presentQuestion(int questionNumber, int respondentID){ int response = 0; respondentID = getCurrentID(); for (int count = 0; count < questions.length; count++) { // System.out.println("Question number: " + (count + 1)); System.out.println("Respondent number " + respondentID + " please answer " + "guestion number " + (count + 1)); System.out.println(questions[count]); if (response >= 1 && response <= 5){ response = in.nextInt(); } responses[count] = response; } } }
This is the main class