about i/o stream

Hi all,

I am getting an an run time error like

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

at InputReader.main(InputReader.java:25)

.

I dont know what is wrong with that. can anybody figure out the error?import java.io.BufferedReader;

import java.io.File;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.Arrays;

publicclass InputReader{

publicstaticvoid main(String arg[])throws Exception{

BufferedReader buf =new BufferedReader(new FileReader (new File("E:\\hw1.xls")));

ArrayList<Student> a =new ArrayList<Student>();

PrintWriter out =new PrintWriter(new FileOutputStream ("E:\\hw11.xls"),true);

out.print("FirstName\tLastName\tSSN\n");

String input = buf.readLine();

while (input!=null){

String[] str = input.split("/t");

Student s =new Student(str[0],str[1],str[2]);

a.add(s);

input = buf.readLine();

}

Object[] array = a.toArray();

Arrays.sort(array);

while(array!=null)

{

out.print(array);

}

buf.close();

out.close();

}

}

publicclass Studentimplements Comparable{

public String FirstName;

public String LastName;

public String SSN;

public Student (String f, String l, String s)

{

FirstName = f;

LastName= l;

SSN = s;

}

publicint compareTo(Object o){

Student s = (Student) o;

if(LastName.compareTo(s.LastName) != 0){

return LastName.compareTo(s.LastName);

}

else{

return FirstName.compareTo(s.FirstName);

}

}

}

[3632 byte] By [Jigisha] at [2007-11-27 10:43:55]
# 1

I would sugest making sure that str[0] through str[2] exist, but for starters...

String[] str = input.split("/t");

should this be

String[] str = input.split("\t");

?

jGardnera at 2007-7-28 20:03:34 > top of Java-index,Java Essentials,Java Programming...
# 2

You might want to have a look at line 25, and then highlight it for us if you still can't work out what's wrong

Help us to help you!

georgemca at 2007-7-28 20:03:34 > top of Java-index,Java Essentials,Java Programming...
# 3

I want to do like this way,

Read file by colum that is why I am spliting that and then i want to read the 3 colums in one line and put into another file.

Let me know if you have any solution for that.

Jigisha at 2007-7-28 20:03:34 > top of Java-index,Java Essentials,Java Programming...
# 4

hi,

I know that , there is something wrong with the line 25.

Student s = new Student(str[0],str[1],str[2]);

but i could not figure out how to deal with it.

Jigisha at 2007-7-28 20:03:34 > top of Java-index,Java Essentials,Java Programming...
# 5

> hi,

> I know that , there is something wrong with the line

> 25.

> Student s = new Student(str[0],str[1],str[2]);

>

> i could not figure out how to deal with it.

Check str.length first? Look before you leap?

BigDaddyLoveHandlesa at 2007-7-28 20:03:34 > top of Java-index,Java Essentials,Java Programming...
# 6

Ok, the long way then.

out.print("FirstName\tLastName\tSSN\n");

/* ... */

String[] str = input.split("/t");

Student s = new Student(str[0],str[1],str[2]);

You have a line "FirstName\tLastName\tSSN\n".

You try to split by the string "/t"

The string "/t" does not exist in the string, so it creates str as thus:

str = [ "FirstName\tLastName\tSSN\n" ]

As you can see, this array has one element... ergo str[1] is out of bounds. The string "/t" does not exist because you are using the tab character to separate things, and the tab character is "\t"

jGardnera at 2007-7-28 20:03:34 > top of Java-index,Java Essentials,Java Programming...
# 7

> hi,

> I know that , there is something wrong with the line

> 25.

> Student s = new Student(str[0],str[1],str[2]);

>

> i could not figure out how to deal with it.

What I meant was, we don't know which line 25 is

georgemca at 2007-7-28 20:03:34 > top of Java-index,Java Essentials,Java Programming...
# 8

The 25th line is

Student s = new Student(str[0],str[1],str[2]);

Jigisha at 2007-7-28 20:03:34 > top of Java-index,Java Essentials,Java Programming...
# 9

hi

I tried using String[] str = input.split("\t");

it worked, but when i ran the program, gave me huge .xls file which takes 2 min to open. Instead of small .xls file.

Jigisha at 2007-7-28 20:03:34 > top of Java-index,Java Essentials,Java Programming...
# 10

> hi

> I tried using String[] str = input.split("\t");

>

> it worked, but when i ran the program, gave me huge

> .xls file which takes 2 min to open. Instead of small

> .xls file.

I find that if I'm not required to write correct code, I can write code that

runs very quickly and produces very short files indeed.

BigDaddyLoveHandlesa at 2007-7-28 20:03:34 > top of Java-index,Java Essentials,Java Programming...
# 11

okay. let me see that.

Jigisha at 2007-7-28 20:03:34 > top of Java-index,Java Essentials,Java Programming...
# 12

Object[] array = a.toArray();

Arrays.sort(array);

while(array!=null)

{

out.print(array);

}

That will be an infinite loop.

Message was edited by:

jGardner

jGardnera at 2007-7-28 20:03:34 > top of Java-index,Java Essentials,Java Programming...
# 13

> BufferedReader buf = new BufferedReader(new FileReader (new File("E:\\hw1.xls")));

If that really is an Excel spreadsheet file, it isn't 100% text, it doesn't contain lines, and can't be read with a Reader. That would explain your array index problem inside InputStreamReader. It can't be read with an InputStream either, you will need JExcel or Apache POI/HSSF.

ejpa at 2007-7-28 20:03:34 > top of Java-index,Java Essentials,Java Programming...