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);

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?

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().

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.

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));
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.