Out of bounds exception

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • krabina1988
    New Member
    • Oct 2011
    • 9

    #1

    Out of bounds exception

    can anyone here help explain why my code is always getting an outOfBoundsExce ption error??? I want to be the one to do the necessary changes to the code, I just need some explanation. Here is my code.

    Code:
    import java.io.*;
    
    public class Comp
    {
        public static void main(String args[]) throws IOException
        {
            BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
            String input;
    
            System.out.print("Enter Input String: ");
            input = buff.readLine();
    
            char [] in = input.toCharArray();
    
            for(int i=0;i<in.length;i++)
            {
               while(!(in[i]==' '))
               {
                   System.out.print(in[i]);
                   i++;
               }
               System.out.print("\n");           
            }
        }
    }
    Last edited by Meetee; Nov 24 '11, 09:17 AM. Reason: code tags added
  • krabina1988
    New Member
    • Oct 2011
    • 9

    #2
    its supposed to work like string tokenizer without using the actual library. the delimiter I am using is white space/" ".

    Comment

    • xunter
      New Member
      • Nov 2011
      • 7

      #3
      Originally posted by krabina1988
      can anyone here help explain why my code is always getting an outOfBoundsExce ption error??? I want to be the one to do the necessary changes to the code, I just need some explanation. Here is my code.

      Code:
      import java.io.*;
      
      public class Comp
      {
          public static void main(String args[]) throws IOException
          {
              BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
              String input;
      
              System.out.print("Enter Input String: ");
              input = buff.readLine();
      
              char [] in = input.toCharArray();
      
              for(int i=0;i<in.length;i++)
              {
                 while(!(in[i]==' '))
                 {
                     System.out.print(in[i]);
                     i++;
                 }
                 System.out.print("\n");           
              }
          }
      }
      You can't use an increment of the "i" variable. It will increment itself because you are using a "for" loop.
      So a reason is the line "i++;" in the "while" block.

      Comment

      • krabina1988
        New Member
        • Oct 2011
        • 9

        #4
        I tried removing the i++ in the while block...the result is an infinite loop. I added that i++ so that it will be able to print a word one character at a time using the while loop.if the char at i is " " then the next character if it is a letter will be printed on the next line. example: if your input is "xx y" it is supposed to display:

        xx
        y

        Comment

        • xunter
          New Member
          • Nov 2011
          • 7

          #5
          Originally posted by krabina1988
          I tried removing the i++ in the while block...the result is an infinite loop. I added that i++ so that it will be able to print a word one character at a time using the while loop.if the char at i is " " then the next character if it is a letter will be printed on the next line. example: if your input is "xx y" it is supposed to display:

          xx
          y
          Ok. You should use a "split" method like below:

          Code:
          void main(String[] args) {
          String inputString = "some string";
          String[] words = inputString.split(" ");
          for (int i=0; i<words.length; i++) {
          System.out.println(words[i]);
          } 
          }
          
          /*
          Out:
          some
          string
          */

          Comment

          • ddyer
            New Member
            • May 2010
            • 8

            #6
            the "while" loop must also end on i>=in.length

            Comment

            • krabina1988
              New Member
              • Oct 2011
              • 9

              #7
              @xunter that would work..but I'm going to expand the program a bit further, say you have "int x=1;" it should out put

              int
              x
              =
              1
              ;

              so my delimiter would not only be " " but it should also catch say "x=1;" its output should be the same when my input is "x = 1 ;"

              Comment

              • krabina1988
                New Member
                • Oct 2011
                • 9

                #8
                @xunter that would work..but I'm going to expand the program a bit further, say you have "int x=1;" it should out put

                int
                x
                =
                1
                ;

                so my delimiter would not only be " " but it should also catch say "x=1;" its output should be the same when my input is "x = 1 ;"

                Comment

                • krabina1988
                  New Member
                  • Oct 2011
                  • 9

                  #9
                  I tried while(!(in[i]==' ')&&(i<in.lengt h))...its not working.can a while loop have multiple conditions???

                  Comment

                  • krabina1988
                    New Member
                    • Oct 2011
                    • 9

                    #10
                    thank you guys...I got the output I wanted. I just used
                    while((i<in.len gth)&&!(in[i]==' ')) instead of while(!(in[i]==' ')&&(i<in.lengt h))

                    Comment

                    Working...