0

I have 4 textviews which take their values from dropdown list (spinner) selected at previous screen. There can be either 2 or 4 numbers/letters as result of this selection. The first position will always be a number and the second position will always be a letter. The third position can be a number or blank and the fourth position can be a letter or blank.
If position 3 and position 4 are blank then I need to make them equal to positions 1 & 2 respectively.

String myGrade = intent.getStringExtra("parameter_name_grade");

   // above takes value of 'myGrade' from spinner selection at previous screen

    String mDisplayGradeNumberEff = (" " + myGrade.charAt(0));
    TextView displayGradeNumberEff = (TextView) findViewById(R.id.gradeNumberEffTV);
    displayGradeNumberEff.setText(mDisplayGradeNumberEff);

    String mDisplayGradeLetterEff = (" " + myGrade.charAt(1));
    TextView displayGradeLetterEff = (TextView) findViewById(R.id.gradeLetterEffTV);
    displayGradeLetterEff.setText(mDisplayGradeLetterEff);

    // above works correctly

    // from here down only works when a character is present in both positions
    // if positions 3(2) and 4(3) are empty app stops running.  

    String mDisplayGradeNumberDia = (" " + myGrade.charAt(2));

    if(mDisplayGradeNumberDia.isEmpty())

    {
        mDisplayGradeNumberDia = mDisplayGradeNumberEff;

    }

    TextView displayGradeNumberDia = (TextView) findViewById(R.id.gradeNumberDiaTV);
    displayGradeNumberDia.setText(mDisplayGradeNumberDia);

    String mDisplayGradeLetterDia = (" " + myGrade.charAt(3));

    if(mDisplayGradeLetterDia.isEmpty()){
        mDisplayGradeLetterDia = mDisplayGradeLetterEff;
    }

    TextView displayGradeLetterDia = (TextView) findViewById(R.id.gradeLetterDiaTV);
    displayGradeLetterDia.setText(mDisplayGradeLetterDia);

Edited by peter_budo: Code formatting

3
Contributors
4
Replies
19
Views
2 Years
Discussion Span
Last Post by Centorpe
0

I don't know android Java, but are you sure isEmpty() will be true if the field contains a blank character as opposed to no character st all?

0

Java in Android is the same as Java except it is on different platform. I agree with JamesCherrill that " " is not equal to string length 0. isEmpty() tests whether the String object contains no character at all, not testing character with white spaces. If the OP really wants to test if empty, either use a better way to cast to String or trim() the string before the test for isEmpty().

0

Hi Taywin. Yes, I know the language is the same, but the API is different, especially around program initialisation and the gui. I didn't want to make any assumptions that could send the op down some invalid side track, that's all.

0

Ok, solved. Because strings are immutable I was unable to change it once string was created and loaded.

This worked:

     String myGrade = intent.getStringExtra("parameter_name_grade");

    String mDisplayGradeNumberEff, mDisplayGradeLetterEff, mDisplayGradeNumberDia, mDisplayGradeLetterDia;

    int myGradeLength = myGrade.length();

   if (myGradeLength != 4) {

        mDisplayGradeNumberEff = ("" + myGrade.charAt(0));
        mDisplayGradeLetterEff = ("" + myGrade.charAt(1));
        mDisplayGradeNumberDia = ("" + myGrade.charAt(0));
        mDisplayGradeLetterDia = ("" + myGrade.charAt(1));
   }else{
        mDisplayGradeNumberEff = ("" + myGrade.charAt(0));
        mDisplayGradeLetterEff = ("" + myGrade.charAt(1));
        mDisplayGradeNumberDia = ("" + myGrade.charAt(2));
        mDisplayGradeLetterDia = ("" + myGrade.charAt(3));
    }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.