java - 数组故障和 '.class' 预期错误

标签 java arrays expected-exception

Java。

所以基本上我在“createWarehouses”数组中的返回方面遇到了问题。如果我返回w[],我会得到一个.class预期错误,或者如果我返回w,我会得到一个不匹配错误:

"array required but warehouse found"

对于所有调用createWarehouses的公共(public)变量。任何帮助将不胜感激。如果您愿意,代码也在这里:

http://pastebin.com/sAeFZu7M

import javax.swing.JOptionPane;

public class WarehouseTest
{
    public static void main(String args[])
    {
        String dataArray[][] = {{ "2200", "10", "5", "5", "200", "First Street",     "Dallas", "Texas", "76000" },
                    { "5550", "12.75", "2", "3", "15", "Mitchell Drive",     "Arlington", "Texas", "76019" },
                    { "12000", "17.5", "6", "7", "0", "Jones Blvd",     "Burleson", "Texas", "76120" },
                    { "7235", "22.35", "54", "80", "30", "Smith Circle",     "Keller", "Texas", "75020" }};

        Warehouse wArray[] = new Warehouse[4];
        wArray = createWarehouses( dataArray ); //15

        JOptionPane.showMessageDialog( null, purchaseItems( wArray ));
        JOptionPane.showMessageDialog( null, printWarehouses( wArray ));
        JOptionPane.showMessageDialog( null, printWarehouseCharge( wArray ));
    }

    public static Warehouse[] createWarehouses( String dataArray[][] )
    {
        Warehouse w[] = new Warehouse[dataArray.length];

        for( int i = 0; i < w.length; i++ )
        {
            w[i] = new Warehouse( Double.parseDouble(dataArray[i][0]),        
                                  Double.parseDouble(dataArray[i][1]),
                                  Integer.parseInt(dataArray[i][2]),
                                  Integer.parseInt(dataArray[i][3]),
                                  Integer.parseInt(dataArray[i][4]),
                                  new Address( dataArray[i][5],
                                          dataArray[i][6], dataArray[i][7],     
                                          Integer.parseInt( dataArray[i][8] )));
        }   
        return w[]; // ****<--- THIS IS PROBLEM**strong text**
    }

    public static String printWarehouses( Warehouse w[] )
    {
        String printMsg = String.format("");        

        for( int i = 1; i < w.length; i++ )
        {
            printMsg += String.format( 
            "Square Foot Size %.2f Price Per Square Foot %s Televisions %d     Computers %d Tablets %d %s", 
            Double.parseDouble( w[i][0] ), Double.parseDouble( w[i][1] ), 
            Int.parseInt( w[i][2] ), Int.parseInt( w[i][3] ), Int.parseInt( w[i]    [4] ),
            w[i][5].toString );      
        }               
        return printMsg;
    }

    public static String printWarehouseCharge( Warehouse w[] )
    {
        String chgMsg = String.format("");      

        for( int i = 1; i < w.length; i++ )
        {
            chgMsg += String.format( "$%,.2f\n", w[i].calculateWarehouseCharge()     + w[i].calculateTax() );
        }
        return chgMsg;
    }

    public static String purchaseItems( Warehouse w[] )
    {
        String p[][] = 
                {
                    {"Dallas", "1", "2", "5"}, 
                    {"Arlington", "0", "0", "15"},
                    {"Burleson", "0", "0", "3"},
                    {"Keller", "10", "25", "0"},
                    {"Dallas", "5", "0", "0"},
                    {"Arlington", "0", "1", "0"},
                    {"Burleson", "2", "4", "0"},
                    {"Keller", "0", "30", "3"}
                };

        String msg = String.format("");

        for ( int i = 0; i < w.length; i++ )
        {
            if ( w[i].getAddress().getCity().equals( p[i][0] ))
            {
                msg += String.format("%,.2f\n", w[i].purchaseTelevision(     599, Integer.parseInt( p[i][1] ))
                    + w[i].purchaseComputer( 759, w[i].Integer.parseInt(     p[i][2] ))
                    + w[i].purchaseTablet( 239.99,     w[i].Integer.parseInt( p[i][3] )));
            }   
            return msg;
        }
    }
} 

最佳答案

发生的情况是您犯了错误。您的 createWarehouses() 方法应返回一个数组。当您尝试返回 w[] 时,w[] 属于 Warehouse 类型,这就是您收到错误消息 "array 的原因需要,但已找到仓库”

当您将 w[] 更改为 w(这是正确的返回类型)时,您可能会认为收到了相同的错误消息,但实际上,消息来自下面的另一个方法,即 purchasedItems() 方法。在该方法中,您尝试从循环内部返回。这也是一个编译错误。 请参阅底部的我的代码进行修复。

在您的 purchasedItem() 中,return 应位于循环外部。

您还应该确保在 createWarehouses()返回 w

使用 purchasedItems(),您应该将整个字符串与 \n 连接起来,而不是尝试多次返回

public static String purchaseItems( Warehouse w[] )
{
    String p[][] = 
            {
                {"Dallas", "1", "2", "5"}, 
                {"Arlington", "0", "0", "15"},
                {"Burleson", "0", "0", "3"},
                {"Keller", "10", "25", "0"},
                {"Dallas", "5", "0", "0"},
                {"Arlington", "0", "1", "0"},
                {"Burleson", "2", "4", "0"},
                {"Keller", "0", "30", "3"}
            };

    String msg = String.format("");

    for ( int i = 0; i < w.length; i++ )
    {
        if ( w[i].getAddress().getCity().equals( p[i][0] ))
        {
            msg += String.format("%,.2f\n", w[i].purchaseTelevision(     599, Integer.parseInt( p[i][1] ))
                + w[i].purchaseComputer( 759, w[i].Integer.parseInt(     p[i][2] ))
                + w[i].purchaseTablet( 239.99,     w[i].Integer.parseInt( p[i][3] )));
        }   
        msg += "\n";
    }
    return msg;
}

编辑:Mayees

也许在您的 printWarehouse() 方法中,您想要这样的东西,因为 w[][] 无法访问

// this is what you have
Double.parseDouble( w[i][0] )

// try this
wArray[i].getSquareFoot();  // or whatever method you use from Warehouse to 
                            // get the square foot from your warehouse class

createWarehouse() 方法之外的任何地方,您尝试访问 w[i].something 时,请将其更改为 wArray[i].somethingwArray[] 是全局变量。 w[] 只能在 createWarehouse() 方法中本地访问。请记住这一点。

关于java - 数组故障和 '.class' 预期错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19961602/

相关文章:

c# - 具有 ExpectedException 的 XUnit 和 MSTest 返回不同的结果

java - Spring DAO可以合并到Service层吗?

java - Linux 更新后 JVM 中的错误文件编码

java - 我在减少图像闪烁时遇到问题

c++将二维数组作为参数传递给函数?

javascript - 解析 json 对象以比较其中的项目

java - Oracle 日志充满 "java.lang.Throwable: No Error"?

将给定单词与数组中的字符串进行比较

C 宏未按预期工作