Error while working in Android Studio - java

This is an application coding for a basic calculator using Android Studio. There is en error just before the end of the coding. I have mentioned the error in the coding directly.
Can some please explain what the error is all about and how I can go about rectifying the same?
package com.example.asish.calculator;
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
Button bone, btwo, bthree, bfour, bfive, bsix, bseven, beight, bnine, bzero, badd, bsub, bmul, bdiv, bclear,
bce, bequal;
TextView txt;
String s = "", s1 = "", s2 = "", s3 = "";
int i = 0, i1 = 0, c = -1, flag_disable = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bone = (Button) findViewById(R.id.one);
btwo = (Button) findViewById(R.id.two);
bthree = (Button) findViewById(R.id.three);
bfour = (Button) findViewById(R.id.four);
bfive = (Button) findViewById(R.id.five);
bsix = (Button) findViewById(R.id.six);
bseven = (Button) findViewById(R.id.seven);
beight = (Button) findViewById(R.id.eight);
bnine = (Button) findViewById(R.id.nine);
bzero = (Button) findViewById(R.id.zero);
bce = (Button) findViewById(R.id.ce);
bequal = (Button) findViewById(R.id.equals);
bclear = (Button) findViewById(R.id.clear);
badd = (Button) findViewById(R.id.plus);
bsub = (Button) findViewById(R.id.minus);
bdiv = (Button) findViewById(R.id.division);
bmul = (Button) findViewById(R.id.multiply);
bone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s = (String) txt.getText();
if(s.equals("+")||s.equals("-")||s.equals("/")||s.equals("*")) {
txt.setText("");
s = "";
}
txt.setText(s+"1");
s = "";
}
});
btwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s = (String) txt.getText();
if(s.equals("+")||s.equals("-")||s.equals("/")||s.equals("*")) {
txt.setText("");
s = "";
}
txt.setText(s+"2");
s = "";
}
});
bthree.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s = (String) txt.getText();
if(s.equals("+")||s.equals("-")||s.equals("/")||s.equals("*")) {
txt.setText("");
s = "";
}
txt.setText(s+"3");
s = "";
}
});
bfour.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s = (String) txt.getText();
if(s.equals("+")||s.equals("-")||s.equals("/")||s.equals("*")) {
txt.setText("");
s = "";
}
txt.setText(s+"4");
s = "";
}
});
bfive.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s = (String) txt.getText();
if(s.equals("+")||s.equals("-")||s.equals("/")||s.equals("*")) {
txt.setText("");
s = "";
}
txt.setText(s+"5");
s = "";
}
});
bsix.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s = (String) txt.getText();
if(s.equals("+")||s.equals("-")||s.equals("/")||s.equals("*")) {
txt.setText("");
s = "";
}
txt.setText(s+"6");
s = "";
}
});
bseven.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s = (String) txt.getText();
if(s.equals("+")||s.equals("-")||s.equals("/")||s.equals("*")) {
txt.setText("");
s = "";
}
txt.setText(s+"7");
s = "";
}
});
beight.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s = (String) txt.getText();
if(s.equals("+")||s.equals("-")||s.equals("/")||s.equals("*")) {
txt.setText("");
s = "";
}
txt.setText(s+"8");
s = "";
}
});
bnine.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s = (String) txt.getText();
if(s.equals("+")||s.equals("-")||s.equals("/")||s.equals("*")) {
txt.setText("");
s = "";
}
txt.setText(s+9);
s = "";
}
});
bzero.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s = (String) txt.getText();
if(s.equals("+")||s.equals("-")||s.equals("/")||s.equals("*")) {
txt.setText("");
s = "";
}
txt.setText(s+"0");
s = "";
}
});
badd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String tmp = (String) txt.getText();
if(!tmp.equals("+")&&!tmp.equals("-")&&!tmp.equals("*")&&!tmp.equals("/"))
s1=tmp;
c=0;
txt.setText("+");
}
});
bsub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String tmp = (String) txt.getText();
if(!tmp.equals("+")&&!tmp.equals("-")&&!tmp.equals("*")&&!tmp.equals("/"))
s1=tmp;
c=2;
txt.setText("-");
}
});
bmul.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String tmp = (String) txt.getText();
if(!tmp.equals("+")&&!tmp.equals("-")&&!tmp.equals("*")&&!tmp.equals("/"))
s1=tmp;
c=3;
txt.setText("*");
}
});
bdiv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String tmp = (String) txt.getText();
if(!tmp.equals("+")&&!tmp.equals("-")&&!tmp.equals("*")&&!tmp.equals("/"))
s1=tmp;
c=2;
txt.setText("/");
}
});
bequal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(s1=="+"||s1=="-"||s1=="/"||s1=="*") {
i = 0;
}
else if(s1==null|| s1==""||s1.trim().length()==0) {
i = i;
}
else {
i = Integer.parseInt(s1);
}
s2 = (String) txt.getText();
if(s2=="+"||s2=="-"||s2=="/"||s2=="*") {
i1 = 0;
}
else if(s2==null|| s2==""||s2.trim().length()==0) {
i1 = 0;
}
else {
i1 = Integer.parseInt(s2);
}
if(c==0) {
i = i + i1;
}
else if(c==1) {
i = i - i1;
}
else if(c==2){
if(i1==0) {
Toast.makeText(getApplicationContext(), "Invalid Input", Toast.LENGTH_LONG).show();
i = 0;
}
else{
i = i / i1;
}
}
else if(c==3) {
i = i * i1;
}
else {
i = 0;
}
txt.setText(i+"");
}
});
bclear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s = (String) txt.getText();
if (s.equals("+") || s.equals("-") || s.equals("/") || s.equals("*") || s.equals("")) {
i = 0;
} else {
i = Integer.parseInt(s);
i = i / 10;
}
if (i == 0) {
txt.setText("");
} else {
txt.setText(i + "");
}
s = null;
}
});
bce.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt.setText("");
i = 0;
i1 = 0;
s1 = "";
s2 = "";
c = -1;
}
});
}
} --> Error in this line. Error message - Class or interface expected.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

