StringIndexOutOfBounceException
Code :
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
Code :
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.
Re: StringIndexOutOfBounceException
What input string do you use when you get this exception?
// Json
Re: StringIndexOutOfBounceException
... If you want to check for a negative number, just use parseInt(), and catch the exception.
Code :
try
{
if (Integer.parseInt(someStr) < 0)
{
System.out.println("Negative number!");
}
else
{
System.out.println("Positive number!");
}
}
Catch(Exception e)
{
System.out.println("You didn't enter a number :(");
}
Re: StringIndexOutOfBounceException
Thats not good practise though, exceptions are sloooow :(
// Json
Re: StringIndexOutOfBounceException
check your code, the maximum value for charAt() is str.length()-1, your x value is str.length()
Re: StringIndexOutOfBounceException
:P 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).
Re: StringIndexOutOfBounceException
Quote:
Thats not good practise though, exceptions are sloooow
whaa!!! getting confuse here... exceptions are what? bad practice? so my my logic is right? i want to remove the exception by adjusting the logic of that code?
Quote:
What input string do you use when you get this exception?
i just want to input a white space or a blank String
or
i want to remove that exception
and chris.. yes i adjusted the length of the string as you have taught me about String measurement (length()) but nothing happens... i tried to minus 1 the length.. it still outofbounds....
please help...
this is as simple as this...
1.) if the input string is null.. then i will make an output 2.) if the input string is whitespace (" ") even greater than 1 whitespace (" ") then the output must be
3.) last and for most, i want to remove that exception..
something a bit hard is poping in my mind.. do i have to catch that exception?
Re: StringIndexOutOfBounceException
guys i've already solved a frament of my problem.
regarding with whitespaces (blank spaces) ,,
the only thing is the empty string... its not null right? because im declaring double qoute
meaning that theres an empty string.. but i dont know how to get that index(i tried length() - 1)
still out of bounds...
and still the same.. want to remove that exception