这是我第一次使用该网站,如果我的内容格式不正确,请原谅我。但我几乎是 Java 的初学者,我正在尝试为在线计算机科学课程创建这个程序,我需要在 Java 的 Try/Catch 中使用双打,我不知道如何去做。
我知道我需要在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 数字长度或类似的东西,但这只是我所做工作的一个例子。
我很感激你们可以提供任何帮助来帮助我解决问题。