Looks like you have to delete one of the brackets where you noted the error. That should fix the issue.

Try this code to your tool.
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.vcs.YOURPACKAGE.R;(it will be import automatically)
public class MainActivity extends AppCompatActivity {
Button bone, btwo, bthree, bfour, bfive, bsix, bseven, beight, bnine, bzero, badd, bsub, bmul, bdiv, bclear,
bce, bequal;
TextView txt;
String s = "", s1 = "", s2 = "", s3 = "";
int i = 0, i1 = 0, c = -1, flag_disable = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bone = (Button) findViewById(R.id.one);
btwo = (Button) findViewById(R.id.two);
bthree = (Button) findViewById(R.id.three);
bfour = (Button) findViewById(R.id.four);
bfive = (Button) findViewById(R.id.five);
bsix = (Button) findViewById(R.id.six);
bseven = (Button) findViewById(R.id.seven);
beight = (Button) findViewById(R.id.eight);
bnine = (Button) findViewById(R.id.nine);
bzero = (Button) findViewById(R.id.zero);
bce = (Button) findViewById(R.id.ce);
bequal = (Button) findViewById(R.id.equals);
bclear = (Button) findViewById(R.id.clear);
badd = (Button) findViewById(R.id.plus);
bsub = (Button) findViewById(R.id.minus);
bdiv = (Button) findViewById(R.id.division);
bmul = (Button) findViewById(R.id.multiply);
bone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s = (String) txt.getText();
if (s.equals("+") || s.equals("-") || s.equals("/") || s.equals("*")) {
txt.setText("");
s = "";
}
txt.setText(s + "1");
s = "";
}
});
btwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s = (String) txt.getText();
if (s.equals("+") || s.equals("-") || s.equals("/") || s.equals("*")) {
txt.setText("");
s = "";
}
txt.setText(s + "2");
s = "";
}
});
bthree.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s = (String) txt.getText();
if (s.equals("+") || s.equals("-") || s.equals("/") || s.equals("*")) {
txt.setText("");
s = "";
}
txt.setText(s + "3");
s = "";
}
});
bfour.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s = (String) txt.getText();
if (s.equals("+") || s.equals("-") || s.equals("/") || s.equals("*")) {
txt.setText("");
s = "";
}
txt.setText(s + "4");
s = "";
}
});
bfive.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s = (String) txt.getText();
if (s.equals("+") || s.equals("-") || s.equals("/") || s.equals("*")) {
txt.setText("");
s = "";
}
txt.setText(s + "5");
s = "";
}
});
bsix.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s = (String) txt.getText();
if (s.equals("+") || s.equals("-") || s.equals("/") || s.equals("*")) {
txt.setText("");
s = "";
}
txt.setText(s + "6");
s = "";
}
});
bseven.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s = (String) txt.getText();
if (s.equals("+") || s.equals("-") || s.equals("/") || s.equals("*")) {
txt.setText("");
s = "";
}
txt.setText(s + "7");
s = "";
}
});
beight.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s = (String) txt.getText();
if (s.equals("+") || s.equals("-") || s.equals("/") || s.equals("*")) {
txt.setText("");
s = "";
}
txt.setText(s + "8");
s = "";
}
});
bnine.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s = (String) txt.getText();
if (s.equals("+") || s.equals("-") || s.equals("/") || s.equals("*")) {
txt.setText("");
s = "";
}
txt.setText(s + 9);
s = "";
}
});
bzero.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s = (String) txt.getText();
if (s.equals("+") || s.equals("-") || s.equals("/") || s.equals("*")) {
txt.setText("");
s = "";
}
txt.setText(s + "0");
s = "";
}
});
badd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String tmp = (String) txt.getText();
if (!tmp.equals("+") && !tmp.equals("-") && !tmp.equals("*") && !tmp.equals("/"))
s1 = tmp;
c = 0;
txt.setText("+");
}
});
bsub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String tmp = (String) txt.getText();
if (!tmp.equals("+") && !tmp.equals("-") && !tmp.equals("*") && !tmp.equals("/"))
s1 = tmp;
c = 2;
txt.setText("-");
}
});
bmul.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String tmp = (String) txt.getText();
if (!tmp.equals("+") && !tmp.equals("-") && !tmp.equals("*") && !tmp.equals("/"))
s1 = tmp;
c = 3;
txt.setText("*");
}
});
bdiv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String tmp = (String) txt.getText();
if (!tmp.equals("+") && !tmp.equals("-") && !tmp.equals("*") && !tmp.equals("/"))
s1 = tmp;
c = 2;
txt.setText("/");
}
});
bequal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (s1 == "+" || s1 == "-" || s1 == "/" || s1 == "*") {
i = 0;
} else if (s1 == null || s1 == "" || s1.trim().length() == 0) {
i = i;
} else {
i = Integer.parseInt(s1);
}
s2 = (String) txt.getText();
if (s2 == "+" || s2 == "-" || s2 == "/" || s2 == "*") {
i1 = 0;
} else if (s2 == null || s2 == "" || s2.trim().length() == 0) {
i1 = 0;
} else {
i1 = Integer.parseInt(s2);
}
if (c == 0) {
i = i + i1;
} else if (c == 1) {
i = i - i1;
} else if (c == 2) {
if (i1 == 0) {
Toast.makeText(getApplicationContext(), "Invalid Input", Toast.LENGTH_LONG).show();
i = 0;
} else {
i = i / i1;
}
} else if (c == 3) {
i = i * i1;
} else {
i = 0;
}
txt.setText(i + "");
}
});
bclear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s = (String) txt.getText();
if (s.equals("+") || s.equals("-") || s.equals("/") || s.equals("*") || s.equals("")) {
i = 0;
} else {
i = Integer.parseInt(s);
i = i / 10;
}
if (i == 0) {
txt.setText("");
} else {
txt.setText(i + "");
}
s = null;
}
});
bce.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt.setText("");
i = 0;
i1 = 0;
s1 = "";
s2 = "";
c = -1;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

