File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Validate Integer Length Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Validate Integer Length" Watch "Validate Integer Length" New topic

Validate Integer Length

Gina Ruffolo-Daniel

Joined: Oct 01, 2005
Posts: 19
I want to verify that the user entered a string value of length of 3 for delivery number and string value of 2 for month. I have tried delvryNumString.length() == 3 but it's not working.

[CODE] import javax.swing.*;
public class CreateDelivery

public static void main(String[] args)
// Declare variables
int yearEntry;
int mthEntry;
int delvryNum;
double packageWt;
int distanceCd;
String yearString;
String mthString;
String delvryNumString;
String packageWtString;
String distanceCdString;

// Input screens for delivery year, delivery month, delivery number,
// package weight, and delivery distance
yearString = JOptionPane.showInputDialog(null, "Please enter a year?" +
"\nThe year must be between 2001 and 2009");
yearEntry = Integer.parseInt(yearString);
// Validation for year
while(yearEntry < 2001 || yearEntry > 2009)
yearString = JOptionPane.showInputDialog(null, "Please re-enter a valid year: " +
"\nValid year is between 2001 and 2009 " + "\nYou entered " + yearString);
// yearEntry = Integer.parseInt(yearString);
mthString = JOptionPane.showInputDialog(null, "Please enter the month?" +
"\nValid months are 01 through 12 only");
mthEntry = Integer.parseInt(mthString);
// Validation for month
while(mthEntry < 1 || mthEntry > 12)
mthString = JOptionPane.showInputDialog(null, "Please re-enter the month: " +
"\nValid months are 01 through 12" + "\nYou entered " + mthString);
mthEntry = Integer.parseInt(mthString);
delvryNumString = JOptionPane.showInputDialog(null, "Please enter the delivery number?" +
"\nValid delivery numbers are 1 through 999 only");

delvryNum = Integer.parseInt(delvryNumString);
// Validation for delivery number
while(delvryNum < 1 || delvryNum > 999)
delvryNumString = JOptionPane.showInputDialog(null, "Please re -enter the delivery number?" +
"\nValid delivery numbers are 001 through 999" + "\nYou entered " + delvryNumString);
delvryNum = Integer.parseInt(delvryNumString);
packageWtString = JOptionPane.showInputDialog(null, "Please enter the package weight?" +
"\nValid package weights are .10 through 100 only");
packageWt = Integer.parseInt(packageWtString);
// Validation for package weight
while(packageWt < .10 || packageWt > 100 )
packageWtString = JOptionPane.showInputDialog(null, "Please re-enter the package weight?" +
"\nValid package weigths are .10 through 100" + "\nYou entered " + packageWtString);
packageWt = Double.parseDouble(packageWtString);
distanceCdString = JOptionPane.showInputDialog(null, "Please enter the distance code?" +
"\nValid distance codes are 1 or 2");
distanceCd = Integer.parseInt(distanceCdString);
//Validation for distance Code
while(distanceCd < 1 || distanceCd > 2 )
distanceCdString = JOptionPane.showInputDialog(null, "Please re-enter the distance code?" +
"\nValid distance codes are 1 or 2" + "\nYou entered " + distanceCdString);
distanceCd = Integer.parseInt(distanceCdString);


Delivery myDelivery = new Delivery(yearString,mthString,delvryNumString,packageWtString,distanceCdString);
JOptionPane.showMessageDialog(null, "You entered " + myDelivery.getDelivery());

Thank you in advance.

Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
Hello Gina,

Please could you tell us what error you are getting with,

It seems to look good, I tried a small program and it worked just fine,

May be some other syntax/logical error is causing a problem in your code. The rest of the code also looks good. Could you please post the error you are getting along with the code that uses,

Good luck !
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
What do you mean "it's not working"? Does this cause a compiler error? If so, what is the error and which line of code causes it? If not, does it run? What happens when you run it? How does the result differ from what you expect? We need some more information to be able to help you. Please answer my questions above and I will be glad to look into it further.


Java API Documentation
The Java Tutorial
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
>> delvryNumString;

So, what's so offensive about those two vowels that you had to drop them? Why are you putting type information in your variable names? Not even the folk at Microsoft recommend that convention anymore, finally realizing that it has no upside and plenty of negatives.

As far as the question, you should post precisely what the error is.
Gina Ruffolo-Daniel

Joined: Oct 01, 2005
Posts: 19
I wasn't receiving a error message per se. It was one of the non-descript red underline the syntax. I have been able to get the stringname.length() == 3 to work. BTW: I have no ill-will against vowels.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
If you are using NetBeans or a similar IDE, the red underline means there is a syntax error. You can see the error message by moving your mouse over the line and leaving it there for a few seconds. You may need to move the mouse to the "gutter" on the left where the line numbers appear (at least in NetBeans). You can also see the error message by compiling. This later method is desirable if you need to copy and paste the message here for our help.

madhup narain
Ranch Hand

Joined: Dec 14, 2004
Posts: 148

havent gone through the code that you wrote.. but the redline indicates syntax error, and more probably because the variable is not declared.


Money for nothing and Java for Free
subject: Validate Integer Length
jQuery in Action, 3rd edition