SlimJimmy
SlimJimmy

Reputation: 1

Small Issue with JOptionPane

Im trying to run my program with JOptionPane and it's not outputting the Error windows. My program must read passengers from a file, all of them have first name, last name, flight class, passport code and clearance stat. If the passenger has a yellow, orange or red clearance warning, a JOptionPane dialog box should be outputted. The title for the message box should read "Security Clearance Alert".

-If the ClearanceException is Yellow, display "Proceed with caution." -If the ClearanceException is Orange, display "Possible threat identified.". -If the ClearanceException is Red, display "Threat identified. Contact law enforcement".

My problem is that a dialog box for Yellow warning comes up for every passenger and the Orange and Red warnings never show up. Heres my code

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;}
    }

Class

`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`

Upvotes: 0

Views: 111

Answers (1)

gla3dr
gla3dr

Reputation: 2309

You forgot:

clCode = input.next();

After this:

fName = input.next();
lName = input.next();
fClass = input.next();
pCode = input.next();

You aren't getting the colour from your text file, so clCode is always Yellow from being set here:

code clCode = code.Yellow;

Of couse, input.next() returns a String, so you are going to have to parse it to figure out which value it corresponds to in your enum. You can do this using the method Enum#valueOf():

code clCode = code.valueOf(input.next());

This will work as long as the Strings match the enum values exactly.

Upvotes: 2

Related Questions