Solved

check for negative integer

Posted on 2007-03-23
4
5,499 Views
Last Modified: 2008-02-20
hi guys
i think this is easy..

I have a field where users can enter a number, but the number must be a positive integer
like 10, 20 ect . i want to check if the number is negative  
something like

if(the integer is negative or starts with a minus '-' sign)

{
System.out.println("Please enter positive integer only"); //display this in console
}

any ideas how i can do that in java

thanks
J
0
Comment
Question by:jaggernat
4 Comments
 
LVL 20

Accepted Solution

by:
gatorvip earned 125 total points
ID: 18781252
umm..... if (your_variable < 0) .....

where your_variable is a numerical representation of your field
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 125 total points
ID: 18781332
int n = 0;
if ((n = Integer.parseInt(fieldValue)) < 0) {
    // problem
}
else {
    // no problem
}
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
topping3 challenge 14 80
firstswap challenge 20 77
How to convert from xls to xlsx using java 7 39
Is Applet the way to go for my drag and drop system? 8 14
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question