In: Computer Science
I just finished this program where the program reads a text file of full names , first and last name, and a zip code. It reads the items then stores the first name and last name and zipcodes as an objects and prints the items. However, when i use my text file i get this error
"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1 "
I notice this issue only happens when the names are formatted by tabs inbetween. For example:
"Firstname Lastname 90210" <-- this prints normally because there is only a space in between each value
"Firstname (tab) Lastname (tab) 90210" <--- Does not print because of the tab spaces but I need it to print in this fashion!
Please help me fix this annoying issue! Thank you!
Please remember the names are read from a txt file thats been pre-created. Not user inputted values.
import java.io.*;
class Details//stores the firstname, lastname and zip
{
String firstname, lastname, zip;
public Details(String a, String b, String
c)//constructor
{
firstname = a;lastname = b;zip
= c;
}
public String getFirstName()//getter methods
{
return firstname;
}
public String getLastName()
{
return lastname;
}
public String getZip()
{
return zip;
}
}
public class A {
public static void main(String[] args)
{
Details details[] = new
Details[25];int i=0;
BufferedReader reader;
try
{
reader
= new BufferedReader(new FileReader("myfile.txt"));//reader object
for file
String
line = reader.readLine();//read next line
while
(line != null) {
// read next line
String[] res = line.split(" ");//split the line according to spaces
and store it in res
details[i++] = new Details(res[0], res[1], res[2]);//create a
details object with the individual strings as parameters
line = reader.readLine();//read next line
}
reader.close();
}
catch (IOException e)
{
e.printStackTrace();
}
System.out.println("FirstName
LastName ZIP");
for(int
j=0;j<i;j++)//display the contents of objects
{
System.out.println(details[j].getFirstName() + "\t " +
details[j].getLastName() + "\t " + details[j].getZip());
}
}
}
Please find the code below.
We just have to modify Line number 37 at split(" ") to split("\\s+) . It is a regex and it tells that More than one space will be used as a split condition.
SOURCE CODE:
*Please follow the comments to better understand the code.
**Please look at the Screenshot below and use this code to copy-paste.
***The code in the below screenshot is neatly indented for better understanding.
import java.io.*; class Details//stores the firstname, lastname and zip { String firstname, lastname, zip; public Details(String a, String b, String c)//constructor { firstname = a;lastname = b;zip = c; } public String getFirstName()//getter methods { return firstname; } public String getLastName() { return lastname; } public String getZip() { return zip; } } public class A { public static void main(String[] args) { Details details[] = new Details[25];int i=0; BufferedReader reader; try { reader = new BufferedReader(new FileReader("myfile.txt"));//reader object for file String line = reader.readLine();//read next line while (line != null) { // read next line String[] res = line.split("\\s+");//split the line according to one or more \\s+ regexspaces and store it in res details[i++] = new Details(res[0], res[1], res[2]);//create a details object with the individual strings as parameters line = reader.readLine();//read next line } reader.close(); } catch (IOException e) { e.printStackTrace(); } System.out.println("FirstName LastName ZIP"); for(int j=0;j<i;j++)//display the contents of objects { System.out.println(details[j].getFirstName() + "\t " + details[j].getLastName() + "\t " + details[j].getZip()); } } }
============
SAMPLE OUTPUT:
File
OUTPUT: