0

How can i set null to integer variable initially instead of "0". I know already by default all int value is "0". I used String.valueOf() method etc. I dont want to print as "null". I want to print blank like " ". I get number format exception. Please give one solution for that.

Thanks in advance

Here is the code

public class IntegerDemo {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String id="";


        System.out.println(""+Integer.parseInt(id));

    }
}
1
  • if(Integer.parseInt(id)==0){ System.out.println(" ");} Commented Aug 31, 2013 at 5:24

8 Answers 8

4

I am sure you are over-complicating the problem, it is a real simple thing to do. Check the code below:

Integer i = null;
System.out.println(i == null ?"":i);
Sign up to request clarification or add additional context in comments.

Comments

4

Use Integer wrapper class instead of primitive

Integer myInt= null;

For object references you can set null.But you cannot to primitives.

Comments

0

Creating an instance of Integer with its primary value as null is meaningless, but you can do this on declaration:

Integer id = null;

The reason you're getting NumberFormatException is laid out in the documentation:

An exception of type NumberFormatException is thrown if any of the following situations occurs:

  • The first argument is null or is a string of length zero.

  • The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX.

  • Any character of the string is not a digit of the specified radix, except that the first > character may be a minus sign '-' ('\u002D') or plus sign '+' ('\u002B') provided that the string is longer than length 1.

  • The value represented by the string is not a value of type int.

You can't call Integer.parseInt("") or Integer.parseInt(null), with that limitation.

Comments

0

If you parse null or blank as int it will throw numberFormatException. Check for that before parsing:

try{
   System.out.println(StringUtils.isNotBlank(id)?Integer.parseInt(id):"");
}
catch(NumberFormatException e){
   System.out.println("Not A Number");
}

Also be sure if string has not number by catching exception. StringUtils.isNotBlank() will check for both null as well as blank.

Comments

0

You would have to stick with the java.lang.Integer class.

String id = ""
Integer test = null;

if (!"".equals(id))
{
   test = Integer.parseInt(id);
}

1 Comment

parseInt is the method.
0

Instead of parse, we can use Integer.valueOf(null);

Comments

-1
Integer int = null;

ints are primitives, they have default values different from null.

2 Comments

int is a reserved keyword; this isn't valid Java syntax. Besides, the OP isn't referring to primitives.
Yes, I'm wrong with variable name, but obviously I was not referring to primitives as well.
-1

The simple boxed Integer Integer i = null; has already been supplied. Here is an alternative:

class NullableInt {
    private int value;
    private boolean isNull;
    public NullableInt() { isNull = true; }
    public NullableInt(int value) { 
      this.value = value; 
      isNull = false;
    }
    public NullableInt(String intRep) {
      try { value = Integer.parseInt(intRep);
      } catch (NumberFormatException) {
         isNull = true;
      }
    }
    boolean isNull() { return isNull; }
    void setNull(boolean toNull) { isNull = toNull; }
    void setValue(int value) { this.value = value; }
    int getValue() { return value; }
    @Override 
    public String toString() { return isNull ? "" : value; }
}

NullableInt integer1 = new NullableInt(56);
NullableInt integer2 = new NullableInt();

System.out.pritnln(integer1); // >> "56"
System.out.println(integer2); // >> ""

// and finally, to address the actual question:
String id = "";
System.out.println(new NullableInt(id)); // >> ""

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.