dkim18
asked on
reading multiple lines
Hi,
I am trying to read multiple line from a file, but I get this error.
If a file contains one line, it works fine.
++++++++++++++++++++++++++ ++++++++++ ++++++++++
lastName:FirstName
firstName:LastName
zip:99999
lastName:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfB oundsExcep tion: 1
I am trying to read multiple line from a file, but I get this error.
If a file contains one line, it works fine.
++++++++++++++++++++++++++
lastName:FirstName
firstName:LastName
zip:99999
lastName:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfB
public String[] readFromFile(File selectedFile){
File file = new File(selectedFile.toString());
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
String []contacts=null;
try {
fis = new FileInputStream(file);
// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
contactList = new LinkedList(); // Doubly-linked list
while (dis.available() != 0) {
String[] results = dis.readLine().split(", ");
String lastName = results[0];
System.out.println("lastName:" + lastName);
String firstName = results[1];
System.out.println("firstName:" + firstName);
String zip = results[2];
System.out.println("zip:" + zip);
contactList.add(lastName +", " + firstName +", " +zip);
}
//contactList.add("asdfa" +", " + "fsdfsf" +", " +"1111");
contacts = new String[contactList.size()];
for(int i=0; i< contactList.size(); i++){
contacts[i] = (String)contactList.get(i);
System.out.println(contacts[i]);
}
// dispose all the resources after using them.
fis.close();
bis.close();
dis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return contacts;
}
unless contactList on line 16 was declared globally outside the function, it needs it's datatype in front:
LinkedList contactList = new LinkedList(); // Doubly-linked list
LinkedList contactList = new LinkedList(); // Doubly-linked list
If you just replace ':' with '=" you can simply load it as a Properties file
ASKER
I don't see ':' in my code.
>>
lastName:FirstName
firstName:LastName
zip:99999
>>
lastName:FirstName
firstName:LastName
zip:99999
>>
what does the input file look like?
ASKER
Doe, John, 23221
Smith, Tom, 98432
++++++++++
System.out.println("lastNa me:" + lastName); --> debugging purpose only
I need to store in string array. (variable contacts).
Smith, Tom, 98432
++++++++++
System.out.println("lastNa
I need to store in string array. (variable contacts).
You need to simply consume space, not specify it, so
// String[] results = dis.readLine().split(", "); NO!
String[] results = dis.readLine().split("\\s*,\\s*"); // YES!
You can results to a List
ASKER
String[] results = dis.readLine().split("\\s* ,\\s*");// --- I still get a same error
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thx!
and you are looking for 3 tokens on each line and the input file only has 2