Link to home
Start Free TrialLog in
Avatar of dkim18
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.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

Avatar of Mick Barry
Mick Barry
Flag of Australia image

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

Avatar of benbu
benbu

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
If you just replace ':' with '="  you can simply load it as a Properties file
Avatar of dkim18

ASKER

I don't see ':' in my code.
>>

lastName:FirstName
firstName:LastName
zip:99999

>>
what does the input file look like?
Avatar of dkim18

ASKER

Doe, John, 23221
Smith, Tom, 98432

++++++++++

System.out.println("lastName:" + lastName); --> debugging purpose only
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!

Open in new window

You can results to a List
Avatar of dkim18

ASKER

String[] results = dis.readLine().split("\\s*,\\s*");// --- I still  get a same error
ASKER CERTIFIED SOLUTION
Avatar of benbu
benbu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dkim18

ASKER

thx!