繁体   English   中英

带有try / catch异常的Java问题

[英]Issues in Java with Try/Catch Exceptions

这是我第一次使用该网站,如果格式不正确,请原谅我。 但是我几乎是Java的初学者,并且正在尝试为在线计算机科学课程创建此程序,并且我需要在Java的“尝试/捕获”中使用双打,而且我不确定该怎么做。

我知道我需要在某个地方使用Math.pow() ,但是讲师并未明确说明如何执行此操作,因此我无法与他们联系。

这是我得到的错误代码,也许可以更好地解释我正在尝试做的事情:

CollectInfo.java:22: error: method collectWage in class Validate cannot be applied to given types;
double wage = Validate.collectWage("Please enter your hourly wage");
^
required: double
found: String
reason: actual argument String cannot be converted to double by method invocation conversion
1 error 

这是到目前为止我可以使用的代码示例,但是我不知道如何解决该错误。

import java.util.*;
import java.util.regex.*;

public class CollectInfo {

public static void main(String[] args) {
    int id = Validate.collectInt("Please enter the ID of the Item");
    String fname = Validate.collectString(3, "Please enter the first name");
    String lname = Validate.collectString(5, "Please enter the last name");
    String email = Validate.collectEmail("Please enter your email address");
    String phone = Validate.collectPhone("Please enter your phone Number\n"
            + "Like (123) 456-7890 or 1234567890 or 123-456-7890");
    String zipcode = Validate.collectZip("Please enter the zipcode");
    String ssn = Validate.collectSsn("Please enter your SSN");
    double wage = Validate.collectWage("Please enter your hourly wage");


    System.out.print(String.format("id: %s\nName: %s %s\nEmail: %s\n"
            + "Phone: %s\nZipCode: %s\n SSN: %s\nWage:%s\n\n",
            id,fname,lname,email,phone,zipcode,ssn,wage));
} //  end main method
}  //end class CollectInfo

class Validate {

public static int collectInt(String messageIn) {
    Scanner input = new Scanner(System.in);
    int intOut = 0;
    boolean valid = true;

    System.out.println(messageIn);
    while (valid) {
        try {
            intOut = input.nextInt();
            valid = false;
        } catch (InputMismatchException e) {
            input.nextLine();
            System.out.println("You must enter a whole number!");
        } // end catch

    }   // end while
    return intOut;


} // end collectInt

public static String collectString(int strLen, String messageIn) {
    Scanner input = new Scanner(System.in);
    String outStr = "";
    boolean valid = true;
    System.out.println(messageIn);
    while (valid) {
        try {
            outStr = input.nextLine();
            if (outStr.length() < strLen) {
                throw new Exception();
            }
            valid = false;
        } catch (Exception e) {
            System.out.printf("You must have more than %s characters\n", strLen);
        } // end catch
    }// end while
    return outStr;
}

public static String collectZip(String messageIn) {
    Scanner input = new Scanner(System.in);
    String outStr = "";
    boolean valid = true;
    System.out.println(messageIn);
    while (valid) {
        try {
            outStr = input.nextLine();
            Integer.parseInt(outStr);
            if (outStr.length() != 5) {
                throw new Exception();
            }
            valid = false;
        } catch (NumberFormatException ne) {
            System.out.printf("Please enter a valid zip code\n");
        } catch (Exception e) {
            System.out.println("A zip code must be 5 characters long");
        }
    }// end while
    return outStr;
}

public static String collectEmail(String messageIn) {
    String expression = "^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
    Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);

    Scanner input = new Scanner(System.in);
    String outStr = "";
    boolean valid = true;
    System.out.println(messageIn);
    while (valid) {
        try {
            outStr = input.nextLine();
            CharSequence emailChk = outStr;
            Matcher matcher = pattern.matcher(emailChk);
            if (!matcher.matches()) {
                throw new Exception();
            }
            valid = false;
        } catch (Exception e) {
            System.out.printf("Please enter a valid email address\n");
        } // end catch
    }// end while
    return outStr;
}

//***************************************************************************

public static Double collectWage(String messageIn) {
Scanner input = new Scanner(System.in);
String strOut = "";
boolean valid = true;
double out = 0;

System.out.println(messageIn);
while (valid) {
    try {
        strOut = input.nextLine();
        if (strOut.length() != 4) {
            throw new Exception();
        }
        out = Double.parseDouble(strOut);
        valid = false;
    } catch (NumberFormatException ne) {
        System.out.println("Please enter a valid wage");
    } catch (Exception e) {

        System.out.printf("A wage should be 4 numbers long");
    } //end catch
} //end while

return out;


}//end collectWage method

//***************************************************************************

public static String collectPhone(String messageIn) {
    String expression = "^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$";
    Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);

    Scanner input = new Scanner(System.in);
    String strOut = "";
    boolean valid = true;

    System.out.println(messageIn);
    while (valid) {
        try {
            strOut = input.nextLine();
            CharSequence phoneChk = strOut;
            Matcher matcher = pattern.matcher(phoneChk);
            if (!matcher.matches()) {
                throw new Exception();
            }
            valid = false;
        } catch (Exception e) {
            System.out.printf("Please try again with a valid phone "
                    + "number\n");
        } //end catch
    } //end while

    return strOut;

}//end collectPhone method


//***************************************************************************
public static String collectSsn(String messageIn) {
    String expression = "^\\d{3}[- ]?\\d{2}[- ]?\\d{4}$";
    Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);

    Scanner input = new Scanner(System.in);
    String strOut = "";
    boolean valid = true;

    System.out.println(messageIn);
    while (valid) {
        try {
            strOut = input.nextLine();
            CharSequence ssnChk = strOut;
            Matcher matcher = pattern.matcher(ssnChk);
            if (!matcher.matches()) {
                throw new Exception();
            }
            valid = false;
        } catch (Exception e) {
            System.out.printf("Please try again with a valid social security "
                    + "number\n");
        } //end catch
    } //end while

    return strOut;

}//end collectSsn method


}  // end of class Validate

我知道我不需要3个数字长度或类似的长度,但这只是我工作的一个示例。

谢谢您能为我提供帮助。

尝试这个

public static void main(String[] args) {
    double wage = Validate.collectWage("Please enter your hourly wage");

}// end main method

public static Double collectWage(String messageIn) {
    Scanner input = new Scanner(System.in);
    String strOut = "";
    boolean valid = true;
    double out = 0;

    System.out.println(messageIn);
    while (valid) {
        try {
            strOut = input.nextLine();
            if (strOut.length() != 3) {
                throw new Exception();
            }
            out = Double.parseDouble(strOut);
            valid = false;
        } catch (NumberFormatException ne) {
            System.out.println("Please enter a valid wage");
        } catch (Exception e) {

            System.out.printf("A wage should be 3 numbers long");
        } //end catch
    } //end while

    return out;


}//end collectWage method

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM