Home > Software design >  Showing empty space for integer
Showing empty space for integer

Time:03-16

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.

CodePudding user response:

You cannot assign an int variable to anything other than an integer value ( "" is a String so won't work). If you need your variable to point to "nothing", consider using the boxed type Integer , which you can initialize to null . Otherwise, don't declare the variable until you're ready to assign it.

CodePudding user response:

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);

CodePudding user response:

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

  • Related