Issue With Verifying Scanner Inputs via File Reading in Java

24 Views Asked by At

Question I'm attempting to verify the scanner inputs in my main method with the contents of input.txt, which contains an id, salary, and employee status number. I'm attempting to do this by reading the text file via a BufferedReader within the read() method in my main method. In this method, I validate the inputs passed through reader and return true if they match any contents of the input.txt file. If they return false by not fitting the parameters I've set, the program should halt and send a println message. However I notice that when I call it within the if statement after the scanner inputs, the program completely skips over the if statement and the read() method. I've tried experimenting around with different ideas and structure, but nothing has been successful. No matter what incorrect variables I input into the scanner, they are all allowed to pass because my code isn't calling the read() method. Is there a way to avoid this issue?

Main Method

import java.io.*; import java.util.Scanner;

public class HW4 {

//Scanner for ID Input
public static String idScan() {
    Scanner x = new Scanner(System.in);
    String id = x.nextLine();
    return id;
}

//Scanner for Salary Input
public static double salaryScan() {
    Scanner y = new Scanner(System.in);
    double salary = y.nextDouble();
    return salary;
}

//Scanner for Status Input
public static int statusScan() {
    Scanner z = new Scanner(System.in);
    int status = z.nextInt();
    return status;
}

//Read Input.txt For Verifying Scanner Inputs
public static boolean read(String id, double sal, int stat) {
            try (BufferedReader reader = new BufferedReader(new FileReader("input.txt")))
            {   
                String line;
                while ((line = reader.readLine()) !=null) {
                    String[] data = line.split(",");
                    String validid = data[0];
                    double validSalary = Double.parseDouble(data[1]);
                    int validStatus = Integer.parseInt(data[2]);
                    
                    if (id.equals(validid) && sal == validSalary && stat == validStatus) {
                        return true;
                    } 
                }
                return false;
            }
            catch (IOException e) 
            {   
                System.out.println("No");
            }
            return false;
                }

public static void main(String args[]) {
    
    //Establishes an Array of 10 Employees
    Employee [] employee = new Employee[10];
    
    
    //For Statement that Allows Input for All 10 Employee's Constructors and Status
    for(int i = 0; i < 4; i++) {    
        System.out.println("Enter emplpoyee ID of an employee?");
        String id = idScan();
        System.out.println("Enter salary of the employee?");
        double sal = salaryScan();
        System.out.println("Is this employee a manager or worker? (Enter 1 for worker and 2 for manager)");
        int stat = statusScan();
        
        //Creates Worker and Manager Classes
        if(read(id, sal, stat)) {
            if (stat == 1) {
                employee [i] = new WorkerEmployee(0,0);
            } else if (stat == 2) {
                employee [i] = new ManagerEmployee(0,0);
            } else {
                System.out.println("No");
                i--;
            }
        }
        
        //Write output.txt file with verified scanner inputs
        try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt", true))) {
            writer.write(""+id+" earns $"+stat+" per pay period");
            writer.newLine();
            writer.flush();
            writer.close();
        }
        catch (IOException e)
        {
            System.out.println("No");
        }
    
    }
}

}

Employee.java public class Employee {

protected int employeeID;
protected double salary;

//Constructor
public Employee(int ID, double esalary) {
    employeeID = ID;
    esalary = salary;
}

//Getter ID
public int getID() {
    return employeeID;
}
//Setter ID
public void setID(int ID) {
    employeeID = ID;
}

//Getter Salary
public double getSalary() {
    return salary;
}
//Setter Salary
public void setSalary(double esalary) {
    salary = esalary;
}

}

ManagerEmployee.java public class ManagerEmployee extends Employee {

//Super Constructor
public ManagerEmployee(int ID, double esalary) {
    super(ID, esalary);
}   

public double monthlyPay(double esalary) {      
    esalary = (salary/12) + ((salary/12) * 0.3);
    return esalary;
        }

}

WorkerEmployee.java public class WorkerEmployee extends Employee {

//Super Constructor
public WorkerEmployee(int ID, double esalary) {
    super(ID, esalary);
}

public double monthlyPay(double esalary) {      
    esalary = (salary/12) - 100;
    return esalary;
        }

}

input.txt u1234567, 50000,1 u4322345, 72000,1 u5874321, 110000,2 u9876789, 250000,2 u1111111, 230000,1 u2222222, 400000,1 u3333333, 5000,2 u4444444, 740000,2 u5555555, 120000,1 u6666666, 35000,1

0

There are 0 best solutions below