Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
  #1  
Old May 7th, 2008, 06:16 PM
JMV290 JMV290 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 9 JMV290 User rank is Private First Class (20 - 50 Reputation Level)JMV290 User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 h 27 m 19 sec
Reputation Power: 0
Homework - Help with a Java GUI assignment

I need the following tasks implemented in the code I wrote below. I would really appreciate if someone could achieve this because it would help me understand the coding process for the next code I have to write. Below are the four classes of code I have so far.

save an array of social security numbers to a file
read this file and display the social security numbers saved

The JFileChooser class must be used to let the user select files for the storage and retrieval of the data.

Make sure the code handles user input and I/O exceptions properly. This includes a requirement that the main() routine does not throw any checked exceptions.

As a part of the code testing routine, design and invoke a method that compares the data saved to a file with the data retrieved from the same file. The method should return a boolean value indicating whether the data stored and retrieved are the same or not.


Code:
/*
 * SSNArray.java
 *
 * Created on February 28, 2008, 9:45 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package exceptionhandling;

import java.util.InputMismatchException; // program uses class InputMismatchException
import org.omg.PortableInterceptor.ORBInitInfoPackage.DuplicateName;

/**
 *
 * @author mer81348
 */
public class SSNArray
{
    public static int SOCIAL_SECURITY_NUMBERS = 10;
    private String[] socialArray = new String[SOCIAL_SECURITY_NUMBERS];
    private int socialCount = 0;
    
    /** Creates a new instance of SSNArray */
    public SSNArray ()
    {
    }
    
    public SSNArray ( String[] ssnArray, int socialNumber )
    {
        
        socialArray = ssnArray;
        socialCount = socialNumber;
        
    }
    public int getSocialCount ()
    {
        return socialCount;
    }
    
    public String[] getSocialArray ()
    {
        return socialArray;
    }
    
    public void addSocial ( String index )
    {
        socialArray[socialCount] = index;
        socialCount++;
    }
    
    public String toString ()
    {
        StringBuilder socialString = new StringBuilder ();
        
        for ( int stringValue = 0; stringValue < socialCount; stringValue++ )
        {
            socialString.append ( String.format ("%6d%32s\n", stringValue, socialArray[stringValue] ) );
        }
        
        return socialString.toString ();
    }
    
    public void validateSSN ( String socialInput ) throws InputMismatchException, DuplicateName
    {
        if (socialInput.matches ("\\d{9}"))
            
            return;
        
        else
            
            throw new InputMismatchException ("ERROR! Incorrect data format");        
    }    
    
}



Code:
/*
 * SSNArrayTest.java
 *
 * Created on February 28, 2008, 9:46 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package exceptionhandling;

import java.util.InputMismatchException; // program uses class InputMismatchException
import java.util.Scanner; // program uses class Scanner
import org.omg.PortableInterceptor.ORBInitInfoPackage.DuplicateName;

/**
 *
 * @author mer81348
 */
public class SSNArrayTest
{
    
    /** Creates a new instance of SSNArrayTest */
    public SSNArrayTest ()
    {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main (String[] args)
    {
        
        // create Scanner to obtain input from command window
        Scanner input = new Scanner ( System.in );
        
        System.out.printf ( "\nWELCOME TO SOCIAL SECURITY NUMBER CONFIRMER\n" );
        
        SSNArray arrayStorage = new SSNArray ();
        
        for( int socialNumber = 0; socialNumber < SSNArray.SOCIAL_SECURITY_NUMBERS; )
        {
            String socialString = ( "\nPlease enter a Social Security Number" );
            System.out.println (socialString);
            String socialInput = input.next ();
                       
            try
            {
                arrayStorage.validateSSN (socialInput);
                
                arrayStorage.addSocial (socialInput);
                socialNumber++;
                
            }
            
            catch (InputMismatchException e)
            {               
                System.out.println ( "\nPlease reenter Social Security Number\n" + e.getMessage() );
            }
            
            catch (DuplicateName e)
            {
                
            }
        
        }
        
        System.out.printf ("\nHere are all your Social Security Numbers stored in our database:\n");
        System.out.printf ( "\n%8s%32s\n", "Database Index", "Social Security Numbers" );
        
        System.out.println (arrayStorage);
        
    }
}



Code:
/*
 * SSNArrayExpanded.java
 *
 * Created on February 28, 2008, 9:52 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package exceptionhandling;

import java.util.InputMismatchException;
import org.omg.PortableInterceptor.ORBInitInfoPackage.DuplicateName;

/**
 *
 * @author mer81348
 */
public class SSNArrayExpanded extends SSNArray
{
    
