How can I determine the type of an input
What I mean is this :
I have to write a program that gets an integer and prints it on the screen. Otherwise, an error message should be printed.
10x in advance. :)
EDIT :
So far what I have done is this :
Code:
import java.util.Scanner;
public class f{
public static boolean isItInt(String inp)
{
char dgts[]={'0','1','2','3','4','5','6','7','8','9'};
int i=0,j=0,count=0;
for (i=0; i<inp.length(); i++)
{
for(j=0;j<dgts.length;j++)
if (inp.charAt(i)==dgts[j])
count++;
}
if (count==inp.length())
return true;
else
return false;
}
public static void main(String argv[]){
Scanner s = new Scanner(System.in);
String p=s.next();
System.out.println(p);
boolean x=isItInt(p);
System.out.println(x);
}
}
But I am sure there is something easier than this, so let me know.