473,320 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

java.lang.ArrayIndexOutOfBoundsException:

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.

10 4066
Rabbit
12,516 Expert Mod 8TB
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
please give me some guideline, I could not fix it.
Oct 14 '12 #3
Rabbit
12,516 Expert Mod 8TB
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
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
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
12,516 Expert Mod 8TB
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
now what I have to do for writing all concated texts?
Oct 15 '12 #8
Rabbit
12,516 Expert Mod 8TB
I have no idea what you mean. That is a very vague description of what you want.
Oct 15 '12 #9
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
12,516 Expert Mod 8TB
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

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

Similar topics

1
by: CM | last post by:
Hi, when i want connect me in my BD with a JSP (with this simple code), this exception is throw. Thank's for ur help Mathieu CODE of my JSP ---------------------
6
by: ganesh.m | last post by:
Hi, I am new to DB2. I am getting this error while loading the DB2Driver. I don't have any idea about where i might have gone wrong. please help me. Below is the stack trace. Stack Trace:...
0
by: snkssa | last post by:
Hi, I got the following error while building an application with ant. please can you give the solution or reason why it is failing? Thank you create_zip_linux: ...
22
oll3i
by: oll3i | last post by:
i get Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at Producent.main(producent.java:605) when i run it from bat @start "Supply Chain Management-Producer to...
3
by: jayattarde | last post by:
Hi, While running .jar file I get the following error. Please helpme to sort it out. ption in thread "main" java.lang.NoClassDefFoundError: while resolving class: Cutter at...
6
by: Light | last post by:
Hi I m getting this error while i m trying to run my program and i don't understand why that happens. import java.util.*; public class Exercise6 {
1
by: shaikhussain | last post by:
public class EmployeeServicesTestCase { /** * @param args */ public static void main(String s)throws Exception { // TODO Auto-generated method stub BeanFactory beans=new...
2
by: David Rothabuer | last post by:
I work with an integration engine that is java based. All my scripting is javascripting and I've run into a problem that I can't find a solution to. I am getting this error when I attempt to read...
1
by: Buena Velasco | last post by:
I'm having trouble finding the error on my code. I know what "Exception thread ... ArrayIndexOutOfBounds... " means but I couldn't tell which part in the looping it is or in other. import...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.