1

So, i' m programming this useless calculator using the Android SDK for learning purposes The problem is that, every operation i do always returns a division I checked, but with my low experience i can't find the problem Well, this is the code for the main Java file:

package com.example.craxcalculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
enum operations{
NO, ADD, SUB, MUL, DIV
};
public class CraxCalculator extends Activity {

public float actualNumber = 0f;
public float finalResult=0.0f;
operations o = operations.NO;
public String onScreenNumber = new String();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_crax_calculator);
    Declarations();

}
@Override
protected void onStop(){
    super.onStop();
    o=operations.NO;
}

protected void Declarations(){
final Button b1 = (Button)findViewById(R.id.add1);
final Button b2 = (Button)findViewById(R.id.add2);
final Button b3 = (Button)findViewById(R.id.add3);
final Button b4 = (Button)findViewById(R.id.add4);
final Button b5 = (Button)findViewById(R.id.add5);
final Button b6 = (Button)findViewById(R.id.add6);
final Button b7 = (Button)findViewById(R.id.add7);
final Button b8 = (Button)findViewById(R.id.add8);
final Button b9 = (Button)findViewById(R.id.add9);
final Button b0 = (Button)findViewById(R.id.add0);
final Button res = (Button)findViewById(R.id.result);
final Button del = (Button)findViewById(R.id.del);
final Button mul = (Button)findViewById(R.id.mul);
final Button add = (Button)findViewById(R.id.add);
final Button sub = (Button)findViewById(R.id.sub);
final Button div = (Button)findViewById(R.id.div);
final TextView finalText = (TextView)findViewById(R.id.resultShow);

res.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        finalText.setText(onScreenNumber);
        switch (o){
        case NO:
            finalText.setText(onScreenNumber);
        case ADD:
            finalResult=actualNumber+Float.parseFloat(onScreenNumber);
            finalText.setText(String.valueOf(finalResult));
        case SUB:
            finalResult=actualNumber-Float.parseFloat(onScreenNumber);
            finalText.setText(String.valueOf(finalResult));
        case MUL:
            finalResult=actualNumber*Float.parseFloat(onScreenNumber);
            finalText.setText(String.valueOf(finalResult));
        case DIV:
            finalResult=actualNumber/Float.parseFloat(onScreenNumber);
            finalText.setText(String.valueOf(finalResult));
    }
    }
});

b1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        onScreenNumber = onScreenNumber+"1";
        finalText.setText(onScreenNumber);

    }
});
b2.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        onScreenNumber = onScreenNumber+"2";
        finalText.setText(onScreenNumber);
    }
});
b3.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        onScreenNumber+="3";
        finalText.setText(onScreenNumber);
    }
});
b4.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        onScreenNumber+="4";
        finalText.setText(onScreenNumber);
    }
});
b5.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        onScreenNumber+="5";
        finalText.setText(onScreenNumber);
    }
});
b6.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        onScreenNumber+="6";
        finalText.setText(onScreenNumber);

    }
});
b7.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        onScreenNumber+="7";
        finalText.setText(onScreenNumber);
    }
});
b8.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        onScreenNumber+="8";
        finalText.setText(onScreenNumber);
    }
});
b9.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        onScreenNumber+="9";
        finalText.setText(onScreenNumber);
    }
});
b0.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v){
        onScreenNumber+="0";
        finalText.setText(onScreenNumber);
    }
});
del.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v){
        onScreenNumber="";
        finalText.setText("0");
        actualNumber=0;
        finalResult=0;
    }
});
add.setOnClickListener(new View.OnClickListener(){

    public void onClick(View v){
        o = operations.ADD;
        actualNumber= Float.parseFloat(onScreenNumber);
        onScreenNumber="";
        finalText.setText("0");
    }
});
sub.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        o = operations.SUB;
        actualNumber=Float.parseFloat(onScreenNumber);
        onScreenNumber="";
        finalText.setText("0");
    }
});
mul.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        o = operations.MUL;
        actualNumber=Float.parseFloat(onScreenNumber);
        onScreenNumber="";
        finalText.setText("0");
    }
});
div.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        o = operations.DIV;
        actualNumber=Float.parseFloat(onScreenNumber);
        onScreenNumber="";
        finalText.setText("0");
    }
});
}
}

And this is my Activity xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >

