9 Replies - 515 Views - Last Post: 03 August 2012 - 12:33 PM Rate Topic: -----

#1 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Binary file to ComboBox

Posted 03 August 2012 - 11:28 AM

So, my final project for Java class is complete. I have a JavaPhone object, and in this object I made 3 arrays of Strings (firstname, lastname, phone). I then wrote these "Contacts" to a binary file. Technically, that's all I need to do.

But I'm thinking I may want to read that binary file, and put the contacts into a combo box for the user to select who they would like to call. I KIND OF know how this would work, but I want to double check so I don't waste 2 days figuring this out.

1. I have to read the file in, and parse each field to a new String array

2. I have to use the String array to populate the combo box

3. Display the combo box (JDialog?), and let the user select a contact to call.

Does that sound right? Any advice?

Here is the code that creates my Contacts file:

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;




public class Contacts 
{

	public static void main(String[] args)
	{
		Contacts fileIO = new Contacts();
		
		
		String lName[] = new String[3];
		String fName[] = new String[3];
		String phone[] = new String[3];
		
		
		lName[0] = "Who";
		lName[1] = "Potter";
		lName[2] = "Hobbit";
		lName[3] = "The Gray";
		
		fName[0] = "Doctor";
		fName[1] = "Harry";
		fName[2] = "Frodo";
		fName[3] = "Gandolf";
		
		phone[0] = "(770) 335 2536";
		phone[1] = "(842) 589 1111";
		phone[2] = "(910) 963 2201";
		phone[3] = "(770) 457 1625";
		
		
		for(int i = 0; i<3; i++)
		{
			fileIO.Write(lName[i], fName[i], phone[i]);
		}
		
		
	}//END MAIN
	
	
	public void Write(String lName, String fName, String phone)
	{
		DataOutputStream output;
        output = null;
        File fileOut = new File("myContacts.data");
        
        try
        {
        	output = new DataOutputStream(new FileOutputStream(fileOut));
        }
        catch(IOException i)
        {System.out.println(i);}
        
        try{                               //try for write
      	  	output.writeUTF("Last Name: ");
        	output.writeUTF(lName);
        	output.writeUTF("First Name: ");
      	  	output.writeUTF(fName);
      	  	output.writeUTF("Phone: ");
    	  	output.writeUTF(phone);
      	  	
      	     }catch (IOException i)
      	         {System.out.println("error in write");}
        try{                            //try for close
 		   output.close();
         }catch (IOException i)
            {System.out.println("error in close");}

		
	}
	

	
}//END CONTACTS


This post has been edited by synlight: 03 August 2012 - 11:29 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Binary file to ComboBox

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Binary file to ComboBox

Posted 03 August 2012 - 11:37 AM

Yes but would be easier if:
- you use and ObjectOutputStream in that case you can simply writeObject(your Contact)
- then read back the Contact object
- it you have a toString() method in Contact, just put the Contact(s) into the ComboBox
Was This Post Helpful? 1
  • +
  • -

#3 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Binary file to ComboBox

Posted 03 August 2012 - 11:41 AM

Heres a couple of pointers that will make your life easier.

I highly suggest you a class that represents 1 contact, to avoid confustion lets call it an Acquaintance. The Acquaintance class will store a fname, lname, & number. By doing this you can read in a line of data and save it the a list of Acquaintances. It will be much easier to manipulate the data, and when the user selects a person, it will be easy to access the relevant data, like their phone number.Theres also a few members on this board that will yell at you for your parrellel array usage :bigsmile: .

Yes the combo box should work fine, but your going to need to put that in a JPanel
Was This Post Helpful? 0
  • +
  • -

#4 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Binary file to ComboBox

Posted 03 August 2012 - 11:47 AM

View Postpbl, on 03 August 2012 - 01:37 PM, said:

Yes but would be easier if:
- you use and ObjectOutputStream in that case you can simply writeObject(your Contact)
- then read back the Contact object
- it you have a toString() method in Contact, just put the Contact(s) into the ComboBox


Okay, so after a quick read of the API for the OOS, I'm having a hard time understanding the difference between Object and Data output streams. I get that the OOS can write objects like arrays, but it seems like it can read it back in, too? Is that correct? Whereas in a datastream I would have to open a new datainput stream? Forgive me if I seem dumb, I'm still struggling with APIs, they contain a lot of info I don't understand yet.
Was This Post Helpful? 0
  • +
  • -

#5 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Binary file to ComboBox

Posted 03 August 2012 - 11:52 AM

View PostSwiftStriker00, on 03 August 2012 - 01:41 PM, said:

Heres a couple of pointers that will make your life easier.

I highly suggest you a class that represents 1 contact, to avoid confustion lets call it an Acquaintance. The Acquaintance class will store a fname, lname, & number. By doing this you can read in a line of data and save it the a list of Acquaintances. It will be much easier to manipulate the data, and when the user selects a person, it will be easy to access the relevant data, like their phone number.Theres also a few members on this board that will yell at you for your parrellel array usage :bigsmile: .

