I'm doing my personal android calculator just for fun to learn Java and Android.
But the function is not working... the String list operacoes
is where I have, in sequence the operators i will need (+,-,x and /). And the String list numeros
is where I have the numbers to do the operations like for example: 32 45 232 12.
Maybe what I want to do in the code is hard to explain but I'll give it a try: I'm trying to do the x or / operations first and then reduce 1 element in numeros
and operacoes
list, and only after all of them are done I will go to + and - operations.
But it gives errors and I don't know what is wrong. Does someone has a clue?
The android compiler says:
.UnsupportedOperationException
at java.util.AbstractList.remove(AbstractList.java:638)
at nunoprograms.com.nunoprogram.Operacoes.conta(Operacoes.java:54)
at nunoprograms.com.nunoprogram.App2Activity.onClick(App2Activity.java:222)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
And code is:
public int conta(){
int j,num1,num2,total=0;
num1=Integer.parseInt(this.numeros.get(0));
int x=this.operacoes.size();
int y=0;
for(int i=0;i<x-y;i++){//Objective: priority to x and / operations
if(this.operacoes.get(i).equals("x")||this.operacoes.get(i).equals("/")) {
if (this.operacoes.get(i).equals("x"))
total = Integer.parseInt(this.numeros.get(i)) * Integer.parseInt(this.numeros.get(i+1));
if (this.operacoes.get(i).equals("/"))
total = Integer.parseInt(this.numeros.get(i)) / Integer.parseInt(this.numeros.get(i+1));
this.numeros.set(i,String.valueOf(total));//atribui o valor da operaçao
for (j=i;j<this.operacoes.size()-1;j++)this.numeros.set(j+1,this.numeros.get(j+2));
for (j=i;j<this.operacoes.size()-1;j++)this.operacoes.set(j,this.operacoes.get(j+1));
this.numeros.remove(this.numeros.size()-1);//remove last element
this.operacoes.remove(this.operacoes.size()-1);
y++;
}
}
//now the + and - operations
total=num1;
for(int i=0;i<this.operacoes.size();i++) {
num2=Integer.parseInt(this.numeros.get(i+1));
System.out.println("conteudo da string de operacoes: "+ this.operacoes.get(i));
if(this.operacoes.get(i).equals("+"))total+=num2;
else if(this.operacoes.get(i).equals("-"))total-=num2;
}
return total;
}
Operacoes Class:
public class Operacoes {
private int numeros_size,operacoes_size;
private List<String> numeros;
private List<String> operacoes;
Operacoes(List<String> x,int x_size, List<String> y, int y_size){
this.numeros=x;
this.numeros_size=x_size;
this.operacoes=y;
this.operacoes_size=y_size;
}
public int conta(){
...
}
}
ADDED
I had to do this on the declaration of the String lists:
Operacoes(List<String> x,int x_size, List<String> y, int y_size){
this.numeros=new ArrayList(x);
this.numeros_size=x_size;
this.operacoes=new ArrayList(y);
this.operacoes_size=y_size;
}
It works now!