Quantcast
Channel: Active questions tagged filereader - Stack Overflow
Viewing all articles
Browse latest Browse all 120

Issue With Verifying Scanner Inputs via File Reading in Java

$
0
0

QuestionI'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 Inputpublic static String idScan() {    Scanner x = new Scanner(System.in);    String id = x.nextLine();    return id;}//Scanner for Salary Inputpublic static double salaryScan() {    Scanner y = new Scanner(System.in);    double salary = y.nextDouble();    return salary;}//Scanner for Status Inputpublic static int statusScan() {    Scanner z = new Scanner(System.in);    int status = z.nextInt();    return status;}//Read Input.txt For Verifying Scanner Inputspublic 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.javapublic class Employee {

protected int employeeID;protected double salary;//Constructorpublic Employee(int ID, double esalary) {    employeeID = ID;    esalary = salary;}//Getter IDpublic int getID() {    return employeeID;}//Setter IDpublic void setID(int ID) {    employeeID = ID;}//Getter Salarypublic double getSalary() {    return salary;}//Setter Salarypublic void setSalary(double esalary) {    salary = esalary;}

}

ManagerEmployee.javapublic class ManagerEmployee extends Employee {

//Super Constructorpublic ManagerEmployee(int ID, double esalary) {    super(ID, esalary);}   public double monthlyPay(double esalary) {          esalary = (salary/12) + ((salary/12) * 0.3);    return esalary;        }

}

WorkerEmployee.javapublic class WorkerEmployee extends Employee {

//Super Constructorpublic WorkerEmployee(int ID, double esalary) {    super(ID, esalary);}public double monthlyPay(double esalary) {          esalary = (salary/12) - 100;    return esalary;        }

}

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


Viewing all articles
Browse latest Browse all 120

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>