Reputation: 479
i'm now in java and eclipse, i just tried to build a simple calculator, but i can't make it to return with the result.
My plan was basicly that when the user click one of the operators, and the EditText isn't empty, then First variable will be equal with the EditText, and the Operator variable change, and when the user hit Result button, and the First variable isn't empty, then the the EditText will be equal with the Second variable, and the Result variable will be equal with the result basicly. I think i messed up something with the types and variables, but i can't figure it out what exactly.
Could someone help?
Here is the java code;
public class Main extends Activity implements OnClickListener {
LinearLayout linear;
float First, Second, Operator, Result;
Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, bResult, bTizedes, bSzorzas, bKivonas, bOsztas, bOsszeadas;
EditText eT;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
First = 0;
Second = 0;
Operator = 0;
Result = 0;
b0 = (Button) findViewById(R.id.b0);
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
b3 = (Button) findViewById(R.id.b3);
b4 = (Button) findViewById(R.id.b4);
b5 = (Button) findViewById(R.id.b5);
b6 = (Button) findViewById(R.id.b6);
b7 = (Button) findViewById(R.id.b7);
b8 = (Button) findViewById(R.id.b8);
b9 = (Button) findViewById(R.id.b9);
bTizedes = (Button) findViewById(R.id.bTizedes);
bSzorzas = (Button) findViewById(R.id.bSzorzas);
bResult = (Button) findViewById(R.id.bEgyenlo);
bKivonas = (Button) findViewById(R.id.bKivonas);
bOsztas = (Button) findViewById(R.id.bOsztas);
bOsszeadas = (Button) findViewById(R.id.bOsszeadas);
eT = (EditText) findViewById(R.id.eT);
b0.setOnClickListener(this);b1.setOnClickListener(this);b2.setOnClickListener(this);b3.setOnClickListener(this);
b4.setOnClickListener(this);b5.setOnClickListener(this);b6.setOnClickListener(this);b7.setOnClickListener(this);
b8.setOnClickListener(this);b9.setOnClickListener(this);bTizedes.setOnClickListener(this);bSzorzas.setOnClickListener(this);
bResult.setOnClickListener(this);bKivonas.setOnClickListener(this);bOsztas.setOnClickListener(this);bOsszeadas.setOnClickListener(this);
bSzorzas.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if(First == 0)
{
EditText eT = (EditText) findViewById(R.id.eT);
float First = Float.valueOf(eT.getText().toString());
Operator = 2;
eT.setText(null);
}
else if(First != 0)
{
Operator = 2;
};
}
});
bKivonas.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if(First == 0)
{
EditText eT = (EditText) findViewById(R.id.eT);
float First = Float.valueOf(eT.getText().toString());
Operator = 4;
eT.setText(null);
}
else if(First != 0)
{
Operator = 4;
};
}
});
bOsztas.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if(First == 0)
{
EditText eT = (EditText) findViewById(R.id.eT);
float First = Float.valueOf(eT.getText().toString());
Operator = 1;
eT.setText(null);
}
else if(First != 0)
{
Operator = 1;
};
}
});
bOsszeadas.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if(First == 0)
{
EditText eT = (EditText) findViewById(R.id.eT);
float First = Float.valueOf(eT.getText().toString());
Operator = 3;
eT.setText(null);
}
else if(First != 0)
{
Operator = 3;
};
}
});
bResult.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if(First != 0)
{
EditText eT = (EditText) findViewById(R.id.eT);
float Second = Float.valueOf(eT.getText().toString());
if(Operator == 1){
int Result = (int) (First) / (int) (Second);
eT.setText(Result);
}
else if(Operator == 2){
int Result = (int) (First) * (int) (Second);
eT.setText(Result);
}
else if(Operator == 3){
int Result = (int) (First) + (int) (Second);
eT.setText(Result);
}
else if(Operator == 4){
int Result = (int) (First) - (int) (Second);
eT.setText(Result);
}
eT.setText(null);
}
else if(First == 0)
{
};
}
});
}
public void onClick(View v) {
switch(v.getId()){
case R.id.b0:
eT.setText( eT.getText() + "0");
break;
case R.id.b1:
eT.setText( eT.getText() + "1");
break;
case R.id.b2:
eT.setText( eT.getText() + "2");
break;
case R.id.b3:
eT.setText( eT.getText() + "3");
break;
case R.id.b4:
eT.setText( eT.getText() + "4");
break;
case R.id.b5:
eT.setText( eT.getText() + "5");
break;
case R.id.b6:
eT.setText( eT.getText() + "6");
break;
case R.id.b7:
eT.setText( eT.getText() + "7");
break;
case R.id.b8:
eT.setText( eT.getText() + "8");
break;
case R.id.b9:
eT.setText( eT.getText() + "9");
break;
}
}}
Upvotes: 0
Views: 2083
Reputation: 31466
Try to focus on this Tutorial and see how things are done, Also try to improve it
Upvotes: 1
Reputation: 15052
float First = Float.valueOf(eT.getText().toString());
2 problems:
First
here? Simply use it.Float.parseFloat(eT.getText().toString());
And why do you name your variables as First
? use first
.Follow conventions.
EDIT:
Could see the same issue of re-declaring almost all your variables. Please don't do that.
Upvotes: 0