Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Loops and arrays

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    31
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Loops and arrays

    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.

     
    import 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;
        }
    This where I am having issues
         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
     
    import java.util.*;
    public class SurveyTest {
     
     
        static Scanner in = new Scanner(System.in);
     
     
     
        public static void main(String[] args) {
     
            Survey survey = new Survey();
            survey.enterQuestions();
            survey.logResponse();
     
     
        }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Loops and arrays

    Time to step through this with a debugger, or at least add some print statements, to figure out what's going on. Where exactly does the code's execution differ from what you expect it to do?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 10
    Last Post: March 26th, 2013, 04:46 PM
  2. Tic-Tac-Toe (loops, arrays, and methods) help please
    By CVL220 in forum Collections and Generics
    Replies: 3
    Last Post: November 10th, 2012, 01:47 PM
  3. Distance formula with arrays, and loops. help?
    By smith999 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 22nd, 2012, 06:27 PM
  4. [SOLVED] Java assignment, concerning Arrays and Loops
    By trejorchest in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 17th, 2011, 12:59 PM
  5. Grades project/arrays, loops
    By rochla16 in forum Collections and Generics
    Replies: 1
    Last Post: March 29th, 2011, 12:26 AM