将数组传递给方法和错误

时间:2015-11-29 21:04:13

标签: java arrays methods

所以我有一个java pogram,假设要制作标签。我想使用一种方法,但我不确定有什么问题,但我很确定它与我的方法有关。

我真正得到的错误是
错误:找不到符号,它引用了7个数组

import javax.swing.JOptionPane;

public class MailOrderpractice {

    public static void main(String[] args) {

        // declare variables

        String nameAddressArray[] = new String[7];
        String numBoxesInput;
        int numBoxes;
        String enterAnother = "Y";
        int counter;

        getLabelData();

        numBoxesInput = JOptionPane
                .showInputDialog("Enter number of boxes in the order:");
        numBoxes = Integer.parseInt(numBoxesInput);

        // begin outer loop logic that determines when user is finished entering
        // mail orders
        while (enterAnother.equalsIgnoreCase("Y")) {
            counter = 1;
            // begin the inner loop to display a label and increment the counter
            while (counter <= numBoxes) {
                System.out.println(nameAddressArray[0] + " "
                        + nameAddressArray[1] + " " + nameAddressArray[2]);
                System.out.println(nameAddressArray[3]);
                System.out.println(nameAddressArray[4] + ", "
                        + nameAddressArray[5] + " " + nameAddressArray[6]);
                System.out.println("Box " + counter + " of " + numBoxes);
                System.out.println();
                counter = counter + 1;
            }

            enterAnother = " "; // initialize the variable to something other
                                // than "Y" before sending the prompt
            enterAnother = JOptionPane
                    .showInputDialog("Do you want to produce more labels? Y or N");

            while (!enterAnother.equalsIgnoreCase("Y")
                    && !enterAnother.equalsIgnoreCase("N")) {

                enterAnother = JOptionPane.showInputDialog(null,
                        "Invalid Response. Please enter Y or N.",
                        "DATA ENTRY ERROR", JOptionPane.ERROR_MESSAGE);
            } // end while

            if (enterAnother.equalsIgnoreCase("Y")) {

                getLabelData();

                numBoxesInput = JOptionPane
                        .showInputDialog("Enter number of boxes in the order:");
                numBoxes = Integer.parseInt(numBoxesInput);
            } // end if
        } // end while

        System.exit(0);
    }

    public static void getLabelData() {
        nameAddressArray[0] = JOptionPane
                .showInputDialog("Enter title (Mr., Ms., Dr., etc.): ");
        nameAddressArray[1] = JOptionPane.showInputDialog("Enter first name: ");
        nameAddressArray[2] = JOptionPane.showInputDialog("Enter lastname: ");
        nameAddressArray[3] = JOptionPane
                .showInputDialog("Enter street address: ");
        nameAddressArray[4] = JOptionPane.showInputDialog("Enter city: ");
        nameAddressArray[5] = JOptionPane
                .showInputDialog("Enter state (IL, MO, etc.): ");
        nameAddressArray[6] = JOptionPane
                .showInputDialog("Enter zip (e.g., 62025): ");

    }
}  

2 个答案:

答案 0 :(得分:0)

nameAddressArray在main方法的本地声明。您可以将其设置为类级变量,或将其作为参数传递给getLabelData。

答案 1 :(得分:0)

更改getLabelData以声明,instanitate和return数组。然后在main中保存引用。在main中可能看起来像,

String nameAddressArray[] = getLabelData();

虽然getLabelData可能看起来像

public static String[] getLabelData() {
    String[] nameAddressArray = new String[7];
    nameAddressArray[0] = JOptionPane.showInputDialog("Enter title (Mr., Ms., Dr., "
            + "etc.): ");
    nameAddressArray[1] = JOptionPane.showInputDialog("Enter first name: ");
    nameAddressArray[2] = JOptionPane.showInputDialog("Enter lastname: ");
    nameAddressArray[3] = JOptionPane.showInputDialog("Enter street address: ");
    nameAddressArray[4] = JOptionPane.showInputDialog("Enter city: ");
    nameAddressArray[5] = JOptionPane.showInputDialog("Enter state (IL, MO, etc.): ");
    nameAddressArray[6] = JOptionPane.showInputDialog("Enter zip (e.g., 62025): ");
    return nameAddressArray;
}

您还可以使用循环构建提示。像

这样的东西
static String[] prompts = { "title (Mr., Ms., Dr., etc.)", "first name", "lastname", //
        "street address", "city", "state (IL, MO, etc.)", "zip (e.g., 62025)" };
public static String[] getLabelData() {
    String[] nameAddressArray = new String[7];
    for (int i = 0; i < nameAddressArray.length; i++) {
        nameAddressArray[i] = String.format("Enter %s: ", prompts[i]);
    }
    return nameAddressArray;
}