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!");
};
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!");
};
> 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.
> 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 ;
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?
> 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/)