Related

I have an Issue in a Calculator app - how to fix it?

The Problem I have is how to change last operation by pressing the new operation the New Operation Will Override the previous
package com.zpat.calculator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity
extends AppCompatActivity {
private Button b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, badd, bsub, bmul, bdiv, bdot, bans, bclear;
private TextView ans, Info;
private final char ADDITION = '+';
private final char SUBTRACTION = '-';
private final char MULTIPLICATION = '*';
private final char DIVISION = '/';
private final char EQUAL = 0;
private String val1 = "";
private String val2 = "";
private char Action;
boolean isAnsPress = false;
boolean isDotPressed = false;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupUIViews();
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String a = ans.getText().toString();
if (a.length() <= 8) {
ans.setText(ans.getText().toString() + "1");
}
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String a = ans.getText().toString();
if (a.length() <= 8) {
ans.setText(ans.getText().toString() + "2");
}
}
});
b3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String a = ans.getText().toString();
if (a.length() <= 8) {
ans.setText(ans.getText().toString() + "3");
}
}
});
b4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String a = ans.getText().toString();
if (a.length() <= 8) {
ans.setText(ans.getText().toString() + "4");
}
}
});
b5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String a = ans.getText().toString();
if (a.length() <= 8) {
ans.setText(ans.getText().toString() + "5");
}
}
});
b6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String a = ans.getText().toString();
if (a.length() <= 8) {
ans.setText(ans.getText().toString() + "6");
}
}
});
b7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String a = ans.getText().toString();
if (a.length() <= 8) {
ans.setText(ans.getText().toString() + "7");
}
}
});
b8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String a = ans.getText().toString();
if (a.length() <= 8) {
ans.setText(ans.getText().toString() + "8");
}
}
});
b9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String a = ans.getText().toString();
if (a.length() <= 8) {
ans.setText(ans.getText().toString() + "9");
}
}
});
b0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String a = ans.getText().toString();
if (a.length() >= 1) {
TextUtils.isEmpty(a);
}
ans.setText(ans.getText().toString() + "0");
}
});
bdot.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!isDotPressed) {
ans.setText(ans.getText() + ".");
isDotPressed = true;
}
}
});
badd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
String a = Info.getText().toString().substring(Info.getText().toString().length() - 1, Info.getText().toString().length());
if (Action == ADDITION) {
compute();
Info.setText(String.valueOf(val1) + "+");
ans.setText(null);
}
} catch (Exception ex) {
ex.printStackTrace();
}
isAnsPress = false;
isDotPressed = false;
}
});
bsub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
compute();
Action = SUBTRACTION;
Info.setText(String.valueOf(val1) + "-");
ans.setText(null);
isAnsPress = false;
isDotPressed = false;
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
bmul.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
compute();
Action = MULTIPLICATION;
Info.setText(String.valueOf(val1) + "*");
ans.setText(null);
isAnsPress = false;
isDotPressed = false;
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
bdiv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
compute();
Action = DIVISION;
Info.setText(String.valueOf(val1) + "/");
ans.setText(null);
isAnsPress = false;
isDotPressed = false;
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
bans.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
if (!isAnsPress) {
compute();
Action = EQUAL;
Info.setText(Info.getText().toString() + String.valueOf(val2) + "=" + String.valueOf(val1));
ans.setText(null);
isAnsPress = true;
isDotPressed = false;
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
bclear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String a = ans.getText().toString();
if (ans.getText().length() > 0) {
CharSequence name = ans.getText().toString();
ans.setText(name.subSequence(0, name.length() - 1));
} else {
Info.setText(null);
val1 = "";
}
isAnsPress = false;
isDotPressed = false;
}
});
}
private void setupUIViews() {
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.btn1);
b2 = (Button) findViewById(R.id.btn_2);
b3 = (Button) findViewById(R.id.btn_3);
b4 = (Button) findViewById(R.id.btn4);
b5 = (Button) findViewById(R.id.btn_5);
b6 = (Button) findViewById(R.id.btn_6);
b7 = (Button) findViewById(R.id.btn_7);
b8 = (Button) findViewById(R.id.btn_8);
b9 = (Button) findViewById(R.id.btn_9);
b0 = (Button) findViewById(R.id.btn_0);
badd = (Button) findViewById(R.id.btn_Add);
bsub = (Button) findViewById(R.id.btn_Sub);
bmul = (Button) findViewById(R.id.btn_Multiply);
bdiv = (Button) findViewById(R.id.btn_div);
bdot = (Button) findViewById(R.id.btn_dot);
bans = (Button) findViewById(R.id.btn_equal);
bclear = (Button) findViewById(R.id.btn_Clear);
ans = (TextView) findViewById(R.id.Answer);
Info = (TextView) findViewById(R.id.info);
}
private void compute() {
if (val1 != "") {
val2 = ans.getText().toString();
switch (Action) {
case ADDITION:
val1 = String.valueOf(Double.parseDouble(val1) + Double.parseDouble(val2));
break;
case SUBTRACTION:
val1 = String.valueOf(Double.parseDouble(val1) - Double.parseDouble(val2));
break;
case MULTIPLICATION:
val1 = String.valueOf(Double.parseDouble(val1) * Double.parseDouble(val2));
break;
case DIVISION:
val1 = String.valueOf(Double.parseDouble(val1) / Double.parseDouble(val2));
break;
case EQUAL:
break;
}
} else {
val1 = ans.getText().toString();
}
double temp = Double.parseDouble(val1);
int temp2 = (int) temp;
double temp3 = temp - temp2;
if (temp3 == 0) {
val1 = String.valueOf((int) Double.parseDouble(val1));
}
}
}

