如何比较if循环中的两个int变量

时间:2014-05-09 13:10:36

标签: java android if-statement int equals

我的代码有点问题。我想在if循环中compare 2 int variables。我知道logical operator == and method .equals();

之间存在一些差异

我的代码是:

代码的一部分问题出在哪里:

问题是result02==var。我没有从Eclipse logcat收到任何错误。

我尝试使用result02.equals(var)但不要跑。

else{
         if (conferma_text.getText().toString().equals(password_text.getText().toString())){
            SQLiteDatabase db = dbLogin.getWritableDatabase();
            int result02 = verifyL.verify(db, login_text.getText().toString(), password_text.getText().toString());
            int var =-1;
            if(result02==var){

检查我的完整代码:

package com.example.prenotazione_esame;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
import android.widget.Toast;
import android.database.sqlite.SQLiteDatabase;
import android.content.ContentValues;
import android.content.DialogInterface;

public class CreateAccountActivity extends Activity {

    private LoginDataBase dbLogin;
    VerifyLogin verifyL = new VerifyLogin();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dbLogin = new LoginDataBase(this);
        setContentView(R.layout.createaccount);
        Button button_crea02 = (Button) findViewById(R.id.Create02);
        button_crea02.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                executeCreate();
            }
        });
    }

    //Per prima cosa si verifica che tutte le EdiText siano compilate, in caso negativo
    //una notifica toast avvisa l'utente dell'errore.
    private void executeCreate(){
       EditText login_text = (EditText) findViewById(R.id.UserName02);
       EditText password_text = (EditText) findViewById(R.id.Password02);
       EditText conferma_text = (EditText) findViewById(R.id.Conferma);
       EmptyField emptyf = new EmptyField();
       String result=emptyf.CreateEmpty(login_text, password_text, conferma_text);
       if (result.equals("Empty")){
            Toast toast = Toast.makeText(this,"Fornire i dati richiesti", Toast.LENGTH_SHORT);
            toast.show();
       }
       //Se le EditText sono compilate si verifica che le credenziali di accesso non
       //siano già associate ad un altro utente, in tal caso si avvisa l'utente con una
       //notifica toast. Se le credenziali sono corrette si procede al loro inserimento
       //nel DB.
       else{
         if (conferma_text.getText().toString().equals(password_text.getText().toString())){
            SQLiteDatabase db = dbLogin.getWritableDatabase();
            int result02 = verifyL.verify(db, login_text.getText().toString(), password_text.getText().toString());
            int var =-1;
            if(result02==var){

                 ContentValues values = new ContentValues();
                 values.put("USERNAME",login_text.getText().toString());
                 values.put("PASSWORD",password_text.getText().toString());      
                 long id = db.insert("T_LOGIN", null, values);
                 if (id != -1){
                     Toast toast = Toast.makeText(this,"Record creato", Toast.LENGTH_SHORT);
                     toast.show();
                 }
             }
            else{
                //Se username e password esistono già viene lanciata un AlertDialog. Premendo Ok
                //si puliscono le EditText
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Attenzione");
                builder.setMessage("Username e Password invalidi");
                builder.setCancelable(false);
                builder.setPositiveButton("Ok", new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which){
                        EditText login_text = (EditText) findViewById(R.id.UserName02);
                        EditText password_text = (EditText) findViewById(R.id.Password02);
                        EditText conferma_text = (EditText) findViewById(R.id.Conferma);
                        login_text.setText("");
                        password_text.setText("");
                        conferma_text.setText("");
                        dialog.cancel();                        
                    }
                });
                AlertDialog alert = builder.create();
            }
         }
         //Se la password inserita e quella di conferma non corrispondono si avvisa l'utente
         else{
            Toast toast = Toast.makeText(this,"Le password non corrispondono", Toast.LENGTH_SHORT);
            toast.show();
         }
       }
    }
}

3 个答案:

答案 0 :(得分:0)

对于诸如 - int,boolean,long等原语 - 你应该使用==

原语不是对象,所以你不能在它们上调用方法。

答案 1 :(得分:0)

首先检查它是否真的进入

 if (conferma_text.getText().toString().equals(password_text.getText().toString())){

//Use log here to check 

}

答案 2 :(得分:0)

原语不使用.equals()。在他们身上使用你的==。如果你想使用.equals()方法,它们必须放入包装类(即Integer)。然后你可以使用.equals()。