Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: StringIndexOutOfBounceException

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default StringIndexOutOfBounceException

    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.

  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: StringIndexOutOfBounceException

    What input string do you use when you get this exception?

    // Json

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: StringIndexOutOfBounceException

    ... If you want to check for a negative number, just use parseInt(), and catch the exception.

         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 :(");
         }

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: StringIndexOutOfBounceException

    Thats not good practise though, exceptions are sloooow

    // Json

  5. #5
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: StringIndexOutOfBounceException

    check your code, the maximum value for charAt() is str.length()-1, your x value is str.length()
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: StringIndexOutOfBounceException

    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).

  7. #7
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: StringIndexOutOfBounceException

    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?







    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
    ERROR
    2.) if the input string is whitespace (" ") even greater than 1 whitespace (" ") then the output must be
     EMPTY STRING. [ERROR]
    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?

  8. #8
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default 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
    StringIndexOutOfBounds