Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I'm writing a code for a calculator. I'm having an error
"else without if"
at the
last "else" in the code
. If you could help me figure out what it is I would be grateful :)
ArrayList<String> arrayList = new ArrayList<String>();
String string = "";
String string1 = "";
public void onClick1(View v) {
TextView textView2 = (TextView) findViewById(R.id.textView2);
Button button = (Button) v;
string = (String) button.getText().toString();
if (!string.contains("+") && !string.contains("-") && !string.contains("*") && !string.contains("/")) {
string1 = string1 + string;
if (arrayList.size() > 0) {
arrayList.remove((arrayList.size() - 1));
arrayList.add(string1);
} else {
arrayList.add(string);
arrayList.add(string);
string1 = "";
//textView2.setText(textView2.getText().toString()+string);
textView2.setText(arrayList.toString());
public void onClick(View v) {
TextView textView1 = (TextView) findViewById(R.id.textView);
int calc = 0;
int c = arrayList.size();
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));
arrayList.remove(2);
arrayList.remove(2);
arrayList.remove(2);
arrayList.add(2, Integer.toString(calc));
c = arrayList.size();
} 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));
arrayList.remove(0);
arrayList.remove(0);
arrayList.remove(0);
arrayList.add(0, Integer.toString(calc));
c = arrayList.size();
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));
arrayList.remove(0);
arrayList.remove(0);
arrayList.remove(0);
arrayList.remove(0);
arrayList.add(0, Integer.toString(calc));
c = arrayList.size();
textView1.setText(Integer.toString(calc));
public void clear(View v) {
TextView textView1 = (TextView) findViewById(R.id.textView);
TextView textView2 = (TextView) findViewById(R.id.textView2);
string1 = "";
string = "";
textView1.setText("0");
textView2.setText("");
arrayList.clear();
–
You can check this code of onClick()
method. You better format your code. I have added comments to point out issues.
if (arrayList.get(1).contains("*"))
calc = Integer.parseInt(arrayList.get(0)) * Integer.parseInt(arrayList.get(2));
{ // <----------------------------this is redundant
if (arrayList.get(1).contains("/"))
calc = Integer.parseInt(arrayList.get(0)) / Integer.parseInt(arrayList.get(2));
arrayList.remove(0); // this code is not in above if block.
arrayList.remove(0);
arrayList.remove(0);
arrayList.add(0, Integer.toString(calc));
c = arrayList.size();
else { // <----------------------------this else is without if
Note: I haven't checked your logic.
Refer: The if-then and if-then-else Statements, it states that
The opening and closing braces are optional, provided that the "then" clause contains only one statement
Properly indented, your code looks like this:
ArrayList<String> arrayList = new ArrayList<String>();
String string = "";
String string1 = "";
public void onClick1(View v) {
TextView textView2 = (TextView) findViewById(R.id.textView2);
Button button = (Button) v;
string = (String) button.getText().toString();
if (!string.contains("+") && !string.contains("-") && !string.contains("*") && !string.contains("/")) {
string1 = string1 + string;
if (arrayList.size() > 0) {
arrayList.remove((arrayList.size() - 1));
arrayList.add(string1);
} else {
arrayList.add(string);
arrayList.add(string);
string1 = "";
//textView2.setText(textView2.getText().toString()+string);
textView2.setText(arrayList.toString());
public void onClick(View v) {
TextView textView1 = (TextView) findViewById(R.id.textView);
int calc = 0;
int c = arrayList.size();
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));
arrayList.remove(2);
arrayList.remove(2);
arrayList.remove(2);
arrayList.add(2, Integer.toString(calc));
c = arrayList.size();
} 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));
arrayList.remove(0);
arrayList.remove(0);
arrayList.remove(0);
arrayList.add(0, Integer.toString(calc));
c = arrayList.size();
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));
arrayList.remove(0);
arrayList.remove(0);
arrayList.remove(0);
arrayList.remove(0);
arrayList.add(0, Integer.toString(calc));
c = arrayList.size();
textView1.setText(Integer.toString(calc));
public void clear(View v) {
TextView textView1 = (TextView) findViewById(R.id.textView);
TextView textView2 = (TextView) findViewById(R.id.textView2);
string1 = "";
string = "";
textView1.setText("0");
textView2.setText("");
arrayList.clear();
It should now be reasonably easy to see that you're missing curly brackets in certain places.
Something of note: sometimes you're doing this:
if(something)
dosomething();
Whilst this is technically fine, it can sneak errors into your code (say if you add something else later). My advice is to err on the side of caution and always use brackets - so it becomes more like this:
if(something){
doSomething();
–
I have just removed error by formatting your code and adding curly braces at two places :
Please check if both braces at proper position for your logic:
Here both are highlighted :
ArrayList<String> arrayList = new ArrayList<String>();
String string = "";
String string1 = "";
public void onClick1(View v) {
TextView textView2 = (TextView) findViewById(R.id.textView2);
Button button = (Button) v;
string = (String) button.getText().toString();
if (!string.contains("+") && !string.contains("-") && !string.contains("*") && !string.contains("/")) {
string1 = string1 + string;
if (arrayList.size() > 0) {
arrayList.remove((arrayList.size() - 1));
arrayList.add(string1);
} else {
arrayList.add(string);
arrayList.add(string);
string1 = "";
//textView2.setText(textView2.getText().toString()+string);
textView2.setText(arrayList.toString());
public void onClick(View v) {
TextView textView1 = (TextView) findViewById(R.id.textView);
int calc = 0;
int c = arrayList.size();
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));
arrayList.remove(2);
arrayList.remove(2);
arrayList.remove(2);
arrayList.add(2, Integer.toString(calc));
c = arrayList.size();
} 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("*")) {// added here
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));
arrayList.remove(0);
arrayList.remove(0);
arrayList.remove(0);
arrayList.add(0, Integer.toString(calc));
c = arrayList.size();
// added below before else word
} 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));
arrayList.remove(0);
arrayList.remove(0);
arrayList.remove(0);
arrayList.remove(0);
arrayList.add(0, Integer.toString(calc));
c = arrayList.size();
textView1.setText(Integer.toString(calc));
public void clear(View v) {
TextView textView1 = (TextView) findViewById(R.id.textView);
TextView textView2 = (TextView) findViewById(R.id.textView2);
string1 = "";
string = "";
textView1.setText("0");
textView2.setText("");
arrayList.clear();
} //removed from here
Thanks
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.