开发者

I need help in trying to re-input a value whenever the user inputs a negative number

import java.util.*;


public class Inheritance {

    public static Scanner objScan = new Scanner(System.in);

    public static void main(String[] args) {

        System.out.println("Enter name:");
        String strName = objScan.nextLine();

        double dSalary;
        int iYears;
        String strTIN;
        String strLoopControl = "1";

        try {
    开发者_开发问答        System.out.println("Enter salary:");
            dSalary = dGetSalary();
            System.out.println("Enter years worked:");
            iYears = iGetYears();
            System.out.println("Enter ID number:");
            strTIN = strGetID();  

            Employee1 objEmp = new Employee1(strName, dSalary, iYears, strTIN);
            objEmp.print();
        }

        catch (Exception e) {           
            if (e instanceof NegativeInputException) {
                strLoopControl = "0";
                System.out.println(e.getMessage());
                //strLoopControl = "0";
            }
            else if (e instanceof ZeroIdentificationException) {
                System.out.println(e.getMessage());
            }

        }

    }

   public static double dGetSalary () throws NegativeInputException {
       double dSalary;

      do { 
       try {    
           dSalary = objScan.nextDouble();

           if (dSalary < 0) {
               throw new NegativeInputException();
           }
           return dSalary;
       }

       catch (NegativeInputException nie) {
           throw nie;
       } 
      } while (dSalary<0);
   } 

    public static int iGetYears() throws NegativeInputException {
        int iYears;

        try {
            iYears = objScan.nextInt();

            if (iYears < 0) {
                throw new NegativeInputException();
            }
            return iYears;
        }

        catch (NegativeInputException nie) {
        throw nie;
       }

    }

    public static String strGetID() throws ZeroIdentificationException {
        String strTIN; 

        try {
            strTIN = objScan.next();

            if (strTIN.equals("0000000")) {
                throw new ZeroIdentificationException();
            }
            return strTIN;
        }

        catch (ZeroIdentificationException zie) {
            throw zie;
        }
}

}

class NegativeInputException extends Exception {

    public NegativeInputException() {
        super("You have inputted a negative number.");
    }

    public NegativeInputException (String strMessage) {
        super(strMessage);
    }
}

class ZeroIdentificationException extends Exception {
    public ZeroIdentificationException() {
        super("You inputted an invalid ID number. \n Please input again.");

    }

    public ZeroIdentificationException(String strMessage) {
        super(strMessage);
    }
}

class Person1 {

    private String strName;

    public Person1() {
        strName = "";
      }

    public Person1(String strName) {
        this.strName = strName;
    }

    public void setName(String strName) {
        this.strName = strName;
    }

    public String getName() {
        return strName;
    }

    public void print() {
        System.out.println("Name: " +strName);
    }
}

class Employee1 extends Person1 {

    private String strName;
    private double dSalary;
    private int iYears; 
    private String strTIN;

    public Employee1 (String strName, double dSalary, int iYears, String strTIN) {

        this.strName = strName;
        this.dSalary = dSalary;
        this.iYears = iYears;
        this.strTIN = strTIN;

    }

    public void print() {
        System.out.println();
        System.out.print("Name: " +this.strName);
        System.out.println();
        System.out.print("Salary:" +this.dSalary);
        System.out.println();
        System.out.print("Years WorkedS:" +this.iYears);
        System.out.println();
        System.out.print("ID Number:"  +this.strTIN);
    }

        }

This is a program that asks for an employee's records. It asks for the name, salary, years worked and ID number. It catches an Exception whenever the user inputs a negative value and catches an exception whenever "0000000" is inputted in the strTIN variable. This program works flawlessly however I need to re-input whenever a negative value and "0000000" is inputted. I've tried using do-while statements but no luck. Can anyone help me? I'm having an idea of using "flag" for controlling the loop but I do not know where to put it in my code. I'm pretty sure there's a way to re-input, I just can't figure it out.


Your methods are broken - they're trying to loop while the value is negative, but they'll throw an exception if it's negative... so they'll only ever go through the loop once, either returning a value or throwing an exception.

You want something like this:

public static double getNonNegativeDouble(Scanner scanner) {
  while (true) {
    double value = scanner.nextDouble();
    if (value >= 0) {
      return value;
    }
  }
}

You should probably repeat the prompt and give an error message on invalid input though...

Also note that:

  • Catching an exception only to throw it again like this is horrible:

    // Ick: just don't catch! (May mean you don't need "try" block at all.)
    catch (NegativeInputException nie) {
        throw nie;
    } 
    
  • Your "pseudo-Hungarian" method names go against Java naming conventions

  • Catching Exception and then using instanceof to check for different types is horrible; just catch the types you're interested in.


Instead of throwing a NegativeInputException you could wrap your nextInt code in a while loop that only returns for a positive value...

In addition, maybe it's a good idea to wrap input functionality in methods like

int readInteger(int minValue, int maxValue) {
    if (minValue >= maxValue)
       throw new IllegalArgumentException("...")
    while(true) {
       System.out.println("Please enter an integer ("+minValue+"-"+maxValue+")");
       int val = objScan.nextInt();
       if (val >= minValue && val <= maxValue) return val;
    }
}
double readDouble(...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