Calculator:How to repeat the calculation by clicking on equal button and entering more than two numbers for calculate

I'm almost new to android and trying to write a calculator code.
It's almost done but I want to try some other options for that
when I repeat clicking on equal button,I want to repeat last calculation entered.for example 3+5=8 then if I click on exe button again it would be 13(3+5+5)
and so on(18,23,...)
Also I have problem with entering more than two numbers.for example when I enter (4+5+6)and then click equal the answer will appear 11 and just calculates last two numbers entered.I want it to show the result of first two numbers then get other numbers.example:for(2+3*4-5/)->the output will be equal to (5->20->15)
Here is my code;If any one can help,I will be grateful! :) thanks
package com.example.sony.calculator;
public class MainActivity extends AppCompatActivity {
Float firstNumber,secondNumber,result;
Float thirdNumber;
TextView display;
Button one,two,three,four,five,six,seven,eight,nine,zero,exe,clear,multiply,divide,sum,minus;
boolean isSum,isMinus,isMultiply,isDivide,isEqual;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display = (TextView) findViewById(R.id.khali);
one = (Button) findViewById(R.id.adade1);
two = (Button) findViewById(R.id.adade2);
three = (Button) findViewById(R.id.adade3);
four = (Button) findViewById(R.id.adade4);
five = (Button) findViewById(R.id.adade5);
six = (Button) findViewById(R.id.adade6);
seven = (Button) findViewById(R.id.adade7);
eight = (Button) findViewById(R.id.adade8);
nine = (Button) findViewById(R.id.adade9);
zero = (Button) findViewById(R.id.adade0);
multiply = (Button) findViewById(R.id.zarb);
divide = (Button) findViewById(R.id.taghsim);
sum = (Button) findViewById(R.id.jam);
minus = (Button) findViewById(R.id.menha);
exe = (Button) findViewById(R.id.mosavi);
clear = (Button) findViewById(R.id.pak);
final Button[] operators = new Button[5];
operators[0] = multiply;
operators[1] = divide;
operators[2] = sum;
operators[3] = minus;
operators[4] = exe;
final Button[] numbers = new Button[10];
numbers[0] = zero;
numbers[1] = one;
numbers[2] = two;
numbers[3] = three;
numbers[4] = four;
numbers[5] = five;
numbers[6] = six;
numbers[7] = seven;
numbers[8] = eight;
numbers[9] = nine;
for (int a = 0; a < 10; a++) {
final int finalA = a;
numbers[a].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
display.setText(display.getText().toString() + String.valueOf(finalA));
}
});
}
clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
display.setText("");
}
});
sum.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (display.getText() == "") {
display.setText("");
return;
}
else{
firstNumber = parseFloat(display.getText().toString());
isSum=true;
display.setText("");
return;
}
}
});
minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (display.getText()==""){
display.setText("");
}else {
firstNumber = parseFloat(display.getText().toString());
isMinus = true;
display.setText("");
}
}
});
multiply.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (display.getText()==""){
Toast Error1=Toast.makeText(MainActivity.this,"Error;Please enter right format",Toast.LENGTH_SHORT);
Error1.show();
}else {
firstNumber = parseFloat(display.getText().toString());
isMultiply = true;
display.setText("");
}
}
});
divide.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (display.getText() == "") {
Toast Error1 = Toast.makeText(MainActivity.this, "Error;Please enter right format", Toast.LENGTH_SHORT);
Error1.show();
} else {
firstNumber = parseFloat(display.getText().toString());
isDivide = true;
display.setText("");
}
}
});
exe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
isEqual=true;
if (display.getText() == "") {
display.setText("");
}
else {
secondNumber = parseFloat(display.getText().toString());
if (isSum == true) {
result = firstNumber + secondNumber ;
display.setText(String.valueOf(result));
isSum=false;
return;
}
if (isMinus == true) {
result = firstNumber - secondNumber;
display.setText(String.valueOf(result));
isMinus = false;
return;
}
if (isDivide == true) {
result = firstNumber / secondNumber;
display.setText(String.valueOf(result));
isDivide = false;
return;
}
if (isMultiply == true) {
result = firstNumber * secondNumber;
display.setText(String.valueOf(result));
isMultiply=false;
return;
}
}
}
});
}
}

How to transfer and save numbers

