Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Oct 2012
    Posts
    28
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Help with selecting options in java

    Hi im new to the java language.

    import javax.swing.JOptionPane;

    public class TravelAgentSystem {

    public static void main(String[] args) {
    // Local variable declared
    int selectValue;

    // Main Menu is displayed

    System.out.println("| Flight Travel Systems |");
    System.out.println("|--------------------------|");
    System.out.println("| Options: |");
    System.out.println("| 1. Time |");
    System.out.println("| 2. Price |");
    System.out.println("| 3. Hotels |");
    System.out.println("| 4. End |");
    System.out.println("============================") ;

    selectValue();

    }
    public static void selectValue()
    {

    int choice = -1;
    while(choice < 0)
    {
    String input = JOptionPane.showInputDialog("Please select an option: ");
    }

    }
    }

    I want to make it so when i select an option such as 1, it will take me to a new option where i can display more infomation.
    Anyone know how i can go about doing this?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    That's wierd; you're combining gui and cli for io?
    The input within the selectValue method will contain the value entered into the option dialog. Cast that to an integer with Integer.parseInt and assign it to the choice. Then switch on the choice to determine what to do next.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    nervousprogramm (03-01-2013)

  • #3
    New Coder
    Join Date
    Oct 2012
    Posts
    28
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    That's wierd; you're combining gui and cli for io?
    The input within the selectValue method will contain the value entered into the option dialog. Cast that to an integer with Integer.parseInt and assign it to the choice. Then switch on the choice to determine what to do next.
    At the moment im just sticking to basics so would using scanner be more easier?

    also this is what ive done is it correct??

    import javax.swing.JOptionPane;

    public class TravelAgentSystem {

    public static void main(String[] args) {
    // Local variable declared
    int selectValue;

    // Main Menu is displayed

    System.out.println("| Flight Travel Systems |");
    System.out.println("|--------------------------|");
    System.out.println("| Options: |");
    System.out.println("| 1. Time |");
    System.out.println("| 2. Price |");
    System.out.println("| 3. Hotels |");
    System.out.println("| 4. End |");
    System.out.println("============================");

    selectValue();

    }
    public static void selectValue()
    {

    int choice = -1;
    while(choice < 0)
    {
    String input = JOptionPane.showInputDialog("Please select an option: ");
    if (input.equals("1")){
    System.out.println("fdsfds");
    }
    else if (input.equals("2")){
    System.out.println("ggfdgd " );
    }
    else if (input.equals("3")){
    System.out.println("hgfdhgf " );
    }
    else if (input.equals("4")){
    System.out.println("nhgdhd " );
    }
    else {
    JOptionPane.showMessageDialog(null, "User error,please select an option");

    }

    }
    }
    }



    i need to change the system to options but at the moment the concept seems baffling. If i select 1 i want to bring up an array of flights in which can be selected

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Yeah more or less.
    I'd use the scanner and keep the components separate. If you want to use JOptionPane, I'd use that in conjunction with a frame instead of with the cli.
    The while loop won't end though, so you'll need to do something with the choice variable. Or change it to something like a do/while with a boolean control and simply default it to not loop and simply swap it to loop if entry is no good.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    nervousprogramm (03-01-2013)

  • #5
    New Coder
    Join Date
    Oct 2012
    Posts
    28
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Yeah more or less.
    I'd use the scanner and keep the components separate. If you want to use JOptionPane, I'd use that in conjunction with a frame instead of with the cli.
    The while loop won't end though, so you'll need to do something with the choice variable. Or change it to something like a do/while with a boolean control and simply default it to not loop and simply swap it to loop if entry is no good.
    Kk im going to convert it to CLI and then when i finish i will convert to GUI to get a better understanding. Thanks for the help, i need a nice break lol.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •