Quantcast

Maximum PC

It is currently Sun Sep 21, 2014 10:46 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Java Assignment..I/O and GUI
PostPosted: Tue Nov 30, 2004 7:20 pm 
Little Foot
Little Foot

Joined: Tue Oct 12, 2004 12:51 pm
Posts: 127
Location: Atlanta
Ok I have this assignment for java class. My Prof is indian(no offense, just hard to understand). I understand how I/O works, and I understand how GUI works, but I am having trouble putting them together. This is our assignment:

Question #1 - name it Contact.java. Write an application with a class or classes that implement the GUI components (shown below) with actions provided in the Table:1. Please note that this GUI without any actions has been implemented under Assignment 7 - Question #2 (Assignment 7). You need to write a file on C drive (name the file information.txt) that will store information: name, phone number and email. You may directly write these information with a delimeter and use StringTokenizer class to break the information when you read back.


Table: 1 Buttons Actions to be generated

LOOKUP When the name of a person is entered in the text field labelled as name and the button is clicked, phone number and email address are shown in the text fields labelled as phone and email respectively for that person stored in a disk file. If the information does not exist in the disk file, "NOT FOUND" is written in the first text area.

INSERT When all information (name, phone and email) are entered in the text fields and the button is clicked, the information is written in a disk file

Exit Terminates the application

a picture of what I created is here:

http://science.kennesaw.edu/~mahmed/fall2004/csis2302/Contact.gif

and here is a link to what I have.

http://www.geocities.com/swanysto/contact.java

The program creates the information.txt file, but it does not pront the info to the file.
Any help would be greatly appreciated.
JoN


Top
  Profile  
 
 Post subject:
PostPosted: Tue Nov 30, 2004 7:40 pm 
There is no cause for alarm... yet
There is no cause for alarm... yet

Joined: Tue Nov 09, 2004 10:50 am
Posts: 626
Location: Vancouver Island
Post code using the code tags in the future - safer for us.

That said - I get:

unreported exception java.io.IOException; must be caught or declared to be thrown FileWriter fw=new FileWriter("c:/information.txt",append);

So... catch the exception ;)


Top
  Profile  
 
 Post subject:
PostPosted: Wed Dec 01, 2004 7:23 am 
Thunderbird
Thunderbird
User avatar

Joined: Wed Jul 07, 2004 1:13 pm
Posts: 817
Location: Missouri
Canucklehead wrote:
Post code using the code tags in the future - safer for us.

That said - I get:

unreported exception java.io.IOException; must be caught or declared to be thrown FileWriter fw=new FileWriter("c:/information.txt",append);

So... catch the exception ;)


I am not familar with FileWriter. It is new to Java SDK? I worked with 2.1.2 I believe in my class. I took it about year ago. I have not really kept current with new features with the newer SDKs.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Dec 01, 2004 8:12 am 
There is no cause for alarm... yet
There is no cause for alarm... yet

Joined: Tue Nov 09, 2004 10:50 am
Posts: 626
Location: Vancouver Island
baldeagle wrote:
It is new to Java SDK? I worked with 2.1.2 I believe in my class.


Current was 1.4.2, 1.5 was in beta and checking java.un.com it looks like they might have branded 1.5 as 5.0...

This this link for an example of using FileWriter - when in doubt, check the API. It will list when a class was introduced.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Dec 01, 2004 11:29 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
baldeagle wrote:
Canucklehead wrote:
Post code using the code tags in the future - safer for us.

That said - I get:

unreported exception java.io.IOException; must be caught or declared to be thrown FileWriter fw=new FileWriter("c:/information.txt",append);

So... catch the exception ;)


I am not familar with FileWriter. It is new to Java SDK? I worked with 2.1.2 I believe in my class. I took it about year ago. I have not really kept current with new features with the newer SDKs.

FileWriter has been around since Java 1.1 which puts it pre-1998. My guess would be sometime in 1996. Version 2.1.2 isn't scheduled for release until 2011. :)

The FileWriter class is less commonly used these days since the introduction of the new IO classes in Java 2 (versions 1.2.0 to 1.4.2) which introduced the BufferedReader and BufferedWriter classes. As with all IO in Java, you are required to handle exceptions - no exceptions allowed (sorry, couldn't resist).


Top
  Profile  
 
 Post subject:
PostPosted: Mon Dec 06, 2004 11:49 am 
Bitchin' Fast 3D Z8000
Bitchin' Fast 3D Z8000

Joined: Mon Jun 14, 2004 2:50 pm
Posts: 45
Location: Rochester, NY
A couple of things you'll want to fix.

First, when you're reading the data from each text box, you're not really reading in what is entered.

Code:
   try {
      boolean append = true;
      FileWriter fw = new FileWriter( "c:/information.txt", append );
      BufferedWriter buffer1 = new BufferedWriter( fw );
      String concat = text1.getText() + "*" + text2.getText() +
                            "*" + text3.getText();
      buffer1.write( concat );
      buffer1.newLine();
      buffer1.close();
      text1.setText( "" );
      text2.setText( "" );
      text3.setText( "" );
   } catch ( FileNotFoundException fnfe ) {
      System.err.println( "FileNotFoundException Error." );
   } catch ( Exception exc ) {
      System.err.println( "An exception occured." );
   }


Notice how I called the getText() method for the TextBox objects? If you don't do that and simply concatenate the objects, you're invoking the toString() method of TextBox which gives you a bunch of information pertaining to the object instance and tells you nothing of its state. (I do recommend you try it that way and look at the text file to see how they've implemented their toString() method.)

Notice the try-catch block. Any type of I/O is going to require some sort of exception handling. Now, because you're using the ActionListener interface, you can't take the easy way out and attach the 'throws IOException' at the end of the actionPerformed() method signature. Otherwise, you could have gotten away with it.

Also, to add some refinement to the GUI, I clear the text boxes once you click the INSERT button. It's not necessary, but it's cleaner.


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group