Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check for non-numeric value

Status
Not open for further replies.

blinton25

Programmer
Mar 20, 2004
104
BB
Hi,

I have the following code:


<HTML>
<HEAD>
<TITLE>Project 4-1</TITLE>
</HEAD>
<BODY>
<% String amount = request.getParameter("sales");
if(amount == null) amount = "0";
int sales = Integer.parseInt(amount);

double comis = 0; //commission stored here
double total = 0;

%>
<b>Enter the amount of your sales:</b>
<form name="thisForm" action="Project1.jsp" method=post>
Amount of sales:<input name=sales size=10>
<input type=submit value="Get commissions">
</form>

<% if (sales>=1 && sales<=2000) {
comis = (sales*0.03);
total = (comis+sales);

} else if (sales>=2001 && sales<=5000) {
comis = (sales*0.04);
total = (comis+sales);

} else if (sales>=5001 && sales<=7000) {
comis = (sales*0.07);
total = (comis+sales);
} else if (sales>=7001) {
comis = (sales*0.10);
total = (comis+sales);
}%>

<% out.print("Sales is "+sales+" Commission is "+comis+" Total is " + total);
%>

</BODY>
</HTML>

It works ok but if I don't enter a value or enter a non numeric value in the sales field I receive an error.

How can I check if nothing or a non-numeric value is entered. I would rather not do it as a try-catch though.
 
Hi

Check it with regular expression, using [tt]String.matches()[/tt].
Code:
[gray]// ...[/gray]

String amount = request.getParameter("sales");

[highlight #eee]if (amount.matches("\\D")) System.out.println("Invalid character");[/highlight]

if(amount == null) amount = "0";
int sales = Integer.parseInt(amount);

[gray]// ...[/gray]

Feherke.
 
Integer.parseInt throws an Exception if a non number argument is received: you can catch that exception.

Cheers,
Dian
 
I would rather not do it as a try-catch though. "

Thanks
 
The persons that I am demonstrating this to have not been through this as yet.
 
Ah. You're a trainer, then? Have they 'been through' regular expressions yet? :)

Tim
 
Hi

Christiaan said:
That will loose you some students.
Why ? Just because you, as Windows fan, are not familiar with regular expressions ?

I knew regular expressions couple of years before Java. When I read about them in the API, first I thought it is just cheap replacement. And when I saw that Java can handle almost Perl compatible regular expressions, I was impressed. For me the flexibility of the implemented regular expressions is a good consolation for the rigidity of the language, especially the imposed exception handling.

Feherke.
 
I've been a trainer for a long time, and I'd never start teaching Java with regular expressions.

Exception is a key concept not for Java but for programming so it's clearly something they need to know.

Maybe regular expressions are nice for a Perl programmer, but I think they're a programming language themselves.

Cheers,
Dian
 
Hi

Of course, you do not begin with regular expressions. They are not the first step neither in Perl.

But if you would, I do not believe that you would loose students.

Feherke.
 
Well, I guess it depends on the audience and the objectives of training, but for Java novice, I'd rather tell them to do it with Integer.parseInt or Character.isDigit.

Cheers,
Dian
 
Who says I'm a windows fan. I use linux (kubuntu) and Java at home. I just have to use vb.net at work (well I don't have to but it would take to much time to convert the current projects).

I just think you would loose more students if you have regex before exceptions instead of the other way round. And believe me you already loose a lot of students no matter what.

I'm in my final year (I hope) of an associates degree (in eveningschool) and we started with 60 3 years ago and are now left with 3. All programming is done in Java. So we get a chance to go into one language. And I'm pretty sure that we would be 2 if regex came before exceptions. I think regex is a nice addition to any programming language but not a must (you can probably do the same without them albeit a bit slower most of the time) while excpetions are a must.

Christiaan Baes
Belgium

"My new site" - Me
 
I just don't think this site has the right/big enough userbase for linux/java. And I mostly find my answers after a google or a lot of hard work and swearing.

Christiaan Baes
Belgium

"My new site" - Me
 
chrissie said:
And I mostly find my answers after a google or a lot of hard work and swearing

... ah, it's not just me then? [wink]

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top