简体   繁体   中英

How do I output only part of an array with JOptionPane?

I'm having difficulty outputting only part of an array to a JOptionPane.showOptionDialog() . I have a user prompt asking for an integer between 1-10 and would like to output that number of items from an array.

options = JOptionPane.showInputDialog("How many options would you like?"+
                        "\n\nPlease note that you must input a number between 1-10");

try{
    numberofoptions = Integer.parseInt(options);
}
catch(NumberFormatException e){
    JOptionPane.showMessageDialog(null, "Remember to only input a numerical integer");
}

JOptionPane.showOptionDialog(null, "Please select a restaurant.", "Restaurant Selector",
    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, Restaurants, Restaurants[0]);

exit = JOptionPane.showConfirmDialog(null, "Do you wish to continue?", "Restaurant Selector", 
        JOptionPane.YES_NO_OPTION);

You could use the Arrays.copyOf method to get the first n restaurants:

import java.util.Arrays;
import javax.swing.*;

public class ArraySubset {
    public static void main(String[] arguments) {
        SwingUtilities.invokeLater(() -> new ArraySubset().createAndShowGui());
    }

    private void createAndShowGui() {
        JFrame frame = new JFrame("Stack Overflow: part of an array");
        frame.setBounds(100, 100, 800, 600);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        JButton button = new JButton("Let's pick a restaurant");
        button.addActionListener(actionEvent -> pickRestaurant());
        panel.add(button);
        frame.getContentPane().add(panel);

        frame.setVisible(true);
    }

    private void pickRestaurant() {
        String[] allRestaurants = {
                "Restaurant 1", "Restaurant 2", "Restaurant 3", "Restaurant 4", 
                "Restaurant 5", "Restaurant 6", "Restaurant 7", "Restaurant 8", 
                "Restaurant 9", "Restaurant 10"
        };

        int numberOfOptions = allRestaurants.length;

        try {
            String messageNumberOfOptions = "How many options would you like?" +
                                            "\n\nPlease note that you must input " +
                                            "a number between 1-10";

            String options = JOptionPane.showInputDialog(messageNumberOfOptions);

            numberOfOptions = Integer.parseInt(options);
        }
        catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, 
                                          "Remember to only input a numerical integer");
        }

        String[] someRestaurants = Arrays.copyOf(allRestaurants, numberOfOptions);

        JOptionPane.showOptionDialog(null, "Please select a restaurant.", 
                                     "Restaurant Selector",
                                     JOptionPane.YES_NO_OPTION, 
                                     JOptionPane.QUESTION_MESSAGE, 
                                     null,
                                     someRestaurants, someRestaurants[0]);

        int exit = JOptionPane.showConfirmDialog(null, "Do you wish to continue?",
                                                 "Restaurant Selector",
                                                 JOptionPane.YES_NO_OPTION);

        System.out.println("exit: " + exit);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM