isInt Boolean - what have I done wrong..

Hi there, this seems to be always returning true:

publicstaticboolean isInt(String readUsersInput){

returntrue;

};

if(isInt(stringName) ==true){

System.out.println(stringName+"is an int!");

};

[607 byte] By [NewbForJavaa] at [2007-11-27 4:37:17]
# 1

> Hi there, this seems to be always returning true:

>

> [code]

> public static boolean isInt(String readUsersInput){

> return true;

> };

>

> if(isInt(stringName) == true){

> System.out.println(stringName+"is an int!");

> };

>

Your program is doing exactly as you have told it to do. the method isInt always returns true. There is no test inside of it to check for int, just a single return true statement.

petes1234a at 2007-7-12 9:47:30 > top of Java-index,Java Essentials,New To Java...
# 2

> Hi there, this seems to be always returning true:

From the looks of it, it will. It says "return true;", and that's what it will do.

You need to write some code that actually does something with the String readUsersInt and determines whether it is a valid integer numeral.

Also it would be a good idea to drop the semicolons after the closing brackets. So isInt would be declared and defined as:public static boolean isInt(String readUsersInput) {

//return true;

// do something with readUsersInput to determine

// whether or not it is a number

} // no ;

pbrockway2a at 2007-7-12 9:47:30 > top of Java-index,Java Essentials,New To Java...
# 3
Also, no need to compare to a boolean:if(isInt(stringName)){
floundera at 2007-7-12 9:47:30 > top of Java-index,Java Essentials,New To Java...
# 4
oh I though that "isint" was a built-in method that would find out automatically, I got it off the API..Could someone show me how to find out if it is an integer then..
NewbForJavaa at 2007-7-12 9:47:30 > top of Java-index,Java Essentials,New To Java...
# 5

If isInt is a method in the API then you would just call it in your code. By writing your own isInt method, this will override the existing isInt method. So when you call isInt, your method will be executed. Therefore if you want to use the existing isInt method, delete yours.

Clear as mud?

floundera at 2007-7-12 9:47:30 > top of Java-index,Java Essentials,New To Java...
# 6

> Could someone show me how to find out if it is an integer then..

You can use a regular expression, or use the Integer method parseInt() and catch the exception.

I realise this answer isn't very useful, and I'm sorry for that. The problem is that even if there were a "built in" isInt() method, the code you posted shows that you might have misconceptions about how to use it. Perhaps the best place to start would be a Java tutorial (Sun's is here: http://java.sun.com/docs/books/tutorial/)

pbrockway2a at 2007-7-12 9:47:30 > top of Java-index,Java Essentials,New To Java...
# 7
Did your teacher supply you with a special library with an "isInt" method in it?
paulcwa at 2007-7-12 9:47:30 > top of Java-index,Java Essentials,New To Java...
# 8
Did anyone else laugh uncontrollably when they read the original post?
CaptainMorgan08a at 2007-7-12 9:47:30 > top of Java-index,Java Essentials,New To Java...
# 9
I had to refrain from making a very sarcastic reply.
floundera at 2007-7-12 9:47:30 > top of Java-index,Java Essentials,New To Java...
# 10
> Did anyone else laugh uncontrollably when they read> the original post?We all have to start from somewhere. Some of us are more uncomfortably close to this starting point than they'd (I'd) care to admit!
petes1234a at 2007-7-12 9:47:30 > top of Java-index,Java Essentials,New To Java...