|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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. |
|
#2
|
||||
|
||||
|
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 |
|
#3
|
|||
|
|||
|
Quote:
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. |
|
#4
|
||||
|
||||
|
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! |
|
#5
|
|||
|
|||
|
Quote:
Thanks again. |
|
#6
|
||||
|
||||
|
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? |
|
#7
|
||||
|
||||
|
Quote:
|
|
#8
|
|||
|
|||
|
Quote:
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. |
|
#9
|
||||
|
||||
|
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"...
|
|
#10
|
|||
|
|||
|
Quote:
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);
}
}
|
|
#11
|
||||
|
||||
|
Quote:
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)));}} |
|
#12
|
|||
|
|||
|
Quote:
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? |
|
#13
|
||
|
|