java.lang.ArrayIndexOutOfBoundsException:

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sushantshrestha
    New Member
    • Oct 2012
    • 6

    java.lang.ArrayIndexOutOfBoundsException:

    Code:
    import java.io.*;
    import javax.swing.JOptionPane;
    public class read {
    public static void main(String args[])throws IOException
      {
      try{
      FileInputStream fstream = new FileInputStream("C:/Users/sushan/Desktop/abb.txt");
     DataInputStream in = new DataInputStream(fstream);
      BufferedReader br = new BufferedReader(new InputStreamReader(in));
      String strLine;
      while ((strLine = br.readLine()) != null)   {
      
    	String text="";  
          text=text+strLine;
          String[] words=text.split(" ");
      for(int i=0;i<words.length;i++)
      {
       String st=words[i]+words[i+1];
        System.out.println (st); 
      }
      in.close();}
        }catch (Exception e){
      JOptionPane.showMessageDialog(null,"Error");
      }
      }
    }
    Last edited by Rabbit; Oct 14 '12, 07:16 AM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Please use code tags when posting code.

    It's hard to say exactly what the problem is without knowing the line number of code causing the error. But that is a very clear error message. Basically, you have an array but you are trying to access an element of the array that is larger than how many you have.

    I'm going to take a shot in the dark and say it's line 18. When you reach the last word, you are also trying to access the word after the last word, that's impossible. You need to loop one less than you're currently looping.

    Comment

    • sushantshrestha
      New Member
      • Oct 2012
      • 6

      #3
      please give me some guideline, I could not fix it.

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        I did give you guidance. You need to show me what you've changed. I can't tell you what you did wrong if I don't know what you did.

        Comment

        • sushantshrestha
          New Member
          • Oct 2012
          • 6

          #5
          Code:
           import java.io.*; 
            import javax.swing.JOptionPane;
           public class read {
           public static void main(String args[])throws IOException
           {
           try{
           FileInputStream fstream = new FileInputStream("C:/Users/sushan/Desktop/abb.txt");
           DataInputStream in = new DataInputStream(fstream);
           BufferedReader br = new BufferedReader(new InputStreamReader(in));
           String strLine;
           while ((strLine = br.readLine()) != null) {
          
           String text=""; 
           text=text+strLine;
           String[] words=text.split(" ");
           for(int i=0;i<words.length-1;i++)
           {
           String st=words[i]+words[i+1];
           System.out.println (st); 
           }
           in.close();}
           }catch (Exception e){
           JOptionPane.showMessageDialog(null,"Error");
           }
           } 
           }

          Comment

          • sushantshrestha
            New Member
            • Oct 2012
            • 6

            #6
            This code is only able to concat texts they are at same line
            but not able to concat text of two lines. Also, it can write only last one.
            So please help me ..

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #7
              Of course it can only concatenate text on the same line. That's what you programmed it to do, it only reads a single line at a time. If you need it to concat the next line, then you need to read the next line.

              I don't know what you mean by it can only write last one as you've given no information about the input/output.
              Last edited by Rabbit; Oct 15 '12, 04:03 PM.

              Comment

              • sushantshrestha
                New Member
                • Oct 2012
                • 6

                #8
                now what I have to do for writing all concated texts?

                Comment

                • Rabbit
                  Recognized Expert MVP
                  • Jan 2007
                  • 12517

                  #9
                  I have no idea what you mean. That is a very vague description of what you want.

                  Comment

                  • sushantshrestha
                    New Member
                    • Oct 2012
                    • 6

                    #10
                    1. I have to concate two words which are in two lines.
                    2. I have to write concated all texts not only last one.

                    Comment

                    • Rabbit
                      Recognized Expert MVP
                      • Jan 2007
                      • 12517

                      #11
                      1) I answered that in post #7. If you need to concat something from the next line, then you need to read the next line.

                      2) I responded to that in post #7 as well. Without the sample input/output, it's hard to know what's going on.

                      Comment

                      Working...