    /** Creates a new instance of SSNArrayExpanded */
    public SSNArrayExpanded ()
    {
    }
    public SSNArrayExpanded ( String[] ssnArray, int socialNumber )
    {
        
        super ( ssnArray, socialNumber );
    }
    
    public void validateSSN ( String socialInput ) throws InputMismatchException, DuplicateName
    {
        super.validateSSN (socialInput);
        {
            int storedSocial = getSocialCount ();
            for (int socialMatch = 0; socialMatch < storedSocial; socialMatch++ )
            {
                if (socialInput.equals (getSocialArray () [socialMatch]))
                {
                    throw new DuplicateName ();
                }
            }
        }
        
        return;
    }
    
}


Code:
/*
 * SSNArrayTestExpanded.java
 *
 * Created on February 28, 2008, 9:53 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package exceptionhandling;

import java.util.InputMismatchException; // program uses class InputMismatchException
import java.util.Scanner; // program uses class Scanner
import org.omg.PortableInterceptor.ORBInitInfoPackage.DuplicateName;

/**
 *
 * @author mer81348
 */
public class SSNArrayTestExpanded
{
    
    /** Creates a new instance of SSNArrayTest */
    public SSNArrayTestExpanded ()
    {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main (String[] args)
    {
        
        // create Scanner to obtain input from command window
        Scanner input = new Scanner ( System.in );
        
        System.out.printf ( "\nWELCOME TO SOCIAL SECURITY NUMBER CONFIRMER\n" );
        
        SSNArrayExpanded arrayStorage = new SSNArrayExpanded();  
        
        for( int socialNumber = 0; socialNumber < SSNArray.SOCIAL_SECURITY_NUMBERS; )
        {
            String socialString = ( "\nPlease enter a Social Security Number" );
            System.out.println (socialString);
            String socialInput = input.next ();
            
            
            try
            {
                arrayStorage.validateSSN (socialInput);
                
                arrayStorage.addSocial (socialInput);
                socialNumber++;
                
            }
            
            catch (InputMismatchException e)
            {                

            }
            
            catch (DuplicateName e)
            {
                System.out.println ( "\nSocial Security Number is already claimed!\n" + "ERROR: " + e.getMessage() );  
            }
            
        }
        
        System.out.printf ("\nHere are all your Social Security Numbers stored in our database:\n");
        System.out.printf ( "\n%8s%32s\n", "Database Index", "Social Security Numbers" );
        
        System.out.println (arrayStorage);
        
    }
}





Thanks in advance.

Reply With Quote
  #2  
Old May 7th, 2008, 07:12 PM
crownjewel82's Avatar
crownjewel82 crownjewel82 is offline
rebel with a cause
Dev Shed God (5000 - 5499 posts)
 
Join Date: May 2004
Location: The Batsh!t Crazy State.
Posts: 5,349 crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)  Folding Points: 32123 Folding Title: Starter FolderFolding Points: 32123 Folding Title: Starter Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 16 h 11 m 14 sec
Reputation Power: 1935
This is not a do your homework for your forum. If you'd like to get help on a specific problem then ask a specific question.
__________________
The day I get my hands on the cookbook it's all over. -nicky

Reply With Quote
  #3  
Old May 7th, 2008, 07:36 PM
JMV290 JMV290 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 9 JMV290 User rank is Private First Class (20 - 50 Reputation Level)JMV290 User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 h 27 m 19 sec
Reputation Power: 0
Quote:
Originally Posted by crownjewel82
This is not a do your homework for your forum. If you'd like to get help on a specific problem then ask a specific question.
No, you're not doing my homework, you're helping me understand the concept through my code so I can learn to accomplish it for the next part of the same code I must write: how to make the proper method that compares the data saved to a file with data retrieved from the same file.

I have already written most of the code, it's just that part I'm having difficulties with. Sorry for making it look like I was trying to have you do my work earlier.

Reply With Quote
  #4  
Old May 8th, 2008, 04:27 AM
Hugh of Borg's Avatar
Hugh of Borg Hugh of Borg is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2004
Location: Switzerland
Posts: 568 Hugh of Borg User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hugh of Borg User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hugh of Borg User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hugh of Borg User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hugh of Borg User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hugh of Borg User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hugh of Borg User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 6 Days 19 h 4 m 40 sec
Reputation Power: 82
Take a look at the Java I/O Tutorial. That should get you started with the reading and writing from files.

Draw or write out on paper how the program flow should be for the new version that incorporates these new features and then translate it to code.

If that doesn't help you, then please be more specific about the problem you have.
__________________
- Hugh of Borg

The first thing young borg are taught: Keep away from Microsoft software!

Reply With Quote
  #5  
Old May 8th, 2008, 02:53 PM
JMV290 JMV290 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 9 JMV290 User rank is Private First Class (20 - 50 Reputation Level)JMV290 User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 h 27 m 19 sec
Reputation Power: 0
Quote:
Originally Posted by Hugh of Borg
Take a look at the . That should get you started with the reading and writing from files.

Draw or write out on paper how the program flow should be for the new version that incorporates these new features and then translate it to code.

If that doesn't help you, then please be more specific about the problem you have.
Thank you very much. You don't know how helpful that is. I tried asking on Sun's forums was pretty much insulted instead being offered help.

Thanks again.

Reply With Quote
  #6  
Old May 9th, 2008, 04:50 AM
Hugh of Borg's Avatar
Hugh of Borg Hugh of Borg is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2004
Location: Switzerland
Posts: 568 Hugh of Borg User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hugh of Borg User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hugh of Borg User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hugh of Borg User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hugh of Borg User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hugh of Borg User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hugh of Borg User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 6 Days 19 h 4 m 40 sec
Reputation Power: 82
Ok. Let my try this another way...

What exactly is your problem?

Do you not understand the code as it is?
Do you need help with handling files?
Do you have problems with the FileChooser?
Do you need help analyzing the coding assignment?

Reply With Quote
  #7  
Old May 9th, 2008, 09:15 AM
crownjewel82's Avatar
crownjewel82 crownjewel82 is offline
rebel with a cause
Dev Shed God (5000 - 5499 posts)
 
Join Date: May 2004
Location: The Batsh!t Crazy State.
Posts: 5,349 crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)  Folding Points: 32123 Folding Title: Starter FolderFolding Points: 32123 Folding Title: Starter Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 16 h 11 m 14 sec
Reputation Power: 1935
Quote:
Originally Posted by JMV290
Thank you very much. You don't know how helpful that is. I tried asking on Sun's forums was pretty much insulted instead being offered help.

