473,385 Members | 1,331 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

positive/negative integer/float

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
1 2943
Ishan Shah
47 32bit
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

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

Similar topics

19
by: Imbaud Pierre | last post by:
integer division and modulo gives different results in c and python, when negative numbers are involved. take gdb as a widely available c interpreter print -2 /3 0 for c, -1 for python. more...
15
by: jaks.maths | last post by:
How to convert negative integer to hexadecimal or octal number? Ex: -568 What is the equivalent hexadecimal and octal number??
15
by: Ivan Novick | last post by:
Hi, Is it possible to have negative integer literal or only positive? As far as I understand, the code below would be a positive integer literal and the unary negative operator. x = -3.2; ...
5
by: vbgunz | last post by:
/* * BEGIN EXAMPLES */ var text = 'A Cats Catalog of Cat Catastrophes and Calamities'; /*** * EXAMPLE 1: negative lookahead assertion logic ***/
4
by: wangers16 | last post by:
Is it possible to get JavaScript to check the end of the a integer/float variable for a certain number e.g. 5, if so how do you do it? Thanks in advance
3
by: Daniel DePasqua | last post by:
I do not understand the concept of inputing a number and having the program convert it to an integer, float and string. How would you write a program that does that
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.