This code right here is a random math questionnaire and I want to know how to be able to transfer the amount of questions answered right and wrong to a separate stats page after each time they answer a question. I want the stats page to save the numbers so that if the user exits the program and then goes back on later they can still look at their total right answered questions and wrong answered questions. Ive been looking all over the internet and cant find a good way to learn this. If anyone has some advice I would really appreciate it. btw this is pretty much all the code in the main page; I didn't add the stats page code (because it has pretty much nothing.)
Pushme1-4 are the buttons and the AdditionEasyRight and AdditionEasyWrong are the number counts that are displayed on the main page.
public class AdditionEasy extends AppCompatActivity {
int countCNumAddE = 0;
int countWNumAddE = 0;
boolean hasAnswered;
public static final String MY_PREFS_NAME = "MyPrefsFile";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addition);
final TextView count = (TextView) findViewById(R.id.Count);
final TextView count2 = (TextView) findViewById(R.id.Count2);
Button homeButton = (Button) findViewById(R.id.homeButton);
super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
final TextView textOne = (TextView) findViewById(R.id.textView);
final TextView textTwo = (TextView) findViewById(R.id.textView2);
final Button pushMe1 = (Button) findViewById(R.id.button1);
final Button pushMe2 = (Button) findViewById(R.id.button2);
final Button pushMe3 = (Button) findViewById(R.id.button3);
final Button pushMe4 = (Button) findViewById(R.id.button4);
final Button begin = (Button) findViewById(R.id.begin);
begin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
hasAnswered = false;
pushMe1.setEnabled(true);
pushMe2.setEnabled(true);
pushMe3.setEnabled(true);
pushMe4.setEnabled(true);
begin.setVisibility(View.INVISIBLE);
pushMe1.setVisibility(View.VISIBLE);
pushMe2.setVisibility(View.VISIBLE);
pushMe3.setVisibility(View.VISIBLE);
pushMe4.setVisibility(View.VISIBLE);
pushMe1.setTextColor(Color.BLACK);
pushMe2.setTextColor(Color.BLACK);
pushMe3.setTextColor(Color.BLACK);
pushMe4.setTextColor(Color.BLACK);
pushMe1.setTextSize(20);
pushMe2.setTextSize(20);
pushMe3.setTextSize(20);
pushMe4.setTextSize(20);
textTwo.setText("");
String randGenChoice1 = "";
String randGenChoice2 = "";
String randGenChoice3 = "";
String randGenChoice4 = "";
String randText2 = "";
String randText3 = "";
Random RandomNum = new Random();
int randChoice1 = RandomNum.nextInt(40) + 1;
int randChoice2 = RandomNum.nextInt(40) + 1;
int randChoice3 = RandomNum.nextInt(40) + 1;
int randChoice4 = RandomNum.nextInt(40) + 1;
int rando2 = RandomNum.nextInt(20) + 1;
int rando3 = RandomNum.nextInt(20) + 1;
int pick = RandomNum.nextInt(4);
randGenChoice1 = Integer.toString(randChoice1);
randGenChoice2 = Integer.toString(randChoice2);
randGenChoice3 = Integer.toString(randChoice3);
randGenChoice4 = Integer.toString(randChoice4);
randText2 = Integer.toString(rando2);
randText3 = Integer.toString(rando3);
int value1;
int value2;
value1 = Integer.parseInt(randText2);
value2 = Integer.parseInt(randText3);
final int value = value1 + value2;
String line = randText2 + " + " + randText3;
textOne.setText(line);
final String answer;
answer = Integer.toString(value);
pushMe1.setText(randGenChoice1);
pushMe2.setText(randGenChoice2);
pushMe3.setText(randGenChoice3);
pushMe4.setText(randGenChoice4);
Button[] choice = {pushMe1, pushMe2, pushMe3, pushMe4};
Button display = choice[pick];
display.setText(answer);
pushMe1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int buttonAnswer = Integer.parseInt(pushMe1.getText().toString());
if (buttonAnswer == value) {
begin.setVisibility(View.VISIBLE);
textTwo.setText("Correct!");
textTwo.setTextColor(Color.BLACK);
pushMe1.setTextColor(Color.GREEN);
pushMe1.setTextSize(30);
if (hasAnswered != true) {
String AdditionEasyRight = Integer.toString(++countCNumAddE);
count.setText(AdditionEasyRight);
hasAnswered = true;
}
begin.setText("New Question");
begin.setTextSize(20);
pushMe2.setVisibility(View.INVISIBLE);
pushMe3.setVisibility(View.INVISIBLE);
pushMe4.setVisibility(View.INVISIBLE);
pushMe1.setEnabled(false);
pushMe2.setEnabled(false);
pushMe3.setEnabled(false);
pushMe4.setEnabled(false);
}else{
textTwo.setText("Wrong!");
textTwo.setTextColor(Color.BLACK);
pushMe1.setTextColor(Color.RED);
pushMe1.setTextSize(30);
if (hasAnswered != true) {
String AdditionEasyWrong = Integer.toString(++countWNumAddE);
count2.setText(AdditionEasyWrong);
hasAnswered = true;
}
pushMe1.setEnabled(false);
}
}
});
pushMe2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int buttonAnswer = Integer.parseInt(pushMe2.getText().toString());
if (buttonAnswer == value) {
begin.setVisibility(View.VISIBLE);
textTwo.setText("Correct!");
textTwo.setTextColor(Color.BLACK);
pushMe2.setTextColor(Color.GREEN);
pushMe2.setTextSize(30);
if (hasAnswered != true) {
String AdditionEasyRight = Integer.toString(++countCNumAddE);
count.setText(AdditionEasyRight);
hasAnswered = true;
}
begin.setText("New Question");
begin.setTextSize(20);
pushMe1.setVisibility(View.INVISIBLE);
pushMe3.setVisibility(View.INVISIBLE);
pushMe4.setVisibility(View.INVISIBLE);
pushMe1.setEnabled(false);
pushMe2.setEnabled(false);
pushMe3.setEnabled(false);
pushMe4.setEnabled(false);
}else{
textTwo.setText("Wrong!");
textTwo.setTextColor(Color.BLACK);
pushMe2.setTextColor(Color.RED);
pushMe2.setTextSize(30);
if (hasAnswered != true) {
String AdditionEasyWrong = Integer.toString(++countWNumAddE);
count2.setText(AdditionEasyWrong);
hasAnswered = true;
}
pushMe2.setEnabled(false);
}
}
});
pushMe3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int buttonAnswer = Integer.parseInt(pushMe3.getText().toString());
if (buttonAnswer == value) {
begin.setVisibility(View.VISIBLE);
textTwo.setText("Correct!");
textTwo.setTextColor(Color.BLACK);
pushMe3.setTextColor(Color.GREEN);
pushMe3.setTextSize(30);
if (hasAnswered != true) {
String AdditionEasyRight = Integer.toString(++countCNumAddE);
count.setText(AdditionEasyRight);
hasAnswered = true;
}
begin.setText("New Question");
begin.setTextSize(20);
pushMe1.setVisibility(View.INVISIBLE);
pushMe2.setVisibility(View.INVISIBLE);
pushMe4.setVisibility(View.INVISIBLE);
pushMe1.setEnabled(false);
pushMe2.setEnabled(false);
pushMe3.setEnabled(false);
pushMe4.setEnabled(false);
}else{
textTwo.setText("Wrong!");
textTwo.setTextColor(Color.BLACK);
pushMe3.setTextColor(Color.RED);
pushMe3.setTextSize(30);
if (hasAnswered != true) {
String AdditionEasyWrong = Integer.toString(++countWNumAddE);
count2.setText(AdditionEasyWrong);
hasAnswered = true;
}
pushMe3.setEnabled(false);
}
}
});
pushMe4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int buttonAnswer = Integer.parseInt(pushMe4.getText().toString());
if (buttonAnswer == value) {
begin.setVisibility(View.VISIBLE);
textTwo.setText("Correct!");
textTwo.setTextColor(Color.BLACK);
pushMe4.setTextColor(Color.GREEN);
pushMe4.setTextSize(30);
if (hasAnswered != true) {
String AdditionEasyRight = Integer.toString(++countCNumAddE);
count.setText(AdditionEasyRight);
hasAnswered = true;
}
begin.setText("New Question");
begin.setTextSize(20);
pushMe1.setVisibility(View.INVISIBLE);
pushMe2.setVisibility(View.INVISIBLE);
pushMe3.setVisibility(View.INVISIBLE);
pushMe1.setEnabled(false);
pushMe2.setEnabled(false);
pushMe3.setEnabled(false);
pushMe4.setEnabled(false);
}else{
textTwo.setText("Wrong!");
textTwo.setTextColor(Color.BLACK);
pushMe4.setTextColor(Color.RED);
pushMe4.setTextSize(30);
if (hasAnswered != true) {
String AdditionEasyWrong = Integer.toString(++countWNumAddE);
count2.setText(AdditionEasyWrong);
hasAnswered = true;
}
pushMe4.setEnabled(false);
}
}
});
}
});
homeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent homepage = new Intent(AdditionEasy.this , Menu.class);
startActivity(homepage);
}
});
}
}

