Solved

check for negative integer

Posted on 2007-03-23
4
5,501 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

820 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