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