Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi i want to write a program that asks the user for various bits of data about an employee - for example their name, job title, age and salary. I want the program to validate that the age and salary are of the correct data types (i.e. integer and double respectively) and once all data has been validated write it to a file. This is what I have done so far, it prompts the user for input and the try and catch block works and it create a file but it doesn't write to the file. Please help.
Java
import java.io.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.InputMismatchException;

public class EmployeeFiles {

    public static void main (String[] args)  {
        Scanner input = new Scanner(System.in);
        String employeeName = null;
        String jobTitle = null;
        double salary = 0;
        int age = 0;


        try{
                FileWriter fw = new FileWriter("employeFile.txt");
                PrintWriter pw = new PrintWriter(fw);
                pw.println(employeeName);
                pw.print(age);
                pw.print(salary);
                pw.print(jobTitle);
                pw.flush();
                pw.close();

            }
            catch(IOException e){
                System.out.println("ERROR!");
            }try{

            System.out.println("Please enter name: " );
            employeeName = input.next();
            System.out.println("Please enter salary:");
            salary= input.nextDouble();
            System.out.println("Please enter age: " );
            age = input.nextInt();
            System.out.println("Please enter job title: ");
            jobTitle = input.next();

            input.close();

        }catch(InputMismatchException e) {
            System.out.println("ERROR!! - Invald data type.");
        }


    }
}
Posted
Updated 4-Dec-15 4:25am
v2
Comments
Sergey Alexandrovich Kryukov 4-Dec-15 10:40am    
Help with what? You are trying to write data before getting it from a user. How can it possibly be helped?
—SA

Try and think about this in logical steps.
1. Get the information from the user.
2. Validate each input to make sure it is an acceptable value.
Note: you may prefer to validate each item as they are input, rather than getting the inputs and then validating them all together. This will depend on the value to check and whether they have inter-dependencies.
3. If any validation fails, go back to 1 and start again.
4. If validation succeeds, write the values to the output file.
5. If more values required, go to 1 for next set of values.
6. If no more inputs, close all files and terminate.
 
Share this answer
 
Your code is working fine.Let me explain what is happening in your code.

first you have initialized the employee name = null , his salary and age =0 and made this to print in a .txt file.

So the ".txt" file contains
Java
null
0
0

and you have asked the user to enter the data which is not useful.
so you have to remove the initialization.(for your reference code given below)
Java
String employeeName;
       String jobTitle;
       double salary ;
       int age;

and cut the data which ask the user to input his details and paste it above here
Java
 System.out.println("Please enter name: " );
        employeeName = input.next();
        System.out.println("Please enter salary:");
        salary= input.nextDouble();
        System.out.println("Please enter age: " );
        age = input.nextInt();
        System.out.println("Please enter job title: ");
        jobTitle = input.next();

        input.close();
// above here
        try{
                FileWriter fw = new FileWriter("employeFile.txt");
                PrintWriter pw = new PrintWriter(fw);
                pw.println(employeeName);
                pw.print(age);
                pw.print(salary);
                pw.print(jobTitle);
                pw.flush();
                pw.close();
 
            }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900