這是我第一次使用該網站,所以原諒我,如果我沒有格式正確的東西。但是我幾乎是一個初學Java的人,並且正在嘗試爲在線計算機科學課程創建這個程序,並且我需要在Java中的Try/Catch中使用雙打,我不知道如何去做。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][email protected]([\\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號碼長度之類的東西,但是這僅僅是什麼,我沒有得到工作的例子。
我很感謝你們幫助我解決問題的任何幫助。
你的錯誤信息不匹配,你發佈的代碼。清理並重新編譯所有內容,然後查看是否仍然出現錯誤。更好的是,使用一個IDE,它會爲你處理它。 – chrylis