Replace numbers by letters inside the ArrayList

-2

I'm creating a calculator where, I want the numbers inside ArrayList , to be replaced with letters in textView .

Example: 1 = A , 2 = B , etc.

With the code below, I got this by typing the numbers. That is, textView appear letters instead of numbers.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

ArrayList<String> arrayList = new ArrayList<String >();
String string = "";
String string1 = "";
String string10 = "A";
String string2 = "B";
String string3 = "C";
String string4 = "D";
String string5 = "E";
String string6 = "F";
String string7 = "G";
String string8 = "H";
String string9 = "I";
String string0 = "J";


public void onClick1 (View v) {
    TextView textView3 = (TextView) findViewById(R.id.caracteres);
    Button button = (Button) v;

    string = (String) button.getText().toString();

    if (!string.contains("+") && !string.contains("-") && !string.contains("*") && !string.contains("/")) {

        if (string.contains("2")) {
            string1 = string1+string2;
        }

        if (string.contains("3")) {
            string =string3;
        }

        if (string.contains("4")) {
            string =string4;
        }

        if (string.contains("5")) {
            string =string5;
        }

        if (string.contains("6")) {
            string =string6;
        }

        if (string.contains("7")) {
            string =string7;
        }

        if (string.contains("8")) {
            string =string8;
        }

        if (string.contains("9")) {
            string =string9;
        }

        if (string.contains("0")) {
            string =string0;
        }
    }

    textView3.setText(textView3.getText().toString() + string);
  

However, in the result, I could not replace, it always remains the numbers, I've tried everything.

public void onClick (View v) {
    int calc= 0;
    int c = arrayList.size();



    TextView textView3 = (TextView) findViewById(R.id.caracteres);

    if (arrayList.contains("8")) {
        string =string8;
    }


    //eg: array (2,+,3,*,4,-,3) size = 7, so (2,+,3,*,4,-,3)

    while (c!=1){

        if (c>3) {
            if (arrayList.get(3).contains("*") || arrayList.get(3).contains("/")) {
                if (arrayList.get(3).contains("*")) {calc = Integer.parseInt(arrayList.get(2))*Integer.parseInt(arrayList.get(4));}
                if (arrayList.get(3).contains("/")) {calc = Integer.parseInt(arrayList.get(2))/Integer.parseInt(arrayList.get(4));}

                //calc = 12 ;array = (2,+,3,*,4,-,3)

                arrayList.remove(2); // (2,+,+,*,4,-,3)
                arrayList.remove(2); // (2,+,3,*,4,-,3)
                arrayList.remove(2); // (2,+,+,-,3)
                arrayList.add(2, Integer.toString(calc)); // (2,+,12,-,3)
                c= arrayList.size(); // size 5

            }

            else {
                // (2,+,12,-,3)
                if (arrayList.get(1).contains("+")) {calc = Integer.parseInt(arrayList.get(0))+Integer.parseInt(arrayList.get(2));}
                if (arrayList.get(1).contains("-")) {calc = Integer.parseInt(arrayList.get(0))-Integer.parseInt(arrayList.get(2));}
                if (arrayList.get(1).contains("*")) {calc = Integer.parseInt(arrayList.get(0))*Integer.parseInt(arrayList.get(2));}
                if (arrayList.get(1).contains("/")) {calc = Integer.parseInt(arrayList.get(0))/Integer.parseInt(arrayList.get(2));}
                    // calc = 14
                arrayList.remove(0); // (+,12,-,3)
                arrayList.remove(0); // (12,-,3)
                arrayList.remove(0); // (-,3)
                arrayList.add(0, Integer.toString(calc)); // (12,-,3)
                c = arrayList.size(); // size = 3

            }

        }
            //size <= 3

        else {

            if (arrayList.get(1).contains("+")) {calc = Integer.parseInt(arrayList.get(0))+Integer.parseInt(arrayList.get(2));}
            if (arrayList.get(1).contains("-")) {calc = Integer.parseInt(arrayList.get(0))-Integer.parseInt(arrayList.get(2));}
            if (arrayList.get(1).contains("*")) {calc = Integer.parseInt(arrayList.get(0))*Integer.parseInt(arrayList.get(2));}
            if (arrayList.get(1).contains("/")) {calc = Integer.parseInt(arrayList.get(0))/Integer.parseInt(arrayList.get(2));}
                // calc = 9
            arrayList.remove(0); //(-,3)
            arrayList.remove(0); //(3)
            arrayList.remove(0); //()
            arrayList.add(0, Integer.toString(calc)); //(9)
            c = arrayList.size(); // size = 1 since is 1 loop ends.
            arrayList.add(string);
        }
    }


    textView3.setText(Integer.toString(calc));


}
    
asked by anonymous 06.10.2015 / 17:44

1 answer

0

I wanted to comment, but it seems I do not have enough reputation. Maybe I will not solve your problem, but I can give you some tips:

  • 1: If you are implementing a calculator the data structure I recommend is Stack . I realized that you are killing yourself to handle the values you get from your ArrayList . If you use a Stack these operations become much simpler. Basically, very briefly, you stack ( push ) all the input and then popping ( pop ) and handling the numbers and operators. I think it's cool for you to do a search.
  • 2: There are many variables with the same purpose in your code. You can group them all into a single vector. Instead of string0 , string1 , string2 , ... you can simply do:

    String[] string = new String[10];
    
    int i;
    char letra;
    
    for(i = 0, letra = 'A'; i < 10; i++, letra++)
      string[i] = "" + letra;
    
  • 3: Actually, I would not even use a vector / vector to do what you're trying to do. If you want to replace the letters by numbers (or vice versa), you can use the .replace function of the string and already consider all cases. In the example below, this line changes all numbers by their letters (if it exists in the string):

    string = ((((((((string.replace("2", "B")).replace("3", "C")).replace("4", "D")).replace("5", "E")).replace("6", "F")).replace("7", "G")).replace("8", "H")).replace("9", "I")).replace("0", "J");
    
  • 4: If you still want to work with your conditions, I recommend switching if to switch or your .contains by .equals . If you do not know, it's worth a look.

I hope you succeed in your project, if you have something I can help, just give me a hint.

    
07.10.2015 / 04:32