Hi everyone,

I am new in Java programming. I want to know that how to check integer variable whether it is null or not. I have a situation I write a program that asks that how old are you. If a person accidentally press enter and does not put a value, I give a message that age can't be null.

I hope someone will help ...
Regards

There are ints and there are Integers (there is no Java data type called integer). One is a primitive value, the other is an Object. int variables cannot be null, but Integer variables can be.
What you get when the user presses Enter depends on what method you are using. Please post the relevant part of your code.

Integers cannot be null, so I assume you mean you get null from your input method.

Post your code and perhaps someone can advise you on how to deal with the issue.

Edit: Sorry, post collision with James.

There are ints and there are Integers (there is no Java data type called integer). One is a primitive value, the other is an Object. int variables cannot be null, but Integer variables can be.
What you get when the user presses Enter depends on what method you are using. Please post the relevant part of your code.

Thanks for reply! I learn from you that int and Integers are different things. My program is here, please check it and reply.

int age;
		
		Scanner a = new Scanner(System.in);
		
                //I want to check that if someone did not enter value do while loop again asks same question.
 
		do{	
		System.out.println("What is your age ?");
		age = a.nextInt();
		
		if(age == null)
			System.out.println("Age cannot be null");
		
		}while(age == null);

another approach would be:

1. set int age to -1;
2. input age;
3. if age < 0
no age or invalid age given by user
else
run application

Have you tried to compile your code? Does it give compiler errors?
You'll save some time if you compile it first before posting it with errors you don't know about.

Have you tried to compile your code? Does it give compiler errors?
You'll save some time if you compile it first before posting it with errors you don't know about.

Hi, I have write code just to understand problem. I know it is giving errors, I just want to know alternate solution from you guys.

Thanks

When you get errors, please copy and paste them here.

My program is here, please check it

This implies to me that you had already compiled it with no problems and now wanted to know if the code will do what you want it to do.

commented: You are just showing me that you are a big programmer .... -1

Read the doc on nextInt(). You need to catch the InputMismatchException that will be thrown if they don't enter an int.

In integer datatype you have to enter integer value in that.For its solution you have to make condition like if age is greater than 0 than it will be accepted otherwise ask another value to user.

not really, this would suggest 0 is an invalid input for age, which it is not.
0 years = baby.
everything smaller than 0 is invalid, unless you want to include unborn children :P

Yes, you can use a variable initialised to an invalid value, just like you would in C or BASIC.
But since the writers of the Scanner class have given you proper Java Exceptions you would be well advised to use them. In particular, there are three distinct exceptions thrown, depending on what the error was, but only two of these can be followed by re-prompting the user.
(As usual) Ezzaral's post contains the best advice.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.