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

  • Java
  • Thread starter lavster
  • Start date
  • Tags
    Java
In summary, the conversation is about a person trying to store multiple answers in an array in their code, and they eventually figure out how to do it. They also ask if it is possible to delete threads.
  • #1
lavster
217
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
  • #2
WORKED IT OUT

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

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

What is a "Java problem"?

A "Java problem" can refer to any issue or difficulty encountered when using the Java programming language. This can include errors in code, difficulties with debugging, or challenges in understanding and implementing certain concepts.

Is solving a Java problem simple?

The difficulty of solving a Java problem can vary depending on the specific issue and the experience level of the programmer. Some problems may be simple to solve with a quick fix, while others may require more time and effort to troubleshoot and resolve.

What are some common causes of Java problems?

Some common causes of Java problems include syntax errors in the code, incorrect use of methods or functions, conflicting dependencies, and issues with memory management.

How can I troubleshoot a Java problem?

To troubleshoot a Java problem, it is important to carefully review the code and error messages, check for any known issues or bugs, and use debugging tools to identify specific areas of the code that may be causing issues. It can also be helpful to seek assistance from more experienced developers or consult online resources and forums.

What can I do to prevent Java problems?

To prevent Java problems, it is important to practice good coding habits, such as using proper indentation and commenting, testing code frequently, and staying updated on best practices and new updates in the Java language. It can also be helpful to regularly review and debug code to catch any potential issues early on.

Similar threads

  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Programming and Computer Science
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
3
Views
5K
  • Programming and Computer Science
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
Back
Top