Click here to Skip to main content
15,861,168 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how do i fix this problem guys give some solutions.

What I have tried:

package com.imatrix;
import java.io.*;

public class Test {

public static void main(String [] args){

// The name of the file to open.
String fileName = "/home/imtadmin/Desktop/task";

// This will reference one line at a time
String line = null;

try {
// FileReader reads text files in the default encoding.
FileReader fileReader =
new FileReader(fileName);

// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader =
new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
String[] splitted = line.split("~");

String fisrt=splitted[0];
String secod=splitted[1];
String third=splitted[2];

System.out.println(fisrt+"="+secod+"="+third);
}
// Always close files.
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
}

output:-
7892425242=Ramadass12344=Rama
9789949018=Test1=Test2
ABC Company Ltd=Test1=Test2
ABC India Pvt. Ltd.=Test1=Test2
Abhilash Loan A/c=Test1=Test2
Abxd India Pvt Ltd=Test1=Test2
Accum. Dep. on Airconditioner=Test1=Test2
Accum. Dep. on Building=Test1=Test2
Accum. Dep. on Computer & Per.=Test1=Test2
Accum. Dep. on Furn. & Fixt.=Test1=Test2
Accum. Dep. on Motor Car==Test2
Accum. Dep. on P & M - I=Test1=Test2
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at com.imatrix.Test.main(Test.java:29
Posted
Updated 14-Jun-19 23:08pm

The error message tells you that index value 2 is not valid, and the offending code is:

Java
String[] splitted = line.split("~");

String fisrt=splitted[0];
String secod=splitted[1];
String third=splitted[2]; 

You are making one of the most common mistakes we see here. How do you know that there are three parts to this message? What if there are no ~ characters at all in the input data? You must check the number of parts that are returned before you try to access them.
Java
if (splitted.length < 3)
{
    // less than three parts in the data
}


And see, here is the offending line:
Accum. Dep. on P & M - II~Test1~
 
Share this answer
 
Comments
[no name] 15-Jun-19 5:14am    
can you send the coding to solve the error
Richard MacCutchan 15-Jun-19 5:22am    
I have shown you the coding above. Check the length of the array and do whatever is necessary with the data. I have no idea what you are trying to do with the results.
[no name] 15-Jun-19 5:36am    
IVE TRYING TO CLEAR THE RUN TIME ERROR
AND I NEED TO STORE THE VALUES IN HASHMAP AND FROM THAT I NEED TO PRINT THE VALUES AND SHOW AS OUTPUT
Richard MacCutchan 15-Jun-19 6:31am    
I have told you what you need to do. Check the length of the array and do not try and access elements that do not exist.

And, please, stop shouting. Typing in all capitals is considered rude.
[no name] 15-Jun-19 6:44am    
apologize for that...
We can't tell - because we have no access to your datafile, and that will be critical to finding out why it's a problem. But it's pretty simple in essence: at least one of the lines in your file does not contain at least two '~' characters, so the array returned by the split function does not contain three elements.

The problem is that we can't look at teh file, and we can't tell which line(s) are giving the problem.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Quote:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at com.imatrix.Test.main(Test.java:29

This means that your code try to access a place that does not exist in an array.
Since line 29 do not access an array in your code, we can only guess that something went wrong when you copied it, only debugger can tell you what is going on.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.

[Update]
By the way
Java
while((line = bufferedReader.readLine()) != null) {
    String[] splitted = line.split("~");

     String fisrt=splitted[0];
     String secod=splitted[1];
     String third=splitted[2];

    System.out.println(fisrt+"="+secod+"="+third);
}

You should look at the replace function of RegEx, it look more appropriate than split.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900