Why Does This Java Code Only Store One Answer in an Array?

  • Context: Java 
  • Thread starter Thread starter lavster
  • Start date Start date
  • Tags Tags
    Java
Click For Summary
SUMMARY

The Java code provided in the discussion only stores one answer in an array due to the reuse of the same instance of the Answer class for both questions. The variable myAnswer_local is instantiated once and then updated with each answer, leading to both questions referencing the same answer object. To resolve this, a new instance of the Answer class should be created for each question within the loop.

PREREQUISITES
  • Understanding of Java programming concepts
  • Familiarity with object-oriented programming principles
  • Knowledge of Java collections, specifically ArrayList
  • Experience with basic input/output operations in Java
NEXT STEPS
  • Implement the creation of a new Answer instance within the loop in the Control method.
  • Explore Java ArrayList methods for better data management.
  • Learn about Java exception handling to manage user input errors effectively.
  • Investigate design patterns that could improve the structure of the Controller class.
USEFUL FOR

Java developers, software engineers, and students learning object-oriented programming who are looking to understand data handling in Java applications.

lavster
Messages
213
Reaction score
0
hey - any idea why the following bit of code only stores one of my answers, and not both in an array like i was hoping?
thanks

Code:
1 /*
  2  * To change this template, choose Tools | Templates
  3  * and open the template in the editor.
  4  */
  5 package gose1b;
  6 
  7 import java.util.ArrayList;
  8 import java.util.List;
  9 
 10 /**
 11  *
 12  * @author StudentPcOne
 13  */
 14 
 15 
 16 public class Controller { //class def eg controller could be person
 17  
 18  private View myView; 
 19  private Assessment myAssessment;
 20  private Question2 myQuestion1 = new Question2(); 
 21  private Question2 myQuestion2 = new Question2(); 
 22  private List myQuestion_list = new ArrayList(); 
 23 
 24  public Controller(){ 
 25   
 26 myQuestion1.setQuestion_s("What is your name: "); 
 27 myQuestion2.setQuestion_s("What is your Date of Birth"); 
 28         
 29 myQuestion_list.add(myQuestion1);
 30 myQuestion_list.add(myQuestion2);
 31         
 32     }
 33  
 34      public void Control(){ 
 35     
 36      
 37         
 38         myAssessment.setMyQuestionList(myQuestion_list);
 39        
 40                 myAssessment.setCount_i(2); //setting the number of questions to two
 41                 System.out.println("pig");
 42 
 43         List myQuestions_local = myAssessment.getMyQuestionList(); //collector class - retreive question from assessment
 44        
 45         Answer myAnswer_local = new Answer();
 46         List myAnswers_local = new ArrayList(); //list of answers for this class
 47         
 48         int myNumQuestions = myAssessment.getCount_i();
 49         
 50         for(int x=0; x< myNumQuestions; x++){ 
 51          
 52                   
 53             Question2 myQuestion_local = (Question2) myQuestions_local.get(x);
 54             
 55             String myQuestion = new String();
 56             String myAnswer = new String();
 57             myQuestion = (String) myQuestion_local.getQuestion_s();
 58                        
 59             myView.putOutput(myQuestion); //function defined in myView class which outputs question to user
 60                       
 61             myAnswer = myView.getInput(); //function defined in myView to get input from console
 62             myAnswer_local.setAnswer_s(myAnswer);
 63             myAnswers_local.add(myAnswer_local);//storing it locally ie in this class
 64             
 65             if (myAnswer.length()==0) // error message
 66                 {
 67                     if (x==0){
 68                System.out.println("PLEASE ENTER YOUR NAME \n (no answer was given previously) \n" );
 69                myAnswer = myView.getInput();
 70                
 71                     }
 72                     
 73                     if(x==1){
 74                         System.out.println("PLEASE ENTER YOUR DOB \n (no answer was given previously) \n");
 75                         myAnswer = myView.getInput();  
 76                
 77                        }
 78                     
 79                 
 80           }
 81          
 82             
 83 
 84             if (x==0){
 85            
 86             myQuestion1.setMyAnswer1(myAnswer_local);
 87            myView.putOutput(myAnswer);
 88 
 89             }                    
 90 
 91                     
 92             
 93             if (x==1){
 94                myQuestion2.setMyAnswer2(myAnswer_local);
 95                myView.putOutput(myAnswer);
 96          }
 97             
 98             
 99                                        
100             myQuestion_local.setDateAnswered();
101             
102                      
103         }
104         
105        
106                  
107                   myAssessment.setMyAnswerList(myAnswers_local);
108                   
109                                   
110         myView.showResults(myAssessment);
111 
112        
113         
114     }
115 
116     
117     
118     
119     /**
120      * @return the myView
121      */
122     public View getMyView() {
123         return myView;
124     }
125 
126     /**
127      * @param myView the myView to set
128      */
129     public void setMyView(View myView) {
130         this.myView = myView;
131     }
132 
133     /**
134      * @return the myAssessment
135      */
136     public Assessment getMyAssessment() {
137         return myAssessment;
138     }
139 
140     /**
141      * @param myAssessment the myAssessment to set
142      */
143     public void setMyAssessment(Assessment myAssessment) {
144         this.myAssessment = myAssessment;
145     }
146 }
147 
148
 
Technology news on Phys.org
WORKED IT OUT

however, does anyone know how to delete threads - is it even possible

thanks
 
What was the solution to your problem? It might help someone else who comes across this thread.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
1K
Replies
8
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K