0

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;
        }
    }
} 
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
abd9344
  • 71
  • 1
  • 1
  • 7
  • Please remember to tag your questions with the language you're using. – Barmar Nov 13 '13 at 18:32
  • Where do you get that error? – Daniel Kaplan Nov 13 '13 at 18:35
  • I took your code and there were syntax errors everywhere but where your arrow was. Scratch the code and start writing it again, compiling one method at a time. – Karol S Nov 13 '13 at 18:51
  • I have compiled one method at a time, I don't get any syntax errors except when calling other methods (obviously because they aren't there). The only consistent syntax error is in the one described in the description. – abd9344 Nov 13 '13 at 19:25
  • Why the hell one does not simply attach stacktrace? – Antoniossss Nov 13 '13 at 21:08
  • I'm in a beginner Java course, I don't know what stacktrace is. Do you have a link that might enlighten me? I would appreciate any help. – abd9344 Nov 13 '13 at 21:12
  • 1
    In your `purchasedItem()`, the `return` should go on the outside of the loop. – Paul Samsotha Nov 13 '13 at 21:16
  • Can you also put the implentation of Wharehouse here in the question? They are to closely related to left out... – Salandur Nov 13 '13 at 21:17
  • You should `return w` and compile again and see what happens. That's the only thing I see wrong with code, given the error message. – Paul Samsotha Nov 13 '13 at 21:25
  • yes, I will update with the other classes when I get home, I should have included them as well. – abd9344 Nov 13 '13 at 22:25

1 Answers1

1

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.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Thanks for this, I will give it a try as soon as I get home. – abd9344 Nov 13 '13 at 22:27
  • Okay, I tried that, but I still have the exact same issue, when I use 'return w;' it gives me a "array required but warehouse found" error in all the strings that call w[]. Whereas if I use 'return w[];' I get the same '.class. expected error. – abd9344 Nov 14 '13 at 06:54
  • Give me an example of which line you're talking about for this error "array required but warehouse found" error in all the strings that call w[]." – Paul Samsotha Nov 14 '13 at 07:06
  • 1. w[] is only accessible from within the `createWarehouse()` method. 2. You're trying to access a 2D `w[i][3]` when a 2D `w[][]` doesn't exist. – Paul Samsotha Nov 14 '13 at 07:11