johnsnow85 johnsnow85 - 3 months ago 11
Java Question

Check if string is empty didn't work

In my android application i have two

activity
:


  • Player_List

  • Game_Launch



In the first
activity
, I ask the name of the
player
, if all names are empty we play without names, but if they're not empty we play with
players
.

When I add 3 out of 4 players, I have the
activity
with the
player
. But he add the player who is
empty
.

How can I make a
check system
?

Player_List:

Intent i = new Intent(getApplicationContext(),Game_Launch.class);

i.putExtra("player1", player1.getText().toString());
i.putExtra("player2", player2.getText().toString());
i.putExtra("player3", player3.getText().toString());
i.putExtra("player4", player4.getText().toString());
startActivity(i);


Game_Launch :

Intent in = getIntent();
String player1= in.getExtras().getString("player1");
String player2= in.getExtras().getString("player2");
String player3= in.getExtras().getString("player3");
String player4= in.getExtras().getString("player4");
//Log.d();

if (!player1.equals("")) {
playerge= new String[] {"player2", "player3", "player4"};
}
else if (!player2.equals("")){
playerge= new String[] {"player1", "player3", "player4"};
}
else if (!player3.equals("")){
playerge= new String[] {"player1", "player2", "player4"};
}
else if (!player4.equals("")){
playerge= new String[] {"player1", "player2", "player3"};
}
else if (!player1.equals("") && (!player2.equals(""))){
playerge= new String[] {"player3", "player4"};
}
else if (!player3.equals("") && (!player4.equals(""))){
playerge= new String[] {"player1", "player2"};
}


When I use this check it doesn't work. Can you help me to solve my problem? Thank you.

EDIT :

With answerd i have tried :


Intent in = getIntent();
String player1 = in.getStringExtra("player1");
String player2 = in.getStringExtra("player2");
String player3 = in.getStringExtra("player3");
String player4 = in.getStringExtra("player4");

//Log.d();

if (!player1.equals("")) {
playerge= new String[] {"player2", "player3", "player4"};
Toast.makeText(getApplicationContext(), "p1", Toast.LENGTH_SHORT).show();
}
else if (!player2.equals("")) {
playerge= new String[] {"player1", "player3", "player4"};
Toast.makeText(getApplicationContext(), "p2", Toast.LENGTH_SHORT).show();
}
else if (!player3.equals("")) {
playerge= new String[] {"player1", "player2", "player4"};
Toast.makeText(getApplicationContext(), "p3", Toast.LENGTH_SHORT).show();
}
else if (!player4.equals("")) {
playerge= new String[] {"player1", "player3", "player2"};
Toast.makeText(getApplicationContext(), "p4", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), "tout ok", Toast.LENGTH_SHORT).show();
}


My String 1 is completed, 2 not, 3 completed, 4 completed but i have P1 toast.

How String was created :

public void checkuser(){
EditText player1 = (EditText)findViewById(R.id.editText2);
EditText player2 = (EditText)findViewById(R.id.editText);
EditText player3 = (EditText)findViewById(R.id.editText4);
EditText player4 = (EditText)findViewById(R.id.editText3);
String f = player1.getText().toString();
String f1 = player2.getText().toString();
String f2 = player3.getText().toString();
String f3 = player4.getText().toString();

if ((f.trim().equals("")) && (f1.trim().equals("")) && (f2.trim().equals("") && (f3.trim().equals("")))){
Intent i = new Intent(getApplicationContext(),Game_Launch.class);
startActivity(i);
}
else{
Intent in = new Intent(getApplicationContext(),phrasesgenerator_players.class);

in.putExtra("player1", player1.getText().toString());
in.putExtra("player2", player2.getText().toString());
in.putExtra("player3", player3.getText().toString());
in.putExtra("player4", player4.getText().toString());
startActivity(in);
}
}

Answer

You are getting the data incorrectly. When you write data like:

Intent i = new Intent(getApplicationContext(), Game_Launch.class);
i.putExtra("player1", player1.getText().toString());

You get it as:

Intent in = getIntent();
String player1= in.getStringExtra("player1");

Now you can do your comparisons as it is.

Comments