I need help with my constructor that should call for all the user inputs, for example their first name, and the information gets checked to make sure it's valid. For example the name is all letters, and the identifier is actually 9 digits, etc. I can set the default constructor, but then the input from the user doesn't get checked. There's that issue and then the issue with writing to the file. It throws one of my exceptions saying error writing to file and that is from the last line I wrote with writeObject().

 public static void addRecords()
   {

      try
      {
           Customer record = new Customer() ;

           System.out.println( "Please enter customer number (must be greater than 1000):" );
           int custNbr = scanner.nextInt();
           scanner.nextLine();

           System.out.println( "Please enter tax identifier (must be 9 digits):" );
           String taxID = scanner.nextLine();

           System.out.println( "Please enter first name:" );
           String firstName = scanner.nextLine();

           System.out.println( "Please enter last name:" );
             String lastName = scanner.nextLine();

           System.out.println( "Please enter code for title: " );

           System.out.println( "1 = Mr., 2 = Ms., 3 = Mrs., 4 = Miss, 5 = Dr., 99 = none");
           int custHonor = scanner.nextInt();
           scanner.nextLine();


    newFile.writeObject(record);
      }

Recommended Answers

All 4 Replies

You can validate the user input before creating the object. Store the inputs in variables, validate them as they come in (so you can warn the user and have him/her retry). Then when you have all the info you can create the Customer object and pass all the input as parameters in a constructor or separately through setters.

As far as the last line goes, not really sure what error you mean, the code is incomplete. It doesn't tell what type newFile is, is it an ObjectOutputStream? Did you initialize the variable newFile? And if so, is Customer implementing Serializable?

From the documentation:

 FileOutputStream fos = new FileOutputStream("t.tmp");
 ObjectOutputStream oos = new ObjectOutputStream(fos);

 oos.writeInt(12345);
 oos.writeObject("Today");
 oos.writeObject(new Date());

 oos.close();

ObjectOutputStream
Serializable

Sorry, yes it does implement serializable and that is where I validate the input and throw exceptions. And not it just throws my own exception saying it can't write to the file.

Do you validate the input in setters themselves? Or in separate validation methods? If so you could do something along the lines of:

try{
    record.setName(name);
}catch(YourException e){
    //input not valid
}

for each input item, but that would not work if you want the user to be able to re-enter the information that was invalid without starting over. You could use a do/while loop on each item to keep asking for user input until it's valid though. Something like:

boolean firstNameValid = false;
do{
    System.out.println( "Please enter first name:" );
    String firstName = scanner.nextLine();
    try{
        record.setName(firstName); 
        firstNameValid = true; 
    }catch(YourException e){
        //inform user of error
    }
}while(!firstNameValid);

This is assuming your error is thrown by the setter. Since validation is there for a reason (i.e. users often give incorrect information) it might be debatable whether or not errors/exceptions are the right way to go. You could for instance have the setters return true/false when the setting has succeeded/failed, and work with that in your do/while loop.

Can't really help you with the writing error if there's no code about it. Your custom exception probably caught the original exception that was thrown and gave you that non-informative message (the original ones are there for a reason too, they tell you why and where it went wrong).

I have my validation and exceptions thrown in the constructor that calls the set methods. Also I am just using the default constructor, but I need to use the one that passed the 5 arguments, which are essentially the user input.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.