gpt4 book ai didi

java - Java 中的 Try/Catch 异常问题

转载 作者:行者123 更新时间:2023-12-02 11:04:51 25 4
gpt4 key购买 nike

这是我第一次使用该网站,如果我的内容格式不正确,请原谅我。但我几乎是 Java 的初学者,我正在尝试为在线计算机科学类(class)创建这个程序,我需要在 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

我知道我不需要 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

关于java - Java 中的 Try/Catch 异常问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19627482/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com