Java中出现Try/Catch异常的问题

Java中出现Try/Catch异常的问题,java,compiler-errors,Java,Compiler Errors,这是我第一次使用这个网站,所以如果我没有正确格式化,请原谅我。但我基本上是一个Java初学者,我正在尝试为一个计算机科学在线课程创建这个程序,我需要在Java的Try/Catch中使用Double,我不知道如何去做 我知道我需要在某个地方使用Math.pow(),但讲师没有明确说明如何使用,我无法联系他们 下面是我得到的错误代码,也许这能更好地解释我试图做的事情: CollectInfo.java:22: error: method collectWage in class Validate c

这是我第一次使用这个网站,所以如果我没有正确格式化,请原谅我。但我基本上是一个Java初学者,我正在尝试为一个计算机科学在线课程创建这个程序,我需要在Java的Try/Catch中使用Double,我不知道如何去做

我知道我需要在某个地方使用
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
import java.util.*;
导入java.util.regex.*;
公共类集合信息{
公共静态void main(字符串[]args){
int id=Validate.collectin(“请输入项目的id”);
String fname=Validate.collectString(3,“请输入名字”);
String lname=Validate.collectString(5,“请输入姓氏”);
String email=Validate.collectEmail(“请输入您的电子邮件地址”);
String phone=Validate.collectPhone(“请输入您的电话号码\n”
+“如(123)456-7890或1234567890或123-456-7890”);
字符串zipcode=Validate.collectZip(“请输入zipcode”);
字符串ssn=Validate.collectSsn(“请输入您的ssn”);
双倍工资=验证。收取工资(“请输入您的小时工资”);
System.out.print(String.format)(“id:%s\n名称:%s%s\n邮件:%s\n”
+“电话:%s\n IP代码:%s\n SSN:%s\n年龄:%s\n\n”,
id、fname、lname、电子邮件、电话、zipcode、ssn、工资);
}//结束主方法
}//结束类CollectInfo
类验证{
公共静态整型集合整型(字符串messageIn){
扫描仪输入=新扫描仪(System.in);
int intOut=0;
布尔有效=真;
System.out.println(messageIn);
while(有效){
试一试{
intOut=input.nextInt();
有效=错误;
}捕获(输入不匹配异常e){
input.nextLine();
System.out.println(“您必须输入一个整数!”);
}//端盖
}//结束时
返回输入;
}//结束collectInt
公共静态字符串集合字符串(int strLen,String messageIn){
扫描仪输入=新扫描仪(System.in);
字符串outsr=“”;
布尔有效=真;
System.out.println(messageIn);
while(有效){
试一试{
outStr=input.nextLine();
如果(超出长度()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