Solved

check for negative integer

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

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…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now