Thanks again.
Look if the way you asked there was similar to the way you asked here then I can understand why. You've yet to really ask a direct question. Here read the instructions and try again.

Reply With Quote
  #8  
Old May 9th, 2008, 01:33 PM
JMV290 JMV290 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 9 JMV290 User rank is Private First Class (20 - 50 Reputation Level)JMV290 User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 h 27 m 19 sec
Reputation Power: 0
Quote:
Originally Posted by Hugh of Borg
Ok. Let my try this another way...

What exactly is your problem?

Do you not understand the code as it is?
Do you need help with handling files?
Do you have problems with the FileChooser?
Do you need help analyzing the coding assignment?
I need help with handling files. I need to save an array of social security numbers(not real ones, of course) to a text file.
I want the input from the user saved into a separate text file after the code finishes creating the "database" output. Therefore, all the 9 digit numbers in the "Social Security Numbers" column created by the output should be included in the new text file.




That's the output of the program currently.

Reply With Quote
  #9  
Old May 10th, 2008, 10:30 AM
Hugh of Borg's Avatar
Hugh of Borg Hugh of Borg is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2004
Location: Switzerland
Posts: 568 Hugh of Borg User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hugh of Borg User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hugh of Borg User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hugh of Borg User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hugh of Borg User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hugh of Borg User rank is Second Lieutenant (5000 - 10000 Reputation Level)Hugh of Borg User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 6 Days 19 h 4 m 40 sec
Reputation Power: 82
Very well. Take a look at the I/O Tutorial i liked previously. All you need to know is explained there. Read the pages about "File I/O", "Byte Streams" and most importantly "Character Streams"...

