abd9344
abd9344

Reputation: 71

Trouble Arrays and '.class' expected error

Java.

So basically I'm having issues with the return in 'createWarehouses' array. Either I get a .class expected error if I return w[] or if I return w I get a mismatch error:

"array required but warehouse found"

for all the public variables that call createWarehouses. Any help would be greatly appreciated. If you prefer, the code is also here:

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

Upvotes: 0

Views: 165

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209102

What's happening is that you're mistaking errors. Your createWarehouses() method should return an array. When you try and return w[], w[] is of Warehouse type, that's why you get the error message "array required but warehouse found".

When you change the w[] to w, which is the correct return type, you may think you're getting the same error message, when actuality, the message is coming from below in another method, the purchasedItems() method. In that method, you're trying to return from inside of the loop. That's a compile error also. See my code at the bottom for fix.

In your purchasedItem(), the return should go on the outside of the loop.

You should also make sure to return w in the createWarehouses().

With purchasedItems(), you should concatenate the entire string with \ns instead of trying to return multiple times

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

Edit: Maybeees

Maybe in your printWarehouse() method, you want something like this instead, because w[][] is inaccessible

// 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

Anywhere outside of the createWarehouse() method you are tyring to access w[i].something, change it to wArray[i].something. wArray[] is the global variable. w[] is only accible locally within the createWarehouse() method. Remember that.

Upvotes: 1

Related Questions