0

Sorry for seem making so clumpsy for the below code. To sum up, I am making a quiz game where user can choose 4 kinds of difficulties of the Question. if user choose checkbox 1 and 3, for example, the below code is intended to store string arrays from the string.xml into String[] NUM_ALL_QuestionNames.

//same for checkbox1,2,3, so here not duplicate //

      if (checkbox4=="yes") 
      {
          NUM_EXP_QuestionNames = getResources().getStringArray(R.array.Num_Q_Expert_List);                                               
          NUM_EXP_AnswerNames = getResources().getStringArray(R.array.Num_A_Expert_List);

          QuestionImport= 0;                  
          QuestionImport = NUM_EXP_QuestionNames.length;
          int i =0;
          while (i<QuestionImport)
          {
              String Q_toimport = NUM_EXP_QuestionNames[i];
              String A_toimport = NUM_EXP_AnswerNames[i];   

              NUM_ALL_QuestionNames.add(Q_toimport);
              NUM_ALL_AnswerNames.add(A_toimport);                    
              ++i;                    
          }           
      };


      NUM_ALLL_QuestionNames = new String[NUM_ALL_QuestionNames.size()]; //convert ArrayList<String> to String[]
      NUM_ALLL_AnswerNames = new String[NUM_ALL_AnswerNames.size()]; //convert ArrayList<String> to String[]

At last NUM_ALL_Questionnames would be converted back from ArrayList NUM_ALLL_QuestionNames to String[] for further processing.

The files in the string.xml should have no problem because when I try set as follows (directly extract from the string.xml) the apps runs fine:

NUM_ALLL_QuestionNames = getResources().getStringArray(R.array.Num_Q_Simple_List);
NUM_ALLL_AnswerNames = getResources().getStringArray(R.array.Num_A_Simple_List);

Question:

  1. Now is it data from string.xml in String[] format, it was then added to the List, where this List at last convert back to String[]. Are there any modification such that is no need this conversion. It is better if string.xml can be directly to String[]

  2. When goes on executeing the app, logcat does not show any error. However, the Emulator keep black screen. Why does it happen? Are there any endless loop for the Code?

Many thanks!

2
  • 3
    aouch. Java 101 : Strings are compared with equals, not == Commented Nov 20, 2012 at 17:22
  • You might consider checking in debug mode in which moment the app freezes. Commented Nov 20, 2012 at 17:25

2 Answers 2

2

This is wrong:

NUM_ALLL_QuestionNames = new String[NUM_ALL_QuestionNames.size()]; //convert ArrayList<String> to String[]

It doesn't convert an ArrayList to a String[], it sets the value of your variable to an empty String[]. To convert, do this:

NUM_ALLL_QuestionNames = NUM_ALL_QuestionNames.toArray();

assuming that NUM_ALL_QuestionNames is a List.

And the Doctor is right, that string comparison doesn't work in Java.

Sign up to request clarification or add additional context in comments.

Comments

0

I don't see why the checkbox4 value is a string, but don't ever compare strings through == because this way you compare links that might point to different objects. Instead, use the equals method from String like this if (checkbox4.equals("yes"))

2 Comments

That wouldn't cause the app to freeze, it would just always return false
Who knows, we don't see the whole picture. Maybe it loops until it finds somewhere "true"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.