skip to Main Content
String s1 = sh.getString("name", "");
int a = sh.getInt("age", 0);

For this code I would like to show empty spaces for s1 and a if there is no input entered. When I run the application String s1 it shows empty space but int a showd 0 since the default value is 0.

Is there any way to show empty space instead of 0?

I have tried puting

Integer.parseInt("") 

But it was not working.

Is there any solution for this?

Thank you.

2

Answers


  1. You can add a condition to check if it is 0 or not. Using null as suggested by answer by Frank can be used but it is not preferred. Instead you can try this:

    String text = a == 0 ? "" : String.valueOf(a);
    textView.setText(text);
    button.setText(text);
    
    Login or Signup to reply.
  2. You just n need to parse it as string you can’t check empty if you are using int variable So just get your age as String and also send it with string via intent you can also parse it as int after getting it from intent

    Like option 1

    Intent intent =new Intent(this,B.class);
    intent.putExtra("name", name);
    intent.putExtra("age", age.toString);
    

    Here you can get Like

    String s1 = sh.getString("name", "");
    String age= sh.getString("age", "");
    

    Here you can check age is empty then if you want this value as int you can Parse it as int a=Integer.parseInt(age);

    Hope this will help you

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search