By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,653 Members | 1,419 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,653 IT Pros & Developers. It's quick & easy.

java.lang.ArrayIndexOutOfBoundsException:

P: 6
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import javax.swing.JOptionPane;
  3. public class read {
  4. public static void main(String args[])throws IOException
  5.   {
  6.   try{
  7.   FileInputStream fstream = new FileInputStream("C:/Users/sushan/Desktop/abb.txt");
  8.  DataInputStream in = new DataInputStream(fstream);
  9.   BufferedReader br = new BufferedReader(new InputStreamReader(in));
  10.   String strLine;
  11.   while ((strLine = br.readLine()) != null)   {
  12.  
  13.     String text="";  
  14.       text=text+strLine;
  15.       String[] words=text.split(" ");
  16.   for(int i=0;i<words.length;i++)
  17.   {
  18.    String st=words[i]+words[i+1];
  19.     System.out.println (st); 
  20.   }
  21.   in.close();}
  22.     }catch (Exception e){
  23.   JOptionPane.showMessageDialog(null,"Error");
  24.   }
  25.   }
  26. }
Oct 14 '12 #1

✓ answered by Rabbit

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.

Share this Question
Share on Google+
10 Replies


Rabbit
Expert Mod 10K+
P: 12,421
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.
Oct 14 '12 #2

P: 6
please give me some guideline, I could not fix it.
Oct 14 '12 #3

Rabbit
Expert Mod 10K+
P: 12,421
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.
Oct 14 '12 #4

P: 6
Expand|Select|Wrap|Line Numbers
  1.  import java.io.*; 
  2.   import javax.swing.JOptionPane;
  3.  public class read {
  4.  public static void main(String args[])throws IOException
  5.  {
  6.  try{
  7.  FileInputStream fstream = new FileInputStream("C:/Users/sushan/Desktop/abb.txt");
  8.  DataInputStream in = new DataInputStream(fstream);
  9.  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  10.  String strLine;
  11.  while ((strLine = br.readLine()) != null) {
  12.  
  13.  String text=""; 
  14.  text=text+strLine;
  15.  String[] words=text.split(" ");
  16.  for(int i=0;i<words.length-1;i++)
  17.  {
  18.  String st=words[i]+words[i+1];
  19.  System.out.println (st); 
  20.  }
  21.  in.close();}
  22.  }catch (Exception e){
  23.  JOptionPane.showMessageDialog(null,"Error");
  24.  }
  25.  } 
  26.  }
  27.  
Oct 15 '12 #5

P: 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 ..
Oct 15 '12 #6

Rabbit
Expert Mod 10K+
P: 12,421
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.
Oct 15 '12 #7

P: 6
now what I have to do for writing all concated texts?
Oct 15 '12 #8

Rabbit
Expert Mod 10K+
P: 12,421
I have no idea what you mean. That is a very vague description of what you want.
Oct 15 '12 #9

P: 6
1. I have to concate two words which are in two lines.
2. I have to write concated all texts not only last one.
Oct 15 '12 #10

Rabbit
Expert Mod 10K+
P: 12,421
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.
Oct 15 '12 #11

Post your reply

Sign in to post your reply or Sign up for a free account.