IllegalArgumentException在Java中无法正常运行
所以抛出非法异常对我来说是新事物,我希望能从中得到一些启示。我正在编写一个代码,该代码将确定事件中产生或损失的金额,但是,在测试时,我在代码中遇到了两个错误。第一个是,无论我输入什么,我都会收到IllegalArgumentException在Java中无法正常运行,java,illegalargumentexception,Java,Illegalargumentexception,所以抛出非法异常对我来说是新事物,我希望能从中得到一些启示。我正在编写一个代码,该代码将确定事件中产生或损失的金额,但是,在测试时,我在代码中遇到了两个错误。第一个是,无论我输入什么,我都会收到IllegalArgumentException错误消息“线程“main”中的异常java.lang.IllegalArgumentException:选择的字母无效。必须是T、D或E。给定的数据T将被忽略。”我不太清楚为什么,因为即使是留言也说我使用了正确的字母 我遇到的第二个问题是,我的程序在显示方法
IllegalArgumentException
错误消息“线程“main”中的异常java.lang.IllegalArgumentException
:选择的字母无效。必须是T、D或E。给定的数据T将被忽略。”我不太清楚为什么,因为即使是留言也说我使用了正确的字母
我遇到的第二个问题是,我的程序在显示方法中没有实际显示值(除了0)。我觉得我的想法是正确的,所以我想也许我在别的地方做得不对。如果需要的话,我确实附加了整个代码,但是我在感觉错误的地方留下了一条注释
public class EdmondEventManager2 {
/**Purpose of code is to determine the amount of money generated or lost in an event
* @param args the command line arguments
*/
public static void main(String[] args) {
EdmondEventClass object= new EdmondEventClass();
object.addNewValue(amountType(),validateAmount());
object.displayResults();
}
/*static method that will read and validate amount type T-ticket sales, D-Donation, E-expenses */
public static char amountType (){
Scanner keyboard= new Scanner (System.in);
System.out.println("Please select the amount Type: T-Ticket Sales,"
+ " D-Donations, E-Expenses");
char amtType=keyboard.next().charAt(0);
amtType=Character.toUpperCase(amtType);
switch(amtType)
{
case 'T' &'t':
break;
case 'D'& 'd' :
break;
case 'E' &'e' :
break;
}
if (amtType !='T'&& amtType!='D'&& amtType!='E'&&amtType!='t'
&&amtType!='d'&&amtType!='e'){
do {
System.out.println("Choice invalid. Please select T, D, or E");
System.out.println("Enter amount type: T, D, or E.");
amtType=keyboard.next().charAt(0);
}while (amtType!='T'&&amtType!='D'&&amtType!='E');
return amtType= Character.toUpperCase(amtType);
}
return amtType= Character.toUpperCase(amtType);
}
/*static method that will read and validate the amount. I believe te first issue is here */
public static double validateAmount (){
Scanner keyboard= new Scanner (System.in);
System.out.println("Please enter the amount in dollars");
double amount=keyboard.nextInt();
if (amount<=0){
do {
System.out.println("Amount must be a positive number!"
+ " Try Again");
System.out.println("Please enter the amount in dollars");
amount=keyboard.nextInt();
}while (amount<=0);
return amount;
}
return amount;
}
public static class EdmondEventClass{
private int T;
private int D;
private int E;
//constructors
public EdmondEventClass (){
T=0;
D=0;
E=0;
}
//getters
public int getTotalIncomeSale () {
return this.T;
}
public int getTotalDonatins () {
return this.D;
}
public int getTotalExpenses (){
return this.E;
}
/*Instance method that will add a new value to one of the totals
*@char amtType- One of the letters the user must chose. T-ticket sales,
D-Donation, E-expenses
*@ double amount- the amount for the chosen amtType.
*/
public double addNewValue ( char amtType, double amount) {
if (amount<=0) {
throw new IllegalArgumentException("Amount must be positive and "
+ "non-zero. The given data " +amount+ " will be ignored.");
}
else if (amtType !=T&& amtType!=D&& amtType!=E ) {
throw new IllegalArgumentException ("The letter chosen is invalid."
+ "Must be T, D, or E. The given data " +amtType+
" will be ignored");
}
return amount + amtType;
}
//Will display the outcome of the event. I believe the second issue is here.
public double displayResults (){
System.out.println ("Event Overall Outcome:");
System.out.println ("Total ticket sales: "+T);
System.out.println ("Total donations: "+D+ " +");
System.out.println (" -------");
System.out.println ("Total income: ");
System.out.println ("Total expense: "+E+ " -");
System.out.println (" -------");
System.out.println ("Event profits: ");
String numberString=String.format ("%8.2f");
return T+D;
}
}
}
公共类EdmondEventManager 2{
/**代码的目的是确定事件中产生或损失的金额
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
EdmondEventClass对象=新建EdmondEventClass();
addNewValue(amountType(),validateAmount());
displayResults();
}
/*静态方法,用于读取和验证金额类型的T-票销售、D-捐赠、电子费用*/
公共静态字符数量类型(){
扫描仪键盘=新扫描仪(System.in);
System.out.println(“请选择金额类型:T-Ticket Sales,”
+“D-捐赠、电子费用”);
char amtType=keyboard.next().charAt(0);
amtType=Character.toUpperCase(amtType);
开关(AMT类型)
{
案例“T”和“T”:
打破
案例“D”和“D”:
打破
案例“E”和“E”:
打破
}
如果(amtType!='T'&&amtType!='D'&&amtType!='E'&&amtType!='T'
&&amtType!='d'&&amtType!='e'){
做{
System.out.println(“选择无效,请选择T、D或E”);
System.out.println(“输入金额类型:T、D或E”);
amtType=keyboard.next().charAt(0);
}while(amtType!='T'&&amtType!='D'&&amtType!='E');
返回amtType=Character.toUpperCase(amtType);
}
返回amtType=Character.toUpperCase(amtType);
}
/*静态方法,将读取和验证金额。我相信第一个问题在这里*/
公共静态双validateAmount(){
扫描仪键盘=新扫描仪(System.in);
System.out.println(“请输入美元金额”);
double amount=keyboard.nextInt();
如果(amount您的参数是一个char,但您似乎在对照int检查它,因此方法addNewValue
没有意义。您应该将chars与chars进行比较,而int仅与int进行比较
附带建议:请理解代码格式设置非常重要,不是可选的,也不是应该仔细做的事情。如果您希望在这里得到最好的帮助,请努力使您的代码尽可能易于阅读和理解。此外,您还需要学习并遵循Java命名约定,包括使用小写字母开始变量名。您的参数是一个字符,但您似乎是在对照int检查它,因此方法addNewValue
毫无意义。您应该将字符与字符进行比较,将int与int进行比较
附带建议:请理解代码格式设置非常重要,不是可选的,也不是应该仔细做的事情。如果您希望在这里得到最好的帮助,请努力使您的代码尽可能易于阅读和理解。此外,您还需要学习并遵循Java命名约定,包括使用小写字母启动变量名。您能显示实际错误吗?@pczeus下面是我在尝试使用IllegalArgumentException时收到的错误。很抱歉,我不得不打断注释,注释在线程“main”中保持格式奇怪的异常java.lang.IllegalArgumentException:选择的字母无效。必须是T、D或E。给定的数据T将在EdmondeventManager.EdmondeventManager.EdmondeventManager$EdmondEventClass.addNewValue(EdmondeventManager.java:121)在EdmondeventManager.EdmondeventManager.main(EdmondeventManager.java:24)处被忽略C:\Users\My PC Duh\AppData\Local\NetBeans\Cache\8.1\executor snippets\run.xml:53:Java返回:1生成失败(总时间:4秒)绝对糟糕的代码和格式。难怪你不知道自己在做什么。如果你对自己的工作不感到自豪,结果显然反映了你的努力。请花时间格式化并清理你的混乱。你能显示实际错误吗?@pczeus下面是我在尝试使用IllegalArgumentException时收到的错误。S对不起,我不得不打断注释,注释一直在格式化,线程“main”java.lang.IllegalArgumentException中出现异常:选择的字母无效。必须是T、D或E。给定的数据T将在EdmondeventManager.EdmondeventManager$EdmondEventClass.addNewValue处被忽略(EdmondeventManager.java:121)位于EdmondeVentManager.EdmondeVentManager.main(EdmondeVentManager.java:24)C:\Users\My PC Duh\AppData\Local\NetBeans\Cache\8.1\executor snippets\run.xml:53:java返回:1次生成失败(总时间:4秒)绝对糟糕的代码和格式。难怪你不知道自己在做什么。如果你对自己的工作不感到自豪,结果显然反映了你的努力。请花时间格式化并清理你的混乱。非常感谢。我最初尝试过将参数设为int,但我还是很后悔