Hi there,

A thread was started earlier about splitting up a String using the split() method of the string class. Reading that did help me quite a lot in solving a problem of mine that is VERY similiar.

Say I have this in a txt file:
"Dean Grobler 0794400541 NA NA dean3446@gmail.com
Avril Gibson 0825426532 NA NA avril@gmail.com
Peter Pumpkineater 0724128945 0119548714 peter@webmail.com"

and I have this class:

public class Contact
{
	String name,
	       surname,
	       mobile,
  	       home
                work
	       email;
		   
	public Contact(String name, String surname, String mobile, String home,
			String work, String email)
	{
		this.name = name;
		this.surname = surname;
		this.mobile = mobile;
 		this.home = home;
		this.work = work;
		this.email = email;
	}

	/*.
	  .
  	  .getters and setters etc.
	*/
}

How would I continue reading line by line in the file (regardless of how many lines of information there is in the .txt) and keep on popping out 'Contact objects'? So then in the above .txt example it would create 3 Contact objects?

I did this quick? Am I on the right track?

BufferedReader in;
String conInfo;
try{
     in=new BufferedReader(new FileReader("conList.txt"));
     while(in.readLine() != 0){
           String[] infoArray = conInfo.split(" ");
           //infoArray[0] = "dean"
           //infoArray[1] = "grobler"
           //infoArray[2] = "0794400541"
           //infoArray[3] = "NA"
           //infoArray[4] = "NA"
           //infoArray[5] = "dean3446@gmail.com"

           Contact con001 = new Contact(infoArray[0],infoArray[1],infoArray[2],infoArray[3],infoArray[4],infoArray[5]);

}
catch(IOException e){ //handle exception }

I know this is suppose to be easy but I've been having a bad week lol, my brain can't wrap around this... :-(

The content of the while loop expression is incorrect, read further down in that provided tutorial until you find where readLine is actually used in that tutorial. And you should use the regex for "one or more whitespace" (see the API docs for Pattern), rather than a single space in your split.

Okay, so I've change the (" ") in the split method. also the arguments for the while() loop. I really hope I'm getting closer? This is for my final assignment and I'm struggling alot!

also now, in the while() loop. Where a new Contact gets created called con001. Wouldn't this then just create a new con001 contact over and over again? Isn't there some way to make the next contact object be called con002, then con003 etc?

BuffereReader inputStream;
String conInfo;
try{
    in=new BufferedReader(new FileReader("conList.txt"));
    while ((conInfo = inputStream.readLine()) != null) {
         String infoArray = conList.split(\s);

         //infoArray[0]="dean"
         //infoArray[1]="grobler"
         //infoArray[2]="0794400541"
         //infoArray[3]="NA"
         //infoArray[4]="NA"
         //infoArray[5]="dean3446@gmail.com"

         contact con001 = new Contact(infoArray[0],infoArray[1],infoArray[2],infoArray[3],infoArray[4],infoArray[5]);
 
}catch(IOException e){ //handle error }

This is impossible :-(

As Masijade was saying you need the regular expression to get one (or more spaces). There are multiple places for you to test (and get help) with regex. One of my favorites is RegExr, as it provides a nice graphical approach and helps you visualize.

Anyway you are missing your " around your string. The \s that will only match one single whitespace character, you could possibly need more than one. So you need to do something like "[\s]+".

As Masijade mentioned, use the Pattern class to split the string using the above regex.

> Where a new Contact gets created called con001. Wouldn't this then just create a new con001 contact over and over again? Isn't there some way to make the next contact object be called con002, then con003 etc?

Add them to an ArrayList as you create them. You don't need variables for each one.

Edited 6 Years Ago by Ezzaral: n/a

BuffereReader inputStream;
String conInfo;
try{
    inputStream = new BufferedReader(new FileReader("conList.txt"));
    //Creates new ArrayList
    ArrayList contactObj = new ArrayList();	

    while ((conInfo = inputStream.readLine()) != null) {
         String infoArray = conList.split("\\s+");

         //infoArray[0]="dean"
         //infoArray[1]="grobler"
         //infoArray[2]="0794400541"
         //infoArray[3]="NA"
         //infoArray[4]="NA"
         //infoArray[5]="dean3446@gmail.com"
	
	//New Contact object gets created
        contact newCon = new Contact(infoArray[0],infoArray[1],infoArray[2],
				     infoArray[3],infoArray[4],infoArray[5]);
 	
	//Contact object gets added to arraylist
	contactObj.add(newCon);

	}//Close while loop

}catch(IOException e){ //handle error }

Thank you for everybodies help. I feel rather confident that the above code
is closer. If it is, after the entire txt has been read line by line (as
an example given right at the top of this post with the 3lines)

The result will be an ArrayList that now stores 3 Contact objects?:
contactObj[0].getName = "Dean"
contactObj[1].getName = "Avril"
contactObj[2].getName = "Peter"

This must be right now?

Except that contact in the "new Contact" lines needs to be capitalised in both places, of course. And I would do

ArrayList<Contact> contactObj = new ArrayList<Contact>();

Ah ofcourse, didn't see that there.

What is the <contact> for in the ArrayList<contact>?
The API is a bit unclear about this.

Other than that, you think my code will run as expected?
I wish I could test it myself but unfor tunately I am unable to do so as I'm not at a PC with the JDK installed...

As long as it looks okay to you then I'm happy. Thanks again for everything!
Have a great day! or night.. wherever you are in the world..
Cheers

This article has been dead for over six months. Start a new discussion instead.