0

I am trying to allow user to key in two inputs and check user have key in both value or not but it come out error. here is example code

public static void add train{
int a,b;

System.out.println("Enter train ID");
  System.out.println("(**Example '1001'**)");
  b=sc.nextInt();
  System.out.println("1. to Night Safari");
  System.out.println("2. to River Safari");
  System.out.println("3. to Zoo Main Entrance ");
  System.out.println("Enter a destination for the train");
  a= sc.nextInt();
  }

this is my previous code for getting input from user, and now I need to book train ticket

public static void book(){
System.out.println("Where do you want to go?");
  System.out.println("1. to Night Safari");
  System.out.println("2. to River Safari");
  System.out.println("3. to Zoo Main Entrance ");
  a=sc.nextInt();
if (a==1 && b !=null){//incomparable types: int and <nulltype>
    System.out.println("Valid");
}
else if (a==2 && b !=null){
    System.out.println("Valid");
}
//and more
else{
    System.out.println("Invalid");
}
}

because user can key in optional so I need to check the train is exist in order to process booking Is something wrong with my code or just this is impossible to be done in Java?

MorganSIm
  • 27
  • 1
  • 8
  • 5
    What is `b`? It seems to be an `int`. Can primitives be `null`? – Sotirios Delimanolis Apr 06 '14 at 16:21
  • 2
    If `b` is an `int`, it can never be `null`... Show more code – fge Apr 06 '14 at 16:23
  • I'm not even sure what you're trying to accomplish with the comparison. Seeing `b` defined would be nice, although it's been well established here that primitive types cannot be `null`. – Makoto Apr 06 '14 at 16:27
  • @Sotirios Delimanilos: in my concept b is from previous method, is int too but is key in previously so need to check for make sure the value is exist – MorganSIm Apr 06 '14 at 16:28
  • @fge: I declared b is int too but is in previous method and key in by user and back to my main in order to process i need to check weather both value are exist or not – MorganSIm Apr 06 '14 at 16:32
  • what do you mean by "make sure the value is exist" ? – Naili Apr 06 '14 at 16:34
  • ensure that user previously have key in a value – MorganSIm Apr 06 '14 at 16:36

7 Answers7

2

An int can never be null as it is a primitive data type, hence b !=null is uncomputable.

If you are using the Scanner class's nextInt() command, this will always return an integer value (or cause a runtime exception if the user does not enter an int)

numX
  • 830
  • 7
  • 24
1

The type of b is int, and int variables cannot be null by definition.

If you need to be able to store null in b, one possibility would be to change its type to Integer.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

I think you're misunderstanding what null means. null doesn't refer to a value, but to a reference (Read this answer for more info about it). The bottom line is that the only things that can reference null are complex types, like Strings, Integers, classes you define, etc. Primitive types (ints, booleans, etc.) are not references, so can never reference null

Community
  • 1
  • 1
Jason Baker
  • 2,471
  • 3
  • 23
  • 28
  • so what if I need to find out is the int value exist or not, if cannot use null what should i use for replace it? – MorganSIm Apr 06 '14 at 16:34
  • You're giving `a` and `b` values from user input, right? Why not do a check then to make sure that they've entered two values? I'm not sure how you're getting your user input, but I would suggest reading about the [`Scanner`](http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html) class and [Anupam's answer](http://stackoverflow.com/a/22896797/3457753) to give you some ideas – Jason Baker Apr 06 '14 at 16:39
  • yes, I used 'Scanner' for getting input, but 'b' is get from previous method and is optional so now in new method I need both 'a' and 'b' exist so I try to find out is user insert any value in 'b' – MorganSIm Apr 06 '14 at 16:44
  • There's no way to do that with primitives in Java. one thing you could do is make `b` an `Integer` rather than an `int`; `b` would then be a non-primitive types, which could reference `null`. Another option would be some additional meta-data (a `boolean`, for examples), that tells you if this optional parameter has been set – Jason Baker Apr 06 '14 at 16:49
  • I have change b to Integer and now my error is gone =D Thanks for help me out – MorganSIm Apr 06 '14 at 17:04
1

It would be better to do a try catch, instead of this. Doing a try-catch will save you if user enters a string or null.

Scanner sc = new Scanner(System.in);
try
{
    int a = sc.nextInt();
    int b = sc.nextInt();
    System.out.println("valid numbers");
}
catch(InputMismatchException ex)
{
    System.out.println("Invalid numbers");
}
anu
  • 1,017
  • 1
  • 19
  • 36
0

You can change int to object type Integer. Then you can compare it to null.

zrac
  • 153
  • 7
0

An int connot be null because it's a primitive type. Therefore checking b for null is totally wrong, if we correct your code, it will be like this :

if (a==1 || a==2){
    System.out.println("Valid");
}
else{
    System.out.println("Invalid");
}
Naili
  • 1,564
  • 3
  • 20
  • 27
-3

if b is supposed to be an int you could try

if (a==1 && b instanceof int)
lucas
  • 1,485
  • 13
  • 22