1,105,592 Community Members

Reading from CSV file/delimiter issues

Member Avatar
adaniel058
Newbie Poster
18 posts since Jun 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I can not figure out why my code is reading in data to my vector in funny ways. I have added the input file and necesary class to test.
It seems to be skipping values in the input file and also adding things to seperate places in the vector instead of one place like I intended.

Any help would be much appreciated.

import java.util.*;
import java.io.*;

public class AddressBook {
	public static Vector<Contact> contacts = new Vector<Contact>(100, 10);
	
	//buildBook creates Contacts from the input txt file and adds them to a
	//vector. This vector is our Address Book
	public static void buildBook() {
		try {
			Scanner in = new Scanner(new File("input.txt"));
			in.useDelimiter(", ");
			while(in.hasNext()) {
				Contact temp = new Contact();
				int index = 0;
				temp.setfirstName(in.next());
				temp.setlastName(in.next());
				temp.sethomePhone(in.next());
				temp.setworkPhone(in.next());
				temp.setmobilePhone(in.next());
				temp.setaddress(in.next());
				temp.setcity(in.next());
				temp.setstate(in.next());
				temp.setzip(in.next());
				temp.setemail(in.next());
				temp.setbirthday(in.next());
				temp.setfavorite(in.nextBoolean());
				contacts.add(index, temp);
				index++;
			}
			in.close();
		}
		catch(FileNotFoundException e) {};
	}
	
	//adds a contact to the end of the vector
	public static void addContact(Contact contact) {
		Contact temp = contact;
		contacts.addElement(temp);
	}
	
	//removes a contact from the vector
	public static void removeContact(Contact contact) {
		Contact temp = contact;
		contacts.remove(temp);
	}
	
	//the save method will keep our vector and address book database up-to-date
	public static void Save() {
		//everytime the 'save' button is pressed the vector will pass all Contacts into an array
		Contact myArray[] = new Contact[contacts.size()];
		contacts.toArray(myArray);
		try {
			//the array will then be iterated through, creating a new input file with any changes that have been made
			PrintWriter out = new PrintWriter(new File("input.txt"));
			for(int i = 0; i < contacts.size(); i++) {
				Contact temp = myArray[i];
				out.print(temp.getfirstName() + ", ");
				out.print(temp.getlastName() + ", ");
				out.print(temp.gethomePhone() + ", ");
				out.print(temp.getworkPhone() + ", ");
				out.print(temp.getmobilePhone() + ", ");
				out.print(temp.getaddress() + ", ");
				out.print(temp.getcity() + ", ");
				out.print(temp.getstate() + ", ");
				out.print(temp.getzip() + ", ");
				out.print(temp.getemail() + ", ");
				out.print(temp.getbirthday() + ", ");
				out.print(temp.getfavorite() + ", ");
				out.println();
			}
			out.close();
		}
		catch(FileNotFoundException e) {};
		//it will then reset the vector
		contacts.removeAllElements();
		//and rebuild using the new/updated input file
		buildBook();
	}
	
	public static void main(String[] args) {
		buildBook();
		Contact test = contacts.get(0);
		System.out.println(test.getfirstName());
	}
}
Member Avatar
kramerd
Posting Pro in Training
404 posts since Sep 2010
Reputation Points: 29 [?]
Q&As Helped to Solve: 75 [?]
Skill Endorsements: 0 [?]
 
0
 

Without seeing your Contact class and your input file I can't know for sure, but it could be that your problem has to do with reading a string vs. reading a line from the file. Right now your read loop looks something like this:
- Check if the file has another string and if so,
- read a bunch of data from the file
- Repeat.

A better approach might be to do something like this:
- Check if the file has another line and if so,
- Read the next line from the file
- Parse the line and store into your Contact data structure
- Repeat.

You can parse the line using either another Scanner, or using the split method from the String class which returns an array of strings.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: