android java syntax error in If statement
My apk is giving me two errors, down from 26 earlier this morning, but I dont seem to grasp the solution. First question is there a good way/trick for keeping track of open and close parens and open and closed brackets? Second question Is there something fundamentally wrong with my code? The first error is at private Button btnExecute; I think I am missing a closed bracket some where but dont know where.. When this worked last night I did not have a closed bracket near that line The second error is closed bracket two lines up from end onClick. I have a feeling it is missing a semicolon, paren or bracket. The goal is read the value displayed in a spinner and use if statements to determine which routine to run. Inside each routine I will check certain EditText for nulls or blanks and if not blank then a particular formula based on the spinner entry.
package com.androidbook.triviaquiz8;
import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
public class QuizHelpActivity extends QuizActivity {
private EditText m1_sqs1;
private EditText m1_rs1;
private EditText m1_rs2;
private EditText m1_cd;
private EditText m1_els1;
private EditText m1_els2;
private EditText m1_sqs1_eng;
private EditText m1_rs1_eng;
private EditText m1_rs2_eng;
private EditText m1_cd_eng;
private EditText m1_els1开发者_JAVA技巧_eng;
private EditText m1_els2_eng;
private EditText m1_sqs1_met;
private EditText m1_rs1_met;
private EditText m1_rs2_met;
private EditText m1_cd_met;
private EditText m1_els1_met;
private EditText m1_els2_met;
private EditText m1_sa_in;
private EditText m1_sa_ft;
private EditText m1_sa_mm;
private EditText m1_sa_m;
private EditText m1_ed_in;
private EditText m1_ed_ft;
private EditText m1_ed_mm;
private EditText m1_ed_m;
private Spinner m1_ss_spinner;
private Spinner m1_sqs1_spinner;
private Spinner m1_rs1_spinner;
private Spinner m1_rs2_spinner;
private Spinner m1_cd_spinner;
private Spinner m1_els1_spinner;
private Spinner m1_els2_spinner;
private QuizHelpActivity mContext;
private Button btnSave;
private Button btnClear;
private Button btnExecute;
//
// onCreate - let the fun begin!
//
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
mContext = this;
setContentView(R.layout.help);
m1_sqs1 = (EditText) findViewById(R.id.m1_sqs1);
m1_rs1 = (EditText) findViewById(R.id.m1_rs1);
m1_rs2 = (EditText) findViewById(R.id.m1_rs2);
m1_cd = (EditText) findViewById(R.id.m1_cd);
m1_els1 = (EditText) findViewById(R.id.m1_els1);
m1_els2 = (EditText) findViewById(R.id.m1_els2);
m1_sqs1_eng = (EditText) findViewById(R.id.m1_sqs1);
m1_rs1_eng = (EditText) findViewById(R.id.m1_rs1);
m1_rs2_eng = (EditText) findViewById(R.id.m1_rs2);
m1_cd_eng = (EditText) findViewById(R.id.m1_cd);
m1_els1_eng = (EditText) findViewById(R.id.m1_els1);
m1_els2_eng = (EditText) findViewById(R.id.m1_els2);
m1_sqs1_met = (EditText) findViewById(R.id.m1_sqs1);
m1_rs1_met = (EditText) findViewById(R.id.m1_rs1);
m1_rs2_met = (EditText) findViewById(R.id.m1_rs2);
m1_cd_met = (EditText) findViewById(R.id.m1_cd);
m1_els1_met = (EditText) findViewById(R.id.m1_els1);
m1_els2_met = (EditText) findViewById(R.id.m1_els2);
m1_sa_in = (EditText) findViewById(R.id.m1_sa_in);
m1_sa_ft = (EditText) findViewById(R.id.m1_sa_ft);
m1_sa_mm = (EditText) findViewById(R.id.m1_sa_mm);
m1_sa_m = (EditText) findViewById(R.id.m1_sa_m);
m1_ed_in = (EditText) findViewById(R.id.m1_ed_in);
m1_ed_ft = (EditText) findViewById(R.id.m1_ed_ft);
m1_ed_mm = (EditText) findViewById(R.id.m1_ed_mm);
m1_ed_m = (EditText) findViewById(R.id.m1_ed_m);
//
// Create an OnClick Event in each button.
//
Button btnExecute = (Button) findViewById(R.id.btnExecute);
Button btnSave = (Button) findViewById(R.id.btnSave);
Button btnClear = (Button) findViewById(R.id.btnClear);
btnSave.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
}
});
btnClear.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
}
});
btnExecute.setOnClickListener(new View.OnClickListener() {
private AlertDialog show;
public void onClick(View arg0) {
AdapterView<SpinnerAdapter> m1ssspinner;
if (((m1ssspinner.getSelectedItem() == "Square"))) {
if ((m1_sqs1.getText().length() == 0)
|| (m1_sqs1.getText().toString() == " ")) {
show = new AlertDialog.Builder(mContext).setTitle("Error")
.setMessage("The Square Side length is empty")
.setPositiveButton("OK", null).show();
} else {
double result = new Double(m1_sqs1.getText().toString()) * new Double(m1_sqs1.getText().toString());
m1_sa_in.setText(Double.toString(result));
}
if (((m1ssspinner.getSelectedItem() == "Rectangle"))) {
if ((m1_rs1.getText().length() == 0)
|| (m1_rs1.getText().toString() == " ")
|| (m1_rs1.getText().toString() == " ")
|| (m1_rs1.getText().length() == 0)){
show = new AlertDialog.Builder(mContext).setTitle("Error")
.setMessage("A Rectangle Side length is empty")
.setPositiveButton("OK", null).show();
} else {
double result = new Double(m1_rs1.getText().toString()) * new Double(m1_rs2.getText().toString());
m1_sa_in.setText(Double.toString(result));
}
if (((m1ssspinner.getSelectedItem() == "Circle"))) {
if ((m1_cd.getText().length() == 0)
|| (m1_cd.getText().toString() == " ")
|| (m1_cd.getText().toString() == " ")){
show = new AlertDialog.Builder(mContext).setTitle("Error")
.setMessage("The Circle Diameter is empty")
.setPositiveButton("OK", null).show();
} else {
double result = new Double(m1_cd.getText().toString()) * new Double(m1_cd.getText().toString());
m1_sa_in.setText(Double.toString(result));
}
if (((m1ssspinner.getSelectedItem() == "Elliptical"))) {
if ((m1_els1.getText().length() == 0)
|| (m1_els1.getText().toString() == " ")
|| (m1_els2.getText().toString() == 0)
|| (m1_els2.getText().toString() == " ")){
show = new AlertDialog.Builder(mContext).setTitle("Error")
.setMessage("An Elliptical Diameter is empty")
.setPositiveButton("OK", null).show();
} else {
double result = new Double(m1_els1.getText().toString()) * new Double(m1_els2.getText().toString());
m1_sa_in.setText(Double.toString(result));
}
);
//
// End OnClick Event in each button.
//
//
// check media availability
//
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;} else {
// Something else is wrong. It may be one of many other states, but all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
//
// end check media availability
//
//
// create METHOD 1 STACK SHAPE SPINNER (m1_ss_spinner) with displayed text 'centered'
//
Spinner m1ssspinner = (Spinner)findViewById(R.id.m1_ss_spinner);
ArrayAdapter<CharSequence> m1ssadapter = ArrayAdapter.createFromResource(this, R.array.m1_ss_list, R.layout.my_normal_spinner_style);
m1ssadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
m1ssspinner.setAdapter(m1ssadapter);
//
// get METHOD 1 STACK SHAPE UNIT (m1_ss_unit)
//
Object m1_ss_unit = (m1ssspinner.getSelectedItem());
//
// create METHOD 1 SQUARE_SIDE1 SPINNER (m1_sqs1_spinner) with displayed text 'centered'
//
Spinner m1sqs1spinner = (Spinner)findViewById(R.id.m1_sqs1_spinner);
ArrayAdapter<CharSequence> m1sqs1adapter = ArrayAdapter.createFromResource(this, R.array.m1_sqs1_list, R.layout.my_normal_spinner_style);
m1sqs1adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
m1sqs1spinner.setAdapter(m1sqs1adapter);
//
// create METHOD 1 RECTANGLE_SIDE1 SPINNER (m1_rs1_spinner) with displayed text 'centered'
//
Spinner m1rs1spinner = (Spinner)findViewById(R.id.m1_rs1_spinner);
ArrayAdapter<CharSequence> m1rs1adapter = ArrayAdapter.createFromResource(this, R.array.m1_rs1_list, R.layout.my_normal_spinner_style);
m1rs1adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
m1rs1spinner.setAdapter(m1rs1adapter);
//
// create METHOD 1 RECTANGLE_SIDE2 SPINNER (m1_rs2_spinner) with displayed text 'centered'
//
Spinner m1rs2spinner = (Spinner)findViewById(R.id.m1_rs2_spinner);
ArrayAdapter<CharSequence> m1rs2adapter = ArrayAdapter.createFromResource(this, R.array.m1_rs2_list, R.layout.my_normal_spinner_style);
m1rs2adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
m1rs2spinner.setAdapter(m1rs2adapter);
//
// create METHOD 1 CIRCULAR DIAMETER SPINNER (m1_cd_spinner) with displayed text 'centered'
//
Spinner m1cdspinner = (Spinner)findViewById(R.id.m1_cd_spinner);
ArrayAdapter<CharSequence> m1cdadapter = ArrayAdapter.createFromResource(this, R.array.m1_cd_list, R.layout.my_normal_spinner_style);
m1cdadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
m1cdspinner.setAdapter(m1cdadapter);
//
// create METHOD 1 ELLIPTICAL DIAMETER1 SPINNER (m1_cd_spinner) with displayed text 'centered'
//
Spinner m1els1spinner = (Spinner)findViewById(R.id.m1_els1_spinner);
ArrayAdapter<CharSequence> m1els1adapter = ArrayAdapter.createFromResource(this, R.array.m1_els1_list, R.layout.my_normal_spinner_style);
m1els1adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
m1els1spinner.setAdapter(m1els1adapter);
//
// create METHOD 1 ELLIPTICAL DIAMETER2 SPINNER (m1_cd_spinner) with displayed text 'centered'
//
Spinner m1els2spinner = (Spinner)findViewById(R.id.m1_els2_spinner);
ArrayAdapter<CharSequence> m1els2adapter = ArrayAdapter.createFromResource(this, R.array.m1_els2_list, R.layout.my_normal_spinner_style);
m1els2adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
m1els2spinner.setAdapter(m1cdadapter);
}
There are so many problems here I don't even know where to begin. This wont compile of course because you have unbalanced {}, but thats not the least of your problems
Your very first if/then includes all the others and precludes any of the others from ever resolving to true for instance
if (((m1ssspinner.getSelectedItem() == "Square")))
Encapsulates all the other tests such as the next one which is
if (((m1ssspinner.getSelectedItem() == "Rectangle")))
If the first is true, the second will never be.
You also have local and class scoped variables of different types with the same name and the local is not initialized (or of the right type)
AdapterView<SpinnerAdapter> m1ssspinner; // Probably not what you want to use ...
Spinner m1ssspinner = (Spinner) ...
Then you have blocks with duplicate tests
if ((m1_rs1.getText().length() == 0) // Same test twice!
|| (m1_rs1.getText().toString() == " ")
|| (m1_rs1.getText().toString() == " ")
|| (m1_rs1.getText().length() == 0))
Or this one where you are doing same, but also testing a string against an int
if (((m1ssspinner.getSelectedItem() == "Elliptical"))) {
if ((m1_els1.getText().length() == 0)
|| (m1_els1.getText().toString() == " ")
|| (m1_els2.getText().toString() == 0) // you probably meant int!
|| (m1_els2.getText().toString() == " ")){
Also your tests are comparing objects returned from the spinner with string literals (they wont match)
You are intializing your view classes at the top level of the class vs. in the oncreate after setcontent view etc. etc. etc.
Anyway this block at least will compile, but this program simply wont work as written, you still have a heck of lot things to work out.
My advice is to take this one step at a time, if you are finding that you have rats nest of if/then you probably need to rethink the solution
btnExecute.setOnClickListener(new View.OnClickListener()
{
private AlertDialog show;
public void onClick(View arg0)
{
//AdapterView<SpinnerAdapter> m1ssspinner; // Probably you meant to use the class scoped of same name
if (((m1ssspinner.getSelectedItem() == "Square"))) {
if ((m1_sqs1.getText().length() == 0)
|| (m1_sqs1.getText().toString() == " ")) {
show = new AlertDialog.Builder(mContext).setTitle("Error")
.setMessage("The Square Side length is empty")
.setPositiveButton("OK", null).show();
} else
{
double result = new Double(m1_sqs1.getText().toString()) * new Double(m1_sqs1.getText().toString());
m1_sa_in.setText(Double.toString(result));
}
} else
if (((m1ssspinner.getSelectedItem() == "Rectangle"))) {
if ((m1_rs1.getText().length() == 0)
|| (m1_rs1.getText().toString() == " ")
|| (m1_rs1.getText().toString() == " ")
|| (m1_rs1.getText().length() == 0)) {
show = new AlertDialog.Builder(mContext).setTitle("Error")
.setMessage("A Rectangle Side length is empty")
.setPositiveButton("OK", null).show();
} else
{
double result = new Double(m1_rs1.getText().toString()) * new Double(m1_rs2.getText().toString());
m1_sa_in.setText(Double.toString(result));
}
} else
if (((m1ssspinner.getSelectedItem() == "Circle"))) {
if ((m1_cd.getText().length() == 0)
|| (m1_cd.getText().toString() == " ")
|| (m1_cd.getText().toString() == " "))
{
show = new AlertDialog.Builder(mContext).setTitle("Error")
.setMessage("The Circle Diameter is empty")
.setPositiveButton("OK", null).show();
} else
{
double result = new Double(m1_cd.getText().toString()) * new Double(m1_cd.getText().toString());
m1_sa_in.setText(Double.toString(result));
}
} else
if ((m1ssspinner.getSelectedItem() == "Elliptical")) {
if ((m1_els1.getText().length() == 0)
|| (m1_els1.getText().toString() == " ")
|| (m1_els2.getText().length() == 0)
|| (m1_els2.getText().toString() == " "))
{
show = new AlertDialog.Builder(mContext).setTitle("Error")
.setMessage("An Elliptical Diameter is empty")
.setPositiveButton("OK", null).show();
} else {
double result = new Double(m1_els1.getText().toString()) * new Double(m1_els2.getText().toString());
m1_sa_in.setText(Double.toString(result));
}
}
}
}
);
The following have no closing bracket ...
btnExecute.setOnClickListener(new View.OnClickListener() { ...
public void onClick(View arg0) { ...
if (((m1ssspinner.getSelectedItem() == "Square"))) { ...
if (((m1ssspinner.getSelectedItem() == "Rectangle"))) { ...
if (((m1ssspinner.getSelectedItem() == "Circle"))) { ...
if (((m1ssspinner.getSelectedItem() == "Elliptical"))) { ...
Your btnExecute.setOnClickListener should look like this
btnExecute.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
... your if statements ...
}
});
Below seems wrong. put AlertDialog show inside the function
btnExecute.setOnClickListener(new View.OnClickListener()
{
private AlertDialog show;
public void onClick(View arg0)
For errors with brackets. I suggest you go through and set a comment with a number.
Add 1 if you open a bracket, and subtract 1 if you have a close bracket.
This will help you overcome the problem in the future:
if(true)
{ //1
while(true)
{ //2
} //1
} //0
problem is in your btnExecute.you didn't put a '}'.See after last else in btnExecute's you have to put another }
精彩评论