Yes the combo box should work fine, but your going to need to put that in a JPanel


so... the class would pull in a name from the stream and put it into a list... the only lists I know of are arrays. I have heard of linked lists, but haven't read anything on them, they're not even in my book. Heck, half of what I do isn't in my textbook.

As for parallel arrays, well, I don't think I'm doing to badly for week 6 of my first Java class... :whistling:

What would have been a more appropriate way to handle the data?

EDIT: hmm. this seems helpful object serialization tutorial

are you talking about using an Array List to store my Contacts?

This post has been edited by synlight: 03 August 2012 - 11:57 AM

Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,207
  • Joined: 20-September 08

Re: Binary file to ComboBox

Posted 03 August 2012 - 11:59 AM

Quote

Yes but would be easier if:
- you use and ObjectOutputStream in that case you can simply writeObject(your Contact)
- then read back the Contact object


That's absolutely write, but pbl hasn't noticed there isn't a Contact object. There should be (then you can do that)
Was This Post Helpful? 0
  • +
  • -

#7 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Binary file to ComboBox

Posted 03 August 2012 - 12:02 PM

View Postg00se, on 03 August 2012 - 01:59 PM, said:

Quote

Yes but would be easier if:
- you use and ObjectOutputStream in that case you can simply writeObject(your Contact)
- then read back the Contact object


That's absolutely write, but pbl hasn't noticed there isn't a Contact object. There should be (then you can do that)


This line doesn't instantiate a Contacts object?
Contacts fileIO = new Contacts();


I'm not being an ass, I'm really asking.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Binary file to ComboBox

Posted 03 August 2012 - 12:14 PM

Actually yes you can use an ArrayList and put it into a file or individually write your object
Here is a quick example

class Contact implements Serializable {

    String name;
    int age;

    Contact(String name, int age) {
       this.name = name;
       this.age = age;
    }

    public String toString() {
       return name + " is " + age + " years old";
    }

    public static void main(String[] args) {
       String[] name = {"Peter", "Paul", "Mary"};
       int[] age = {35, 40, 45};
       try {
           FileOutputStream fos = new FileOutputStream("contact.dat");
           ObjectOutputStream oos = new ObjectOutputStream(fos);
           oos.writeInt(name.length);
           for(int i = 0; i < name.length; ++i)
              oos.writeObject(new Contact(name[i], age[i]));
           oos.close();
           fos.close();
       }
       catch(IOException e) {
           System.out.println("Problem: " + e);
           return;
       }

       try {
           FileInputStream fis = new FileInputStream("contact.dat");
           ObjectInputStream ois = new ObjectInputStream(fis);
           int nb = ois.readInt();
           for(int i = 0; i < nb; ++i) {
             Contact c = (Contact) ois.readObject();
             System.out.println(c);
           }
           ois.close();
           fis.close();
       }
       catch(IOException e) {
           System.out.println("Problem: " + e);
           return;
       }
   }
}


Was This Post Helpful? 1
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Binary file to ComboBox

Posted 03 August 2012 - 12:29 PM

or
class Contact implements Serializable {

    String name;
    int age;

    Contact(String name, int age) {
       this.name = name;
       this.age = age;
    }

    public String toString() {
       return name + " is " + age + " years old";
    }

    public static void main(String[] args) {
       String[] name = {"Peter", "Paul", "Mary"};
       int[] age = {35, 40, 45};
       ArrayList<Contact>al = new ArrayList<Contact>(name.length);
       for(int i = 0; i < name.length; ++i)
          all.add(new Contact(name[i], age[i]));

       try {
           FileOutputStream fos = new FileOutputStream("contact.dat");
           ObjectOutputStream oos = new ObjectOutputStream(fos);
           oos.writeObj(al);
           oos.close();
           fos.close();
       }
       catch(IOException e) {
           System.out.println("Problem: " + e);
           return;
       }

       try {
           FileInputStream fis = new FileInputStream("contact.dat");
           ObjectInputStream ois = new ObjectInputStream(fis);
           al = (ArrayList<Contact>) ois.readObject();
           ois.close();
           fis.close();
       }
       catch(IOException e) {
           System.out.println("Problem: " + e);
           return;
       }
       for(int i = 0; i < al.size(); ++i) 
         System.out.println(al.get(i));
           
   }
}


Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,207
  • Joined: 20-September 08

Re: Binary file to ComboBox

Posted 03 August 2012 - 12:33 PM

Quote

This line doesn't instantiate a Contacts object?


It does, but i said a Contact object. pbl has given you the idea, but it would be better were it bean-compliant:

public class Contact implements Serializable {
    private String firstName;
    private String firstName;
    private String phoneNumber;

    public Contact(){}

    public String getFirstName() {
        return this.firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getFirstName() {
        return this.firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getPhoneNumber() {
        return this.phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
    //TODO
    //Add constructor(s) and override toString appropriately
}


This post has been edited by g00se: 03 August 2012 - 12:35 PM
Reason for edit:: forgot Serializable

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1