• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Try and catch Help!  RSS feed

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24203
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Sheriff
Posts: 21465
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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:
 
Matt Swaggi
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!
 
Rancher
Posts: 42974
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Beth Laguardia
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all sir!

Will try all the codes and study it. Really thanks a bunch!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!