尝试调用方法时出错

时间:2015-11-27 22:23:57

标签: java methods

我正在尝试创建一个方法。基于我所阅读和观看的内容,虽然这是创建然后调用方法的方法,但我不太确定我是否正确行事。当我遵守时,我收到并收到错误。如果您对如何更好地提供更好的代码有任何建议。

这是错误:

  

错误:找不到符号。

在我试图创建一个方法的公共static void getLabelData()中,它给了我大约10个这些错误的数据。

以下是代码:

import javax.swing.JOptionPane;
public class MailOrderCall
{
public static void main(String[] args)
{
    streetAddress, city, state, zip.
    String nameAddressArray[] = new String[7];
    String numBoxesInput;
    int numBoxes;
    String enterAnother = "Y";
    int counter;



    getLabelData();

    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;
        } // end while

        // ask the user if finished entering mail orders
        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");

        // validate input for enterAnother... keep them here until they enter Y, y, N, 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"))
        {
            // if the user said they have more, then send prompts to read the next mail order input from user
        getLabelData();
        } // end if
    } // end while
// successfully terminate the application
system.exit(0);
} // end main()

 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): ");

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


} // end class

我对此非常陌生,所以我很抱歉,如果我的代码完全是灾难,我只是想弄清楚它为什么会给我一个错误。

2 个答案:

答案 0 :(得分:0)

您似乎正在尝试访问getLabelData函数中不可用的变量。如果你想在多个函数中使用一个变量,你应该在整个类的范围内声明它,如下所示:

public class MailOrderCallEMH {

    String nameAddressArray[] = new String[7];
    String numBoxesInput;

    public static void main(String[] args) {
        // ...
    }

}

此外,streetAddress, city, state, zip.看起来不像有效的java语法。您应该正常声明这些变量:

// Replace Object by the type you want
Object streetAddress;
Object city;
Object state;
Object zip;

希望这有帮助!

答案 1 :(得分:0)

nameAddressArraymain方法中被声明为局部变量。如果在方法中声明变量,则只能从该方法访问它。

如果您希望可以从类中的任何方法访问nameAddressArray,请将其声明为MailOrderCallEMH的成员变量,方法是在类中声明它,但在此类方法之外

public class MailOrderCallEMH
{
    // Since this is a member of the class, you can use it from any method within this class
    static String nameAddressArray[] = new String[7];

    public static void main(String[] args)
    {
       // code
    }

    public static void getLabelData() 
    {
       nameAddressArray[0] = JOptionPane.showInputDialog("Enter     title (Mr., Ms., Dr., etc.): ");
       // and so on
    }
}

另外,请注意我如何将nameAddressArray声明为static。原因是在静态方法中访问的成员变量也必须是静态的。由于您刚开始使用Java,我建议您阅读static的内容。通常,不鼓励使用静态变量和方法。

另一种方法是将nameAddressArray作为参数传递给getLabelData

public class MailOrderCallEMH
{
    public static void main(String[] args)
    {
       String nameAddressArray[] = new String[7];
       getLabelData(nameAddressArray);
       System.out.println(nameAddressArray[0] + " " + nameAddressArray[1] + " " + nameAddressArray[2]);
    }

    public static void getLabelData(String nameAddressArray[]) 
    {
       nameAddressArray[0] = JOptionPane.showInputDialog("Enter     title (Mr., Ms., Dr., etc.): ");
       // and so on
    }
}

重要的是要注意,在此示例中,getLabelData能够更新在main内声明的变量的原因是该变量是通过引用传递的(而不是通过价值)。这是另一个重要的概念,你应该确保你熟悉。