3 Replies Latest reply: Feb 19, 2008 9:07 AM by 807601 RSS

    Print object.toString() from ArrayList problem.

    807601
      //main
      import java.io.IOException;
      import java.util.ArrayList;
      import java.util.Scanner;
      
      public class TestArray {
           public static void main(String args[]) throws IOException{
                
                int inputQuestionnAireNum = 0;
                int inputPostCode = 0;
                int inputGender = 0;
                int inputAge = 0;
                int inputResponse = 0;
                int inputQuestionNum = 0;
                
                int x=0;
                
                Scanner input = new Scanner(System.in);
                
                ArrayList<QuestionnAire> questionArr = new ArrayList<QuestionnAire> ();
                
                int rangeStore[] =new int[10];          
                
                do{
                     System.out.print("1 new entry or 0 print"); 
                     x = Integer.parseInt(input.nextLine());   
                          
                     if(x == 1){
                             
                          //Questionnaire Number 
                          System.out.print("Enter Questionnaire Number [ ] "); 
                          inputQuestionnAireNum = Integer.parseInt(input.nextLine());
                                    
                          //Postal code 
                          System.out.print("Enter Postal Code [ ] ");     
                          inputPostCode = Integer.parseInt(input.nextLine());
                          
                          //Age 
                          System.out.print("Enter Age [ ] "); 
                          inputAge = Integer.parseInt(input.nextLine());
                          out.write("Age: "+inputAge+", ");
                          
                          //Gender 
                          System.out.print("Enter Gender '1' for Male and '2' for for female [ ] "); 
                          inputGender = Integer.parseInt(input.nextLine());
                          
                          System.out.print("Enter Number of Question [ ]"); 
                          inputQuestionNum = Integer.parseInt(input.nextLine());
                                    
                          //Response input
                          for(int j = 0; j<inputQuestionNum; j++) {
                               System.out.print("Input Response for Question " +(j+1) +" [ ] ");     
                               inputResponse = Integer.parseInt(input.nextLine());
                          
                               rangeStore[j] = inputResponse;                                             
                          }//for response
                
                
                          QuestionnAire qObject = new QuestionnAire(inputQuestionnAireNum, inputPostCode, inputAge, inputGender, inputQuestionNum, rangeStore);
                          questionArr.add(qObject);
                                          
                               } else {
                                       for (int i = 0; i < questionArr.size(); i++) {
                                             
                                             QuestionnAire object = (QuestionnAire) questionArr.get(i );
                                             System.out.println(object.toString());
                                              }
                                         }                                   
                             } while(x != 0);
                      }
      }
      //Constructor
      
      public class QuestionnAire {
      
           int questionnAireNum = 0;
           int postCode = 0;
           int response = 0;
           int gender = 0;
           int age = 0;
           
          //int [] responseArr;
           int [] responseArr;
           
           int numOfQuestion = 0;
           int rangeOfResponse = 0;
           
           public QuestionnAire (int questionnAireNum,  int postCode, int age, int gender, int numOfQuestion, int responseArr[]) {
                
                this.questionnAireNum = questionnAireNum;
                this.postCode = postCode;
                this.age = age;
                this.numOfQuestion = numOfQuestion;
                this.gender = gender;
                this.responseArr = responseArr;
                
                } //public
           
                //Questionnaire Number
                public int getQuestionnAireNum() { 
                     return questionnAireNum;
                     }
                
                public void setQuestionnAireNum(int questionnAireNum) { 
                     this.questionnAireNum = questionnAireNum;
                     }
                
                //Postal Code
                public int getPostCode() { 
                     return postCode;
                     }
                
                public void setPostCode(int postCode) { 
                     this.postCode = postCode;
                     }
                
                //Age
                public int getAge() { 
                     return age;
                     }
                
                public void setAge(int age) { 
                     this.age = age;
                     }
                
                //Gender
                public int getGender() { 
                     return gender;
                     }
                
                public void setGender(int gender) { 
                     this.gender = gender;
                     }
                          
                public String toString() {
                     return "QuestionAirNumber: "+ questionnAireNum +", PostCode: "+ postCode+", Age: "+age+", Gender: "+gender +printArr();               
                     }
                
                public int[] printArr(){
                     for(int i =0;i<numOfQuestion;i++){
                          System.out.print("Question "+(i+1)+" Response: "+"["+responseArr[i]+"]"+", ");
                          }
                     return responseArr;
      
           }               
      }//class
      Result:
      1 new entry or 0 print1
      Enter Questionnaire Number [ ] 33
      Enter Postal Code [ ] 44
      Enter Age [ ] 66
      Enter Gender '1' for Male and '2' for for female [ ] 1
      Enter Number of Question [ ]3
      Input Response for Question 1 [ ] 4
      Input Response for Question 2 [ ] 7
      Input Response for Question 3 [ ] 3
      1 new entry or 0 print 0
      
      *Question 1 Response: [4], Question 2 Response: [7], Question 3 Response: [3], QuestionAirNumber: 33, PostCode: 44, Age: 66, Gender: 1[I@3f3789*
      Hi, i'm having this problem, when i print my object from ArrayList, the final output does not display the result in sequence order as shown above. It print the responses followed by QuestionAirNumber ... and then followed by some random characters. I need to display it the other way round starting from QuestionAirNumber, PostCode, ... and so on. What are my mistake ?