如何将String []转换为int []?

时间:2014-06-21 09:28:30

标签: java

这可能是一个显而易见的问题,我不知道,但我无法在任何地方找到任何答案。 所以,我有一个JTextField,它正在获取一些文本并将其放入一个字符串[]但我希望它变成一个int []。这是我的一些代码,但当然我有错误,我需要它转换为int []。关于我如何做到这一点的任何想法?

此外我遇到问题的代码是在按钮actionevent中,只是让你轻松找到它:)谢谢!

private void initComponents() {
            // JFormDesigner - Component initialization - DO NOT MODIFY
            // //GEN-BEGIN:initComponents
            // Generated using JFormDesigner Evaluation license - Will Baker
            textField9 = new JTextField();
            textField10 = new JTextField();
            label10 = new JLabel();
            label9 = new JLabel();
            button5 = new JButton();

            // ======== this ========
            Container contentPane = getContentPane();
            contentPane.setLayout(null);
            contentPane.add(textField9);
            textField9.setBounds(90, 15, 273,
                    textField9.getPreferredSize().height);
            contentPane.add(textField10);
            textField10.setBounds(90, 40, 273,
                    textField10.getPreferredSize().height);

            // ---- label10 ----
            label10.setText("Loot Name(s):");
            contentPane.add(label10);
            label10.setBounds(new Rectangle(new Point(20, 40), label10
                    .getPreferredSize()));

            // ---- label9 ----
            label9.setText("NPC Name(s):");
            contentPane.add(label9);
            label9.setBounds(20, 10, 68, 25);

            // ---- button5 ----
            button5.setText("Start!");
            button5.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent arg0) {
                    NPC_NAME = textField9.getText().split(",");
                    LOOT_NAMES = textField10.getText().split(",");

                    running = true;
                    gui.dispose();
                }
            });
            contentPane.add(button5);
            button5.setBounds(90, 70, 208, button5.getPreferredSize().height);

            { // compute preferred size
                Dimension preferredSize = new Dimension();
                for (int i = 0; i < contentPane.getComponentCount(); i++) {
                    Rectangle bounds = contentPane.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width,
                            preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height,
                            preferredSize.height);
                }
                Insets insets = contentPane.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                contentPane.setMinimumSize(preferredSize);
                contentPane.setPreferredSize(preferredSize);
            }
            pack();
            setLocationRelativeTo(getOwner());
            // JFormDesigner - End of component initialization
            // //GEN-END:initComponents
        }

        // JFormDesigner - Variables declaration - DO NOT MODIFY
        // //GEN-BEGIN:variables
        // Generated using JFormDesigner Evaluation license - Will Baker
        private JTextField textField9;
        private JTextField textField10;
        private JLabel label10;
        private JLabel label9;
        private JButton button5;
        // JFormDesigner - End of variables declaration //GEN-END:variables
    }

2 个答案:

答案 0 :(得分:1)

您必须自己实施。看起来应该是这样的:

public int[] stringsToInts(String[] strings){
    int[] ints = new int[strings.length];
    for (int i = 0; i < strings.lengths; i++){
        ints[i] = Integer.parseInt(strings[i]);
    }
    return ints;
}

答案 1 :(得分:0)

没有神奇的内置功能。你需要自己实现一个。

只需创建int []并遍历string []并在每次迭代中解析字符串并将其保存到新的数组匹配索引中,完成后返回新的int []数组。