Global variable reset to 0

I have a global variable inext set to a random value correctly before a call to setWordsQuestion(). As soon as I enter setWordsQuestion() the value is 0. Do you know how I can debug this?
and reset to 0:
Here is the java file:
public class ExercisesWords_fragment extends Fragment {
static Context context;
String login;
String fileMenuCard = null;
InputStreamReader isr;
InputStream fIn;
BufferedReader input_card = null;
int level, lang;
int ihelp;
String fileLogin;
RadioGroup rg;
RadioButton rb1, rb2, rb3, rb4;
EditText edt;
TextView txt, question;
Button validation, next, help;
int MAXWORDS = 42;
String[] words_questions = new String[MAXWORDS];
String[] words_cb1 = new String[MAXWORDS];
String[] words_cb2 = new String[MAXWORDS];
String[] words_cb3 = new String[MAXWORDS];
String[] words_cb4 = new String[MAXWORDS];
String[] words_response = new String[MAXWORDS];
int[] words_level = new int[MAXWORDS];
int MAXTEMP = 5;
int[] tmp;
int inext_question, inext;
public ExercisesWords_fragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.exercises_words_fragment, container, false);
}
#Override
public void onAttach(Activity activity){
super.onAttach(activity);
}
#Override
public void onPause() {
closeFileWords();
super.onPause();
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
context = getActivity().getApplicationContext();
fileMenuCard = "words";
login = getArguments().getString("login");
lang = getArguments().getInt("lang");
level = getArguments().getInt("level");
fileLogin = login + "_words.txt";
initViewFind();
initExerciseWords();
nextWordsQuestion();
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
edt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
validateResponse();
}
return false;
}
});
validation.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
validateResponse();
}
});
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
nextWordsQuestion();
}
});
help.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ihelp = 1;
hideKeyboard();
helpWords();
ihelp = 0;
}
});
}
private void drawBTN() {
if(lang == 0) {
validation.setText(R.string.pro_validation);
validation.setVisibility(View.VISIBLE);
next.setText(R.string.next);
next.setVisibility(View.VISIBLE);
help.setText(R.string.help);
help.setVisibility(View.VISIBLE);
}
else {
validation.setText(R.string.pro_validation_fr);
validation.setVisibility(View.VISIBLE);
next.setText(R.string.next_fr);
next.setVisibility(View.VISIBLE);
help.setText(R.string.help_fr);
help.setVisibility(View.VISIBLE);
}
}
private void nextWordsQuestion() {
int inext;
Random r;
r = new Random();
inext = r.nextInt(MAXWORDS);
if (inext_question < MAXTEMP) {
tmp[inext_question] = inext;
inext_question++;
setWordsQuestion();
return;
}
for (int i = 0; i < MAXTEMP; i++) {
if (inext == tmp[i]) {
r = new Random();
inext = r.nextInt(MAXWORDS);
i--;
}
}
System.arraycopy(tmp, 1, tmp, 0, MAXTEMP - 1);
tmp[MAXTEMP - 1] = inext;
setWordsQuestion();
}
private void setWordsQuestion(){
question.setText(words_questions[inext]);
question.setVisibility(View.VISIBLE);
edt.setText("");
rb1.setText(words_cb1[inext]);
rb2.setText(words_cb2[inext]);
rb3.setText(words_cb3[inext]);
rb4.setText(words_cb4[inext]);
rb1.setVisibility(View.INVISIBLE);
rb2.setVisibility(View.INVISIBLE);
rb3.setVisibility(View.INVISIBLE);
rb4.setVisibility(View.INVISIBLE);
rg.clearCheck();
hideKeyboard();
}
private void validateResponse(){
String response;
if(ihelp == 1){
int sel = rg.getCheckedRadioButtonId();
if(sel < 0) return;
RadioButton rb = (RadioButton) getActivity().findViewById(sel);
response = rb.getText().toString();
}
else {
response = edt.getText().toString();
}
TextView resp = (TextView) getActivity().findViewById(R.id.resp_words);
if(response.equals(words_response[inext])) {
next.setTextColor(Color.GREEN);
next.setVisibility(View.VISIBLE);
resp.setText(R.string.resp_card);
resp.setTextColor(Color.GREEN);
resp.setVisibility(View.VISIBLE);
}
else {
resp.setText(R.string.resp_card_no);
resp.setTextColor(Color.RED);
resp.setVisibility(View.VISIBLE);
}
}
private void helpWords(){
hideKeyboard();
rb1.setText(words_cb1[inext]);
rb1.setVisibility(View.VISIBLE);
rb2.setText(words_cb2[inext]);
rb2.setVisibility(View.VISIBLE);
rb3.setText(words_cb3[inext]);
rb3.setVisibility(View.VISIBLE);
rb4.setText(words_cb4[inext]);
rb4.setVisibility(View.VISIBLE);
}
private void hideKeyboard(){
EditText e = (EditText) getActivity().findViewById(R.id.words_answer);
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(e.getWindowToken(), 0);
}
private void openWords(){
String l = null;
try {
if (lang == 0)
fIn = context.getResources().getAssets()
.open(fileMenuCard+".txt", Context.MODE_PRIVATE);
else
fIn = context.getResources().getAssets()
.open(fileMenuCard+"_fr.txt", Context.MODE_PRIVATE);
isr = new InputStreamReader(fIn);
input_card = new BufferedReader(isr);
} catch (Exception e) {
e.getMessage();
}
for(int i=0;i<MAXWORDS;i++){
try {
l = input_card.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (l != null) {
StringTokenizer tokens = new StringTokenizer(l, ";");
words_questions[i] = tokens.nextToken();
words_level[i] = Integer.valueOf(tokens.nextToken());
words_cb1[i] = tokens.nextToken();
words_cb2[i] = tokens.nextToken();
words_cb3[i] = tokens.nextToken();
words_cb4[i] = tokens.nextToken();
words_response[i] = tokens.nextToken();
}
}
}
private void closeFileWords(){
try {
fIn.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
input_card.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void initExerciseWords(){
tmp = new int[MAXTEMP];
for(int i=0;i<MAXTEMP;i++)
tmp[i] = -1;
inext_question = 0;
openWords();
}
private void initViewFind(){
edt = (EditText) getActivity().findViewById(R.id.words_answer);
txt = (TextView) getActivity().findViewById(R.id.title_words);
validation = (Button) getActivity().findViewById(R.id.validation_words);
next = (Button) getActivity().findViewById(R.id.next_words);
help = (Button) getActivity().findViewById(R.id.help_words);
question = (TextView) getActivity().findViewById(R.id.words_question);
rg = (RadioGroup) getActivity().findViewById(R.id.rg_words);
rb1 = (RadioButton) getActivity().findViewById(R.id.radio1_words);
rb2 = (RadioButton) getActivity().findViewById(R.id.radio2_words);
rb3 = (RadioButton) getActivity().findViewById(R.id.radio3_words);
rb4 = (RadioButton) getActivity().findViewById(R.id.radio4_words);
if(lang == 0)
txt.setText(R.string.words_fr);
else
txt.setText(R.string.words_title_fr);
}
}
You are setting a local variable that hides the instance member :
private void nextWordsQuestion() {
int inext; // remove this line
...
inext = r.nextInt(MAXWORDS);
...
setWordsQuestion();
...
}
Therefore setWordsQuestion, which uses the instance member, doesn't see the value you set in nextWordsQuestion.
When you assign a random value to inext, you are assigning it to a local variable, not the global one.
To fix, simply remove the local variable.
private void nextWordsQuestion() {
//int inext;
Random r;
r = new Random();
inext = r.nextInt(MAXWORDS);
if (inext_question < MAXTEMP) {
tmp[inext_question] = inext;
inext_question++;
setWordsQuestion();
return;
}

how to add Double and Char arrays with each other in my scientific calculator program

i have two arrays.
double number[] = new double[5];
char oper[] = new char[4];
in my program when user press any operator sign like +,-,*,/ the number array is taking user input for example if he is entering 345 it is taking and saving it in number[0] and [0] become [1] and also save the current operation input from user and save it in oper[0] and so on.
but i dont know how can i get the result using both arrays.
i am pasting whole code here.
package com.example.calculatortesting;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity implements OnClickListener {
TextView textdisplay;
EditText currentcalc;
EditText et1;
double number[] = new double[5]; //for saving numbers in array
char oper[] = new char[4]; //for saving operation in array
int numposition = 0; //for position of number array
int operposition = 0; //for position of operation array
double currentnum; //saving current number before operation
int last_button = 0; //checking last button pressed
char operator; //saving pressed operation value
String newnum = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculator);
et1 = (EditText) findViewById(R.id.editText1);
textdisplay = (TextView) findViewById(R.id.editText1);
currentcalc = (EditText) findViewById(R.id.textView1);
Button b1 = (Button) findViewById(R.id.b1);
Button b2 = (Button) findViewById(R.id.b2);
Button b3 = (Button) findViewById(R.id.b3);
Button b4 = (Button) findViewById(R.id.b4);
Button b5 = (Button) findViewById(R.id.b5);
Button b6 = (Button) findViewById(R.id.b6);
Button b7 = (Button) findViewById(R.id.b7);
Button b8 = (Button) findViewById(R.id.b8);
Button b9 = (Button) findViewById(R.id.b9);
Button b0 = (Button) findViewById(R.id.b0);
Button multiply1 = (Button) findViewById(R.id.multiply);
Button divide1 = (Button) findViewById(R.id.divide);
Button plus1 = (Button) findViewById(R.id.plus);
Button minus1 = (Button) findViewById(R.id.minus);
Button equal1 = (Button) findViewById(R.id.equal);
Button clear1 = (Button) findViewById(R.id.clear);
Button back1 = (Button) findViewById(R.id.backspace);
Button dot1 = (Button) findViewById(R.id.decimal);
Button plusminus1 = (Button) findViewById(R.id.plusminus);
Button percent = (Button) findViewById(R.id.percent);
Button shift = (Button) findViewById(R.id.shift);
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);
b0.setOnClickListener(this);
multiply1.setOnClickListener(this);
divide1.setOnClickListener(this);
plus1.setOnClickListener(this);
minus1.setOnClickListener(this);
equal1.setOnClickListener(this);
clear1.setOnClickListener(this);
back1.setOnClickListener(this);
dot1.setOnClickListener(this);
plusminus1.setOnClickListener(this);
percent.setOnClickListener(this);
shift.setOnClickListener(this);
}
public void currentcalcmethod(String currentcalcoper) {
currentcalc.setText(currentcalc.getText() + currentcalcoper);
currentcalc.setSelection(currentcalc.getText().length());
}
void number() {
}
void oper() {
number[numposition] = currentnum;
oper[operposition] = operator;
numposition ++;
operposition++;
currentnum= 0;
}
void result (){
double total;
StringBuilder builder = new StringBuilder();
for (double num : number) {
for (char op : oper){
builder.append(num + op);
total = num+op;
et1.setText(Double.toString(total));
}}
}
public void shownum(String number) {
newnum = newnum + number ;
currentnum = Double.parseDouble(newnum);
et1.setText(Double.toString(currentnum));
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.b0) {
currentcalcmethod("0");
shownum("0");
} else if (v.getId() == R.id.b1) {
currentcalcmethod("1");
shownum("1");
} else if (v.getId() == R.id.b2) {
currentcalcmethod("2");
shownum("2");
} else if (v.getId() == R.id.b3) {
currentcalcmethod("3");
shownum("3");
} else if (v.getId() == R.id.b4) {
currentcalcmethod("4");
shownum("4");
} else if (v.getId() == R.id.b5) {
currentcalcmethod("5");
shownum("5");
} else if (v.getId() == R.id.b6) {
currentcalcmethod("6");
shownum("6");
} else if (v.getId() == R.id.b7) {
currentcalcmethod("7");
shownum("7");
} else if (v.getId() == R.id.b8) {
currentcalcmethod("8");
shownum("8");
} else if (v.getId() == R.id.b9) {
currentcalcmethod("9");
shownum("9");
} else if (v.getId() == R.id.plus) {
currentcalcmethod("+");
operator = '+';
oper();
} else if (v.getId() == R.id.minus) {
currentcalcmethod("-");
operator = '-';
oper();
} else if (v.getId() == R.id.percent) {
currentcalcmethod("%");
operator = '%';
oper();
} else if (v.getId() == R.id.divide) {
currentcalcmethod("/");
operator = '/';
oper();
} else if (v.getId() == R.id.multiply) {
currentcalcmethod("*");
operator = '*';
oper();
} else if (v.getId() == R.id.decimal) {
currentcalcmethod(".");
} else if (v.getId() == R.id.equal) {
result ();
currentcalcmethod("=");
} else if (v.getId() == R.id.backspace) {
if (currentcalc.getText().toString().length() > 0) {
int start =0;
int end2 = currentcalc.getText().toString().length() - 1;
String newText2 = currentcalc.getText().toString()
.substring(start, end2);
currentcalc.setText(newText2);
}
} else if (v.getId() == R.id.clear) {
textdisplay.setText("");
currentcalc.setText("");
number = null;
oper = null;
numposition = 0;
operposition = 0;
} else if (v.getId() == R.id.plusminus) {
}
last_button = v.getId();
}
}
Take the user input as a String and parse it. Something like:
String userInput = "3+5";
List<Character> numbers = new ArrayList<>();
List<Character> operators = new ArrayList<>();
for (char c : userInput.toCharArray()) {
if(Character.isDigit(c)){
numbers.add(c);
}
else{
operators.add(c);
}
}
// Now loop through lists and perform the desired arthimetic operation based on operator
You could put all of the inputs into a String and convert the string to a double to get an actual equation.
String ans = Double.toString(number[0]) +
Char.toString(oper[0]) + Double.toString(number[1]); //could use a loop to enter all values != null
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript"); // build-in javascript engine allows for string equations.
try {
System.out.println(engine.eval(ans));
} catch (ScriptException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Something like that.

Categories