2015-10-16 53 views
0

我試圖用JOptionPane運行我的程序,它不輸出錯誤窗口。我的程序必須從文件中讀取乘客,他們都有名字,姓氏,航班等級,護照代碼和通關統計。如果乘客有黃色,橙色或紅色清除警告,應輸出JOptionPane對話框。消息框的標題應爲「安全清除警報」。JOptionPane的小問題

- 如果ClearanceException爲黃色,則顯示「繼續謹慎操作」。 - 如果ClearanceException爲橙色,則顯示「可能的威脅已識別。」。 - 如果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."); 
    } 
}` 

記事本文件

`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

在最窄的範圍內聲明變量是個好習慣 - 在你的情況下,在循環內而不是外部。你根本不需要默認值'黃色'。 – MirMasej

回答

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的比賽枚舉值正是工作。