Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8224929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:14:24+00:00 2026-06-07T15:14:24+00:00

I am experimenting with variations on the dialog box tutorial on Oracle. I felt

  • 0

I am experimenting with variations on the dialog box tutorial on Oracle. I felt that the number of options I offered resulted in too many buttons so I converted from OptionDialog to InputDialog and used an Object array. Now, however, my switch statement (in replyMessage) doesn’t work because of the change in data type. How do I get the index in the Object array corresponding to the user’s selection?

Object answer;        
    int ansInt;//the user'a answer stored as an int

    Object[] options = {"Yes, please",
            "No, thanks",
            "No eggs, no ham!",
            "Something else",
            "Nothing really"
        };//end options
    answer = JOptionPane.showInputDialog(null, "Would you like some green eggs to go with that ham?",
        "A Silly Question",
        JOptionPane.QUESTION_MESSAGE,
        null,
        options,
        options[2]);

    ansInt = ;//supposed to be the index of user's seleection   

    replyMessage(ansInt);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T15:14:26+00:00Added an answer on June 7, 2026 at 3:14 pm

    showInputDialog will return the string that the user entered, which is one of the strings in the options array. The shortest way to know which index was selected is to loop on the options array and find equality:

    for(i = 0; i < options.length; i++) {
      if(options[i].equals(answer) {
        ansInt = i;
        break;
      }
    }
    

    Another possibility is to have a map of key/values:

    Map<String, Integer> optionsMap = new HashMap<String, Integer>();
    options = optionsMap.keySet().toArray();
    ... Call the dialog ...
    ansInt = optionsMap.get(answer);
    

    The first option is good if the number of options is low. If you have many options, the second solution will perform a lot better. For even better performance, you can cache theoptionsMap and options array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When experimenting with (embedded) Apache Derby DB, I noticed that a fresh database, with
Just experimenting and learning, and I know how to create a shared dictionary that
In experimenting with this question I created an example that I utterly do not
Experimenting with jQuery and trying to make a small slide show that rotates through
After experimenting with many IMAP API's, I have decided to write my own (Most
While experimenting with this question on collections in Spring.NET , I discovered that Spring
When experimenting with Cassandra I've observed that Cassandra writes to the following files: /.../cassandra/commitlog/CommitLog-<id>.log
While experimenting with ternary and null coalesce operators in C# I discovered that it
Experimenting with Asynchronous Mail 0.2.1 using Oracle as backing database. Running small test app
I experimenting with jQuery. As I was trying I found out that I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.