public class Sample { private static Scanner sc = new Scanner(System.in); public static void main(String[] args) { String str; System.out.print("Enter: "); str = sc.nextLine(); int x = 0; if ((str == null) || (str.equals("") || (str.equals(" ")))) { System.out.println("Invalid"); } int sz = str.length(); for (int i = 0; i < sz; i++) { x = i; } if (Character.isDigit(str.charAt(x)) == false) { System.out.println("Its Not A Number."); } else if (Character.isDigit(str.charAt(x)) == true) { System.out.println("Its A Number."); } } }
this program is for checking if the input string or char is a number or not..
what i want is if the enterd value is " "(which is whitespace) and "" (which is empty) the program wont respond a StringOfIndexOutOfBounce... the compiler is pointing to this part
if (Character.isDigit(str.charAt(x)) == false) {
i dont know how to get out of this mess..
please correct my mistakes coz i've only done this program a while ago which was sir Json gave me , and i applied the simple logic ofthis in one of my programs.


LinkBack URL
About LinkBacks
Reply With Quote

The performance difference here is negligible. However, yes. Exceptions are generally bad practice (like goto's, though exceptions are slightly more useful and not quite as bad programming practice).