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());
	}
}

Dani AI

Generated

A few focused points to check — these are the things that most often produce the "skipping" and "fields in the wrong place" behavior you described.

First, the parsing strategy. Token-based reads that call many next()/nextBoolean() calls in one loop will silently drift if a single token is missing or formatted slightly differently; parsing line-by-line is safer. Second, look for a logic bug that always inserts new contacts at position zero (that will make entries appear in odd places and reverse order). Third, the delimiter/format: a strict "comma+space" splitter breaks on lines that use no space, extra spaces, trailing commas, or embedded commas inside quoted fields. Also avoid swallowing exceptions with empty catch blocks — they hide the exact parse error.

A practical, minimal fix: read each input line, skip empty lines, split on a forgiving regex that allows optional spaces, verify you got the expected number of fields, parse the boolean explicitly, and append the Contact (do not reset an index inside the loop). For example:

BufferedReader r = new BufferedReader(new FileReader("input.txt"));
String line;
while ((line = r.readLine()) != null) {
  line = line.trim();
  if (line.isEmpty()) continue;
  String[] f = line.split(",\\s*");
  if (f.length < 12) { System.err.println("Bad line: " + line); continue; }
  boolean fav = Boolean.parseBoolean(f[11].trim());
  // map f[0..11] into a Contact and call contacts.add(contact);
}
r.close();

Other tips: stop writing a trailing separator when you save (it can create empty tokens), log exceptions (printStackTrace) while debugging, and if your CSV can contain quoted fields or commas inside values use a real CSV library (OpenCSV or Apache Commons CSV) rather than rolling your own parser.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.