We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,437 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Reading from CSV file/delimiter issues

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());
	}
}
2
Contributors
1
Reply
16 Hours
Discussion Span
2 Years Ago
Last Updated
2
Views
adaniel058
Newbie Poster
18 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

kramerd
Posting Pro in Training
405 posts since Sep 2010
Reputation Points: 49
Solved Threads: 75
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0551 seconds using 2.7MB