java strings_Comparing strings in Java

问题

Im trying to compare the values of two edittext boxes. What i would like is to just compare passw1 = passw2. As my code is now comparing two strings i have entered as i could not get to compare them.

final EditText passw1= (EditText) findViewById(R.id.passw1);

final EditText passw2= (EditText) findViewById(R.id.passw2);

Button buttoks = (Button) findViewById(R.id.Ok);

buttoks.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

if (passw1.toString().equalsIgnoreCase("1234") && passw2.toString().equalsIgnoreCase("1234")){

Toast.makeText(getApplication(),"Username and password match", Toast.LENGTH_SHORT).show();

}

else {

Toast.makeText(getApplication(),"Username and password doesn't match", Toast.LENGTH_SHORT).show();

}

} });

回答1:

[EDIT]

I made a mistake earlier, because, to get the text, you need to use .getText().toString().

Here is a full working example:

package com.psegina.passwordTest01;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.LinearLayout;

import android.widget.Toast;

public class Main extends Activity implements OnClickListener {

LinearLayout l;

EditText user;

EditText pwd;

Button btn;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

l = new LinearLayout(this);

user = new EditText(this);

pwd = new EditText(this);

btn = new Button(this);

l.addView(user);

l.addView(pwd);

l.addView(btn);

btn.setOnClickListener(this);

setContentView(l);

}

public void onClick(View v){

String u = user.getText().toString();

String p = pwd.getText().toString();

if( u.equals( p ) )

Toast.makeText(getApplicationContext(), "Matches", Toast.LENGTH_SHORT).show();

else

Toast.makeText(getApplicationContext(), user.getText()+" != "+pwd.getText(), Toast.LENGTH_SHORT).show();

}

}

Original answer (Will not work because of the lack of toString())

Try using .getText() instead of .toString().

if( passw1.getText() == passw2.getText() )

#do something

.toString() returns a String representation of the whole object, meaning it won't return the text you entered in the field (see for yourself by adding a Toast which will show the output of .toString())

回答2:

Using the == operator will compare the references to the strings not the string themselves.

Ok, you have to toString() the Editable. I loaded up some of the code I had before that dealt with this situation.

String passwd1Text = passw1.getText().toString();

String passwd2Text = passw2.getText().toString();

if (passwd1Text.equals(passwd2Text))

{

}

回答3:

In onclik function replace first line with this line u will definitely get right result.

if (passw1.getText().toString().equalsIgnoreCase("1234") && passw2.getText().toString().equalsIgnoreCase("1234")){

回答4:

You can compare the values using equals() of Java :

public void onClick(View v) {

// TODO Auto-generated method stub

s1=text1.getText().toString();

s2=text2.getText().toString();

if(s1.equals(s2))

Show.setText("Are Equal");

else

Show.setText("Not Equal");

}

回答5:

You need both getText() - which returns an Editable and toString() - to convert that to a String for matching.

So instead of: passw1.toString().equalsIgnoreCase("1234")

you need passw1.getText().toString().equalsIgnoreCase("1234").

回答6:

ou can use String.compareTo(String) that returns an integer that's negative ().

Use it so:

You can use String.compareTo(String) that returns an integer that's negative ().

Use it so:

String a="myWord";

if(a.compareTo(another_string) <0){

//a is strictly < to another_string

}

else if (a.compareTo(another_string) == 0){

//a equals to another_string

}

else{

// a is strictly > than another_string

}

回答7:

Try to use .trim() first, before .equals(), or create a new String var that has these things.

回答8:

did the same here needed to show "success" twice

response is data from PHP

String res=response.toString().trim;

Toast.makeText(sign_in.this,res,Toast.LENGTH_SHORT).show();

if ( res.compareTo("success")==0){

Toast.makeText(this,res,Toast.LENGTH_SHORT).show();

}

来源:https://stackoverflow.com/questions/4700357/comparing-strings-in-java

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值