aspose file tools*
The moose likes Beginning Java and the fly likes Try and catch Help! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Try and catch Help!" Watch "Try and catch Help!" New topic
Author

Try and catch Help!

Beth Laguardia
Ranch Hand

Joined: Jun 01, 2008
Posts: 34
Hello guys I am trying to play with try and catch but i cannot get the results that I want. Hope you will help me out.

Here's the program:



What I want to do is catch and print error if the user input an integer. But in the progam even thou i input an integer it will still pint it. Can you please help me? I have not taken his topic before because i am a very much newbie with java.

Thanks
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Beth,

Nothing in your program will actually throw a NumberFormatException, so you won't ever catch one. NumberFormatException is thrown by methods that want a string to contain an integer, if it turns out that the string doesn't. For example, the "Integer.valueOf()" will throw an exception here, and the catch block will execute:



[Jess in Action][AskingGoodQuestions]
Matt Swaggi
Ranch Hand

Joined: May 29, 2008
Posts: 61
hai friend ! i hope this coding works ! it keeps on asking the user to give name as long as he types the character ! coding is asfollows

***************************************************************************


[edit]Add code tags. CR[/edit]
[ June 15, 2008: Message edited by: Campbell Ritchie ]
Beth Laguardia
Ranch Hand

Joined: Jun 01, 2008
Posts: 34
Originally posted by Ernest Friedman-Hill:
Hi Beth,

Nothing in your program will actually throw a NumberFormatException, so you won't ever catch one. NumberFormatException is thrown by methods that want a string to contain an integer, if it turns out that the string doesn't. For example, the "Integer.valueOf()" will throw an exception here, and the catch block will execute:




Hello ma'am,

May I know what should i do so my program can catch integer entered? What i want my program to do is accept string not integer but it still accept integers. Is there a way? Thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Originally posted by Beth Laguardia:
Hello ma'am,

May I know what should i do so my program can catch integer entered? What i want my program to do is accept string not integer but it still accept integers. Is there a way? Thanks

Surely you're not calling Ernest ma'am?? Dr. Friedman-Hill is quite male indeed

If you want to disallow numbers then you should do it the other way around: try parsing the name into an integer, and if that fails then it's a GOOD thing:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Matt Swaggi
Ranch Hand

Joined: May 29, 2008
Posts: 61
Hey
Beth Laguardia wont my coding is not working for You ! i hope you may not run the coding ! well anyway i said my suggestion its up to You !
bye !!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42263
    
  64
If you want to make sure it's not an integer you would quit if you were successful parsing the string into an integer. Something like this:

There are more graceful ways of writing this code (using string operations or regular expressions), but for starters this may suffice.


Ping & DNS - my free Android networking tools app
Beth Laguardia
Ranch Hand

Joined: Jun 01, 2008
Posts: 34
Originally posted by GUNA RANJAN:
Hey
Beth Laguardia wont my coding is not working for You ! i hope you may not run the coding ! well anyway i said my suggestion its up to You !
bye !!



Hey Gina I am sorry. I thought the code you posted is yours and you are having a hard time also. Will try all the sugestion here. Its the only way I can learn..

Thanks a lot
Beth Laguardia
Ranch Hand

Joined: Jun 01, 2008
Posts: 34
Thank you all sir!

Will try all the codes and study it. Really thanks a bunch!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Try and catch Help!