?
Solved

reading multiple lines

Posted on 2009-04-22
12
Medium Priority
?
741 Views
Last Modified: 2012-06-21
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.ArrayIndexOutOfBoundsException: 1
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;
	}

Open in new window

0
Comment
Question by:dkim18
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24211364
You're splitting on , but the items are delimited by :
and you are looking for 3 tokens on each line and the input file only has 2

0
 

Expert Comment

by:benbu
ID: 24211930
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24213031
If you just replace ':' with '="  you can simply load it as a Properties file
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:dkim18
ID: 24215307
I don't see ':' in my code.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24215343
>>

lastName:FirstName
firstName:LastName
zip:99999

>>
0
 

Expert Comment

by:benbu
ID: 24215417
what does the input file look like?
0
 

Author Comment

by:dkim18
ID: 24215924
Doe, John, 23221
Smith, Tom, 98432

++++++++++

System.out.println("lastName:" + lastName); --> debugging purpose only
I need to store in string array. (variable contacts).
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24215977
You need to simply consume space, not specify it, so
// String[] results = dis.readLine().split(", "); NO!
 
String[] results = dis.readLine().split("\\s*,\\s*"); // YES!

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24215986
You can results to a List
0
 

Author Comment

by:dkim18
ID: 24216081
String[] results = dis.readLine().split("\\s*,\\s*");// --- I still  get a same error
0
 

Accepted Solution

by:
benbu earned 1000 total points
ID: 24216183
I ran your code on my system and it worked just fine. When I added a blank line to the input file with a space on it I got your error. Make sure there are no blank lines at the bottom or top of your input file.
0
 

Author Closing Comment

by:dkim18
ID: 31573621
thx!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question