<Button
    android:id="@+id/add1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="30dp"
    android:layout_marginTop="171dp"
    android:text="1" />

<Button
    android:id="@+id/add2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/add1"
    android:layout_alignBottom="@+id/add1"
    android:layout_marginLeft="21dp"
    android:layout_toRightOf="@+id/add1"
    android:text="2" />

<Button
    android:id="@+id/add3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/add2"
    android:layout_alignBottom="@+id/add2"
    android:layout_marginLeft="20dp"
    android:layout_toRightOf="@+id/add2"
    android:text="3" />

<Button
    android:id="@+id/add6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/add3"
    android:layout_below="@+id/add3"
    android:text="6" />

<Button
    android:id="@+id/add5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/add2"
    android:layout_toLeftOf="@+id/add3"
    android:text="5" />

<Button
    android:id="@+id/add4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/add1"
    android:layout_toLeftOf="@+id/add2"
    android:text="4" />

<Button
    android:id="@+id/add7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/add4"
    android:layout_below="@+id/add4"
    android:text="7" />

<Button
    android:id="@+id/add8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/add7"
    android:layout_alignBottom="@+id/add7"
    android:layout_toLeftOf="@+id/add3"
    android:text="8" />

<Button
    android:id="@+id/add9"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/add6"
    android:layout_below="@+id/add6"
    android:text="9" />

<TextView
    android:id="@+id/resultShow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/add1"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/add3"
    android:layout_marginTop="34dp"
    android:text="0"
    android:textSize="40dp" />

<Button
    android:id="@+id/result"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/add0"
    android:layout_alignBottom="@+id/add0"
    android:layout_alignLeft="@+id/add9"
    android:text="=" />

<Button
    android:id="@+id/add0"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/add8"
    android:layout_below="@+id/add8"
    android:text="0" />

<Button
    android:id="@+id/add"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/add6"
    android:layout_alignParentRight="true"
    android:layout_marginRight="14dp"
    android:text="+" />


<Button
    android:id="@+id/mul"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/result"
    android:layout_alignLeft="@+id/add"
    android:text="*" />

<Button
    android:id="@+id/sub"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/add"
    android:layout_below="@+id/add"
    android:text="-" />

<Button
    android:id="@+id/div"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/mul"
    android:layout_below="@+id/mul"
    android:text="/" />

<Button
    android:id="@+id/del"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/add7"
    android:layout_toLeftOf="@+id/add2"
    android:text="C" />

Well, i bonked my head everywhere, but nothing, i'm stuck with this problem c.c

2 Answers2

4

Your switch statement in your onClick method has no break statements ending cases. With no breaks, execution continues through cases below the case that was intended to run. In this case, the last case, DIV, overwrites the results of any other case in all cases.

Place breaks at the end of all cases.

switch (o){
    case NO:
        finalText.setText(onScreenNumber);
        break;
    case ADD:
        finalResult=actualNumber+Float.parseFloat(onScreenNumber);
        finalText.setText(String.valueOf(finalResult));
        break;
    case SUB:
        finalResult=actualNumber-Float.parseFloat(onScreenNumber);
        finalText.setText(String.valueOf(finalResult));
        break;
    case MUL:
        finalResult=actualNumber*Float.parseFloat(onScreenNumber);
        finalText.setText(String.valueOf(finalResult));
        break;
    case DIV:
        finalResult=actualNumber/Float.parseFloat(onScreenNumber);
        finalText.setText(String.valueOf(finalResult));
        break;
}
rgettman
  • 176,041
  • 30
  • 275
  • 357
0

You are missing the break in the switch:

        case NO:
            finalText.setText(onScreenNumber);
            break;
        case ADD:
            finalResult=actualNumber+Float.parseFloat(onScreenNumber);
            finalText.setText(String.valueOf(finalResult));
            break;
        case SUB:
            finalResult=actualNumber-Float.parseFloat(onScreenNumber);
            finalText.setText(String.valueOf(finalResult));
            break;
        case MUL:
            finalResult=actualNumber*Float.parseFloat(onScreenNumber);
            finalText.setText(String.valueOf(finalResult));
            break;
        case DIV:
            finalResult=actualNumber/Float.parseFloat(onScreenNumber);
            finalText.setText(String.valueOf(finalResult));
            break;
pomber
  • 23,132
  • 10
  • 81
  • 94