• Post Reply Bookmark Topic Watch Topic
  • New Topic

parameter type check  RSS feed

 
Emanuel Mensa
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i want to be able to check the TYPE of the parameter that a user inputs. i tried the following. it's wrong, how do i correct it?



thanks a lot
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the point? The parameter guarantees that the argument can only be an Integer. What else could possible be passed?
 
Emanuel Mensa
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm learning. so i was thinking what if the user types a String as parameter instead of an int?...so i was trying to check this error.
is there a better way to check this?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the method is called with a string, the code will not even compile.

What does this have to do with user input?
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don’t write throws declarations with unchecked Exceptions; it is poor style and unnecessary. The compiler will ignore unchecked Exceptions.
 
Emanuel Mensa
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Don’t write throws declarations with unchecked Exceptions; it is poor style and unnecessary. The compiler will ignore unchecked Exceptions.


how do i correct this please? i'm a complete noob
 
Niraj Jha
Ranch Hand
Posts: 63
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
write this piece of code in try{}catch......and it will be better you parse your input value into number and handle NumberFormatException
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Niraj Jha wrote:write this piece of code in try{}catch......and it will be better you parse your input value into number and handle NumberFormatException
Nonsense. That has nothing to do with the original question.

It would be better to try some validation of the input, but that is a different matter. For inspiration, look here.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emanuel Mensa wrote:how do i correct this please? i'm a complete noob

Remove the throws RuntimeException in your method declaration (line 1).

The if statement in line 2 is unnecessary. The parameter age is always going to be an Integer. You specified that in line 1. It can never be anything else than an Integer.

If you try to call this method by passing it a String instead of an Integer, then your code will not compile.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!