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 5 of 5

Thread: Arrays and MD Arrays

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

    Default Arrays and MD Arrays

    Hi I am confused and stuck on this I have a method enterQuestions() that uses a String array to hold 10 questions for a survey. However I also need to display these 10 questions and then store the responses as an int between 1 and 5. Then display the responses in a my logResponses() method. I am confused I fell like I may be close but I am not sure. Any help is appreciated.

    import java.util.*;
     
    public class Survey21 {
     
        private Scanner in = new Scanner(System.in);
        private String surveyTitle;
        private static int rID;
        private static int respondentID;
        private int[][] responses = new int[10][11];
        String questions[] = new String[10];
     
        //Constructors
        // Default Constructor
        Survey21() {
     
            surveyTitle = "Customer Survey";
        }
     
        // Overloaded Constructor 1
        Survey21(String title, int rID) {
            this.surveyTitle = title;
            rID = 0;
            generateRespondentId();
        }
     
        public static int getrID() {
            return rID;
        }
     
        public static void setRespondentID(int respondentID) {
            respondentID = rID;
     
        }
     
        public String getTitle() {
            return surveyTitle;
        }
     
        public void setTitle(String title) {
            surveyTitle = title;
        }
     
        public static int generateRespondentId() {
            rID = ++respondentID;
            return rID;
        }
     
        // displays name of survey and entire grid of results
        public void displaySurveyResults() {
            System.out.printf("%s\n\n", getTitle());
            logResponse();
        }
     
        // dispalys question number and results for that question so far
        public void displayQuestionStats(int questionNumber) {
        }
     
        // enter questions and store in an array of Strings
        public void enterQuestions() {
            for (int i = 0; i < questions.length; i++) {
                System.out.println("Please enter a question!");
                questions[i] = in.nextLine();
            }
            /*TEST TEST***
     
             */
     
        }
     
        // enters the response in the correct grid 
        public void logResponse() {
             for (int i = 0; i < responses.length; i++) {
                System.out.print(questions[0] + "\n");
                responses[i][i] = in.nextInt();
                System.out.print(questions[1] + "\n");
                responses[i][i] = in.nextInt();
                System.out.print(questions[2] + "\n");
                responses[i][i] = in.nextInt();
                System.out.print(questions[3] + "\n");
                responses[i][i] = in.nextInt();
                System.out.print(questions[4] + "\n");
                responses[i][i] = in.nextInt();
                System.out.print(questions[5] + "\n");
                responses[i][i] = in.nextInt();
                System.out.print(questions[6] + "\n");
                responses[i][i] = in.nextInt();
                System.out.print(questions[7] + "\n");
                responses[i][i] = in.nextInt();
                System.out.print(questions[8] + "\n");
                responses[i][i] = in.nextInt();
                System.out.print(questions[9] + "\n");
                responses[i][i] = in.nextInt();
                System.out.println("The responses are:\n");
                System.out.print("          "); // align column heads
     
                // create a column heading for each question
                for (int qNumber = 1; qNumber < responses[0].length; qNumber++) {
                    System.out.printf("Question number %d       ", qNumber);
                    System.out.println("Response"); // column heading 
                }
                for (int response = 0; response < responses.length; response++) {
                    System.out.printf("Response %2d", response + 1);
     
                    for (int qNumber : responses[response])// output responses
                    {
                        System.out.printf("%8d\n", qNumber);
                    }
                }
            }
     
        }
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Arrays and MD Arrays

    Quote Originally Posted by mstratmann View Post
    I fell like I may be close but I am not sure.
    Do a test run and see what happens.
    As a side note, the code tag on the forum has no spaces.

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

    Default Re: Arrays and MD Arrays

    I have ran a test and this is what I am getting

    run:
    Please enter a question!
    3
    Please enter a question!
    4
    Please enter a question!
    5
    Please enter a question!
    2
    Please enter a question!
    f
    Please enter a question!
    rtr
    Please enter a question!

    Please enter a question!
    vef
    Please enter a question!
    ve
    Please enter a question!
    fg
    Customer Survey

    3

    4
    4
    5
    5
    6
    2
    7
    f
    8
    rtr
    3

    2
    vef
    1
    ve
    5
    fg
    5
    The responses are:

    Question number 1 Response
    Question number 2 Response
    Question number 3 Response
    Question number 4 Response
    Question number 5 Response
    Question number 6 Response
    Question number 7 Response
    Question number 8 Response
    Question number 9 Response
    Question number 10 Response
    Response 1 5
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    Response 2 0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    Response 3 0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    Response 4 0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    Response 5 0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    Response 6 0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    Response 7 0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    Response 8 0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    Response 9 0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    Response 10 0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    3

    --- Update ---

    I do not know why it is outputting like this I am confused!!

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Arrays and MD Arrays

    Looks like some looped output...
    What do you expect the output to look like?
    Also it is not easy to test the code without a driver, posting a SSCCE will speed up the Q/A process

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,775
    My Mood
    Mellow
    Thanks
    235
    Thanked 715 Times in 701 Posts

    Default Re: Arrays and MD Arrays

    Instead of using a number of parallel arrays in which to store the data, are you allowed to use a class in which to store each question, the answer, and maybe its number in the test (if that matters)? Then perhaps another class would be helpful for holding each respondent's answers. That approach would be much simpler than what you're doing, and it would be consistent with OOP principles.

Similar Threads

  1. Help with Arrays!
    By le pencil in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 12th, 2013, 12:05 PM
  2. 2D arrays
    By Fluffy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 5th, 2012, 04:11 PM
  3. Arrays ?
    By nhiap6 in forum Java Theory & Questions
    Replies: 3
    Last Post: November 17th, 2012, 11:15 AM
  4. Arrays
    By Emperor_Xyn in forum Java Theory & Questions
    Replies: 6
    Last Post: December 9th, 2011, 06:24 PM
  5. Help with arrays
    By mike2452 in forum Collections and Generics
    Replies: 6
    Last Post: August 6th, 2011, 12:15 PM