我试图用JOptionPane运行我的程序,但它没有输出错误窗口。我的程序必须从文件中读取乘客,所有这些都有名字,姓氏,航班,护照代码和清关统计。如果乘客有黄色,橙色或红色间隙警告,则应输出JOptionPane对话框。消息框的标题应为“安全许可警报”。
- 如果ClearanceException为黄色,则显示“继续小心”。 - 如果ClearanceException为Orange,则显示“已识别可能的威胁”。 - 如果ClearanceException为红色,则显示“已识别威胁。请联系执法部门”。
我的问题是每个乘客都会出现一个黄色警告对话框,橙色和红色警告从不显示。继承我的代码
DRIVER / MAIN
package lab.assignment.pkg11;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.SecurityException;
import java.nio.file.Paths;
import java.util.Scanner;
import javax.swing.*;
import javax.swing.JOptionPane;
public class ClearanceException
{
private static Scanner input;
public static void main(String[] args)
{
String fName;
String lName;
String fClass;
String pCode;
String securityStatus;
code clCode = code.Yellow;
try
{
input = new Scanner(Paths.get("Passengers.dat"));
input.useDelimiter("[\r\n,]");
while (input.hasNext())
{
fName = input.next();
lName = input.next();
fClass = input.next();
pCode = input.next();
System.out.printf("%s, %s, %s,%s, %s%n", fName, lName, fClass, pCode, clCode);
switch (clCode)
{
case Yellow:
JOptionPane.showMessageDialog(null, "Proceed with caution.", "Code Yellow", JOptionPane.ERROR_MESSAGE);
break;
case Orange:
JOptionPane.showMessageDialog(null, "Possible threat identified.", "Code Orange", JOptionPane.ERROR_MESSAGE);
break;
case Red:
JOptionPane.showMessageDialog(null, "Threat identified. Contact law enforcement.", "Code Red", JOptionPane.ERROR_MESSAGE);
break;
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
enum code {Green, Yellow, Orange, Red;}
}
类
`package lab.assignment.pkg11;
public class ClearanceMain extends Exception
{
public ClearanceMain (String message)
{
super (message);
}
ClearanceMain()
{
throw new UnsupportedOperationException("Not supported yet.");
}
}`
NOTEPAD FILE
`Bryan,Buonaiuto,econo,USA,Green
Emily,Cativo,busin,USA,Yellow
Edmond,Wint,first,USA,Orange
Eric,Monforte,busin,ITA,Red
James,Kilmeade,econo,USA,Green
Alexander,Antonacci,econo,PRT,Green
Gabriella,Johnson,first,CAN,Green
Barbara,Martinez,first,COL,Orange
Enam,Safi,econo,YEM,Orange
Sean,Yakub,busin,YEM,Orange
Christina,Tarin,busin,CMR,Green
Emily,Sharma,econo,CMR,Green
Charnelle,Kupfer,econo,DEU,Green
Aaron,Gossett,econo,DEU,Green
Conrad,Golder,econo,USA,Green
Carla,Vasquez,first,USA,Green
Melinda,Osorio,first,DOM,Green
Antonio,Espinoza,econo,DOM,Green
Seth,Howell,busin,USA,Orange
Navpreet,Afzal,busin,PAK,Red
Thomas,Przywara,busin,BEL,Green
Lea,Gaang,econo,BEL,Green
Phoebe,Starks,first,USA,Green
Netel,Abdelghani-Serour,econo,PAK,Green
Ayush,Juzumas,econo,AGO,Green
Ayesha,Saagber,first,AGO,Green
Darla,Zagorski,busin,DEU,Green
Ling,Weng,first,CHIN,Yellow
Chin,Weng,first,CHIN,Yellow
Adbdul,Islam,econo,PAK,Red
Gissele,Bencosme,econo,USA,Green
Ismanel,Kefalas,busin,IND,Green
Lee,Kang,busin,KOR,Green
Graciela,Quinones,econo,SLV,Green
Jorges,Quinones,econo,SLV,Green
Lissette,Quinones,econo,SLV,Green
Nutan,Patel,first,IND,Green
Wilson,Singh,first,IND,Green`
答案 0 :(得分:2)
你忘了:
clCode = input.next();
之后:
fName = input.next();
lName = input.next();
fClass = input.next();
pCode = input.next();
您没有从文本文件中获取颜色,因此clCode
总是Yellow
在此设置:
code clCode = code.Yellow;
当然,input.next()
会返回String
,因此您必须解析它以确定它在枚举中对应的值。您可以使用Enum#valueOf()
方法执行此操作:
code clCode = code.valueOf(input.next());
只要String
s与枚举值完全匹配,这将有效。