By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,400 Members | 1,219 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,400 IT Pros & Developers. It's quick & easy.

positive/negative integer/float

P: 1
want to check whether user enters the number is positive integer or negative integer or positive float or negative float there is no error in the code but after entering the value it is not giving me any output as it stuck on the running state


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Please input a number:");
double number = input.nextDouble();
if (number >0 && input.hasNextInt()){
System.out.println(number +" is a positive, integer number.");
}

else if (number< 0 && input.hasNextInt()){
System.out.println(number +" is a negative, integer number.");
}

else if (number > 0 && input.hasNextFloat()){
System.out.println(number +" is a positive, floating number.");
}

else if (number< 0 && input.hasNextFloat()){
System.out.println(number +" is a negative, floating number.");
}



}
}
May 7 '18 #1
Share this Question
Share on Google+
1 Reply

P: 37
It giving the output after entering any number second time because scanner class methods hasNextInt and hasNextFloat method are check if the next token in this scanner's input can be interpreted as an int value using the nextInt() method or as a float value using the nextFloat() method.

C:\Users\LENOVO\JavaInterviewPrograms>java Demo
Please input a number : 2
1
2 is a positive, integer number.
Oct 20 '20 #2

Post your reply

Sign in to post your reply or Sign up for a free account.