-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?
– rray
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.
– Jéf Bueno
@rray, yes it is java.
– Andremasa Fiares
@jbueno, thank you!
– Andremasa Fiares