Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java problem - prob simple!

  1. Nov 6, 2011 #1
    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 (Text):


    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

     
     
  2. jcsd
  3. Nov 6, 2011 #2
    WORKED IT OUT

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

    thanks
     
  4. Nov 6, 2011 #3

    jtbell

    User Avatar

    Staff: Mentor

    What was the solution to your problem? It might help someone else who comes across this thread.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Java problem - prob simple!
  1. Simple Java Program Help (Replies: 18)

  2. Simple Java Problem (Replies: 2)

Loading...