简体   繁体   English

将数组传递给方法和错误

[英]Passing Arrays to Methods and errors

So I have a java pogram that is suppose to make labels. 所以我有一个Java pogram可以做标签。 I wanted to use a method but I'm not sure what is wrong but I'm pretty sure it has to do with my method. 我想使用一种方法,但是我不确定什么地方出错了,但是我很确定它与我的方法有关。

the errors I am curently getting are 我现在正在得到的错误是
Error: cannot find symbol and it is in reference to the 7 arrays 错误:找不到符号,它引用了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): ");

    }
}  

nameAddressArray is declared locally to the main method. nameAddressArray在main方法的本地声明。 You can make it a class-level variable or pass it as a parameter to getLabelData. 您可以将其设为类级别的变量,也可以将其作为参数传递给getLabelData。

Change getLabelData to declare, instanitate, and return the array. 更改getLabelData以声明,实例化并return数组。 Then save the reference in main . 然后将引用保存在main In main that might look like, main会像她那样,

String nameAddressArray[] = getLabelData();

while getLabelData might look like 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;
}

You could also build up your prompts with a loop. 您还可以通过循环建立提示。 Something like 就像是

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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