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

  • Thread starter Thread starter lavster
  • Start date Start date
  • Tags Tags
    Java
AI Thread Summary
The discussion revolves around a coding issue where a user was unable to store multiple answers in an array as intended. The user shared a Java code snippet that defines a Controller class, which manages questions and answers. The problem stemmed from how answers were being stored; the same Answer object was being reused for both questions, leading to only the last answer being retained. The user later resolved the issue but sought advice on deleting forum threads, indicating a desire to clean up their posts. The discussion highlights the importance of creating new instances of objects for each answer to avoid overwriting previous entries.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top