I have code that was working earlier and now seems to be throwing some errors with regard to a case statement I have for a button event. The case structure looks at which button is clicked and does something and it’s the second case that is aggravating me. The error in Eclipse on the line that says case R.id.button2: is telling me “syntax error on token “case”, @ expected” and on the line below it (Intent), it is saying “syntax error, insert “;” to complete assert statement”. However, it seems like the error messages keep changing. Earlier, Eclipse was nagging me to put a “;” instead of a @ and I’m not sure why since the code was fine earlier. I’ve tried putting “;” after the colon in the second case line and still throws an error. I’m not sure why it’s happening all of a sudden or why the error messages keep changing, despite typing what it wants (which usually fixes things in Eclipse.) Did I miss something in my case structure? Thanks.
Here is my code (posting in full):
import java.text.DecimalFormat;
import java.text.NumberFormat;
import android.R.integer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
public class MainActivity extends Activity {
EditText mile,diesel;
Button button1, button2;
TextView tv, tv2, tv3;
private double x, y, z, costper, gallon, litres, ophours, ophour, drive, stopdrive;
CheckBox checkBox1, checkBox2, checkBox3, checkBox4;
NumberFormat format = NumberFormat.getCurrencyInstance();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mile= (EditText) findViewById(R.id.mile);
checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
checkBox3 = (CheckBox) findViewById(R.id.checkBox3);
checkBox4 = (CheckBox) findViewById(R.id.checkBox4);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(onClickListener);
button2.setOnClickListener(onClickListener);
tv = (TextView) findViewById(R.id.cost);
tv2 = (TextView) findViewById(R.id.cpm);
tv3 = (TextView) findViewById(R.id.gallons);
diesel= (EditText) findViewById(R.id.diesel);
}
private OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(final View v) {
switch(v.getId()){
case R.id.button1:
x=Double.parseDouble(mile.getText().toString());
y=Double.parseDouble(diesel.getText().toString());
ophour = x/55;
ophours = 0;
if (ophour>10){
drive = ophour/10;
if (drive>1) {
stopdrive = drive-1;
ophour = ophour + 10;
if (stopdrive>1);
ophour = ophour + (10*stopdrive);
if (stopdrive<1);
ophour = ophour + 10;
}
}
ophours = ((x/55)*10);
}
if (checkBox2.isChecked()) {
x=x*2;
}
if (checkBox1.isChecked()) {
x=x*0.62137;
}
gallon = x/5.5;
if (checkBox4.isChecked()) {
gallon = gallon + (ophour*1.1);
}
if (checkBox3.isChecked()) {
litres = gallon*3.785;
tv3.setText(new DecimalFormat("####.##").format(litres)+"L");
}
z=(gallon*y)+(x*0.655);
costper=z/x;
tv.setText(format.format(z));
tv2.setText(format.format(costper)+"/mile");
tv3.setText(new DecimalFormat("####.##").format(gallon)+"gal.");
break;
case R.id.button2:
Intent browserIntent =
new Intent(Intent.ACTION_VIEW, Url.parse("http://www.google.com"));
startActivity(browserIntent);
break;
}
;};}
Your
{}s are simply mismatched. This is an excellent example of why it is important to properly format your code, including indentation.I’ve taken the time to do this for you, though you can do it in Eclipse by using Control+Shift+F:
Now, I’ve added 4 comments in here to show you issues with the code (though only 2 are related to this issue).
}that should not be there.}, and there should be. (End ofswitch.)ifstatement which will always execute due to;.ifstatement which will always execute due to;.