Replace numbers with letters inside Arraylist

Asked

Viewed 1,109 times

-2

I’m creating a calculator where I want the numbers inside the ArrayList, are replaced by letters in textView.

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

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

@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, always remains the numbers, I have 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));


}
  • That’s java right there?

  • Welcome to Sopt. I suggest you do the [tour] and visit [help] to better enjoy the site. I also suggest you see the en.stackoverflow.com/help/mcve page, this will help you with your next questions. Click [Edit] on your question and add what programming language you are using, and if possible, remove some code (only what is trivial) leaving only what is really essential for us to help you.

  • @rray, yes it is java.

  • @jbueno, thank you!

1 answer

0

I wanted to comment, but it seems I don’t have enough reputation. I may not solve your problem, but I can give you some tips:

  • 1 : If you are implementing a calculator the data structure that I recommend is the Stack (Stack). I realized you’re killing yourself to treat the values you take from your Arraylist. If you use one Stack these operations become much simpler. Basically, very briefly, you stack (push) all the input and then it will pop (pop) and treating the numbers and operators. I think it’s nice that you 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 just do:

    String[] string = new String[10];
    
    int i;
    char letra;
    
    for(i = 0, letra = 'A'; i < 10; i++, letra++)
      string[i] = "" + letra;
    
  • 3 : In fact, I wouldn’t even use variable/vector to do what you’re trying. If you want to replace the letters with numbers (or vice versa), you can use the function .replace of the string and already consider all cases. In the example below, this line exchanges all numbers by the respective 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 changing if for switch or your .contains for .equals. If you don’t know, it’s worth a search.

I hope you succeed in your project, if there’s anything I can do to help, just give it a touch.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.