Reply With Quote
  #10  
Old May 10th, 2008, 11:01 AM
JMV290 JMV290 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 9 JMV290 User rank is Private First Class (20 - 50 Reputation Level)JMV290 User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 h 27 m 19 sec
Reputation Power: 0
Quote:
Originally Posted by Hugh of Borg
Very well. Take a look at the I/O Tutorial i liked previously. All you need to know is explained there. Read the pages about "File I/O", "Byte Streams" and most importantly "Character Streams"...
I'll do that again.

This is what I have right now, it creates the file but doesn't save the social security numbers

Code:
System.out.printf ("\nHere are all your Social Security Numbers stored in our database:\n");
System.out.printf ( "\n%8s%32s\n", "Database Index", "Social Security Numbers" );
System.out.println ( arrayStorage );

try
{
File file = new File ("Social Security Numbers.txt");

// Create file if it does not exist
boolean success = file.createNewFile ();
if (success)
{
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("Social Security Numbers.txt"))); 
// BufferedWriter out = new BufferedWriter (new FileWriter ("Social Security Numbers.txt")); 

out.write( "How do I get the Social Security Numbers here?");
out.close ();
// File did not exist and was created
}
else
{
// File already exists
}
}
catch (IOException e)
{
} 

//JFileChooser program starts here

System.exit (0);
}

}

Reply With Quote
  #11  
Old May 10th, 2008, 11:38 AM
Yawmark's Avatar
Yawmark Yawmark is offline
Feelin' Groovy
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Aug 2001
Location: WDSMIA
Posts: 7,627 Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level)Yawmark User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 1 Day 14 h 12 m 21 sec
Reputation Power: 1344
Send a message via ICQ to Yawmark Send a message via MSN to Yawmark
Quote:
catch (IOException e)
{
}

Argh. Never, ever, ever, EVER swallow exceptions. Doing so hides the very information that will tell you how to solve your problem. Print a stack trace while you're learning.

~
__________________
Yawmark
class Sig{public static void main(String...args){\u0066or(int
\u0020$:"vÌÈÊ\"¤¾Àʲ¬Æ\"v¤Î¤\"²¤¨¸¬Æ".to\u0043h\u0061rArray()
)System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
+(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}

Reply With Quote
  #12  
Old May 10th, 2008, 04:22 PM
JMV290 JMV290 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 9 JMV290 User rank is Private First Class (20 - 50 Reputation Level)JMV290 User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 h 27 m 19 sec
Reputation Power: 0
Quote:
Originally Posted by Yawmark
Argh. Never, ever, ever, EVER swallow exceptions. Doing so hides the very information that will tell you how to solve your problem. Print a stack trace while you're learning.

~
Thanks for the recommendation. I'll do that.
As for something more related to what I was asking earlier:

Is out.write appropriate for saving the social security numbers to the file or would another method be more appropriate?

Reply With Quote
  #13  
Old May 10th, 2008, 06:38 PM
Yawmark's Avatar