-1

Hey guys i'm a newby programmer, and i'm getting a .class in return from this, but not sure what its asking. Any help would be awesome thank you!

public static Letter[] addLetter(Letter[] array, Scanner kb)


{
      Letter[] temp = new Letter[array.length + 1];
      String toName, toAddress, toCity, toState, fromName, fromAddress, fromCity, fromState;
      int toZip, fromZip;
      double weight;

  for (int x = 0; x < array.length; x++){
     temp[x] = array[x];

     System.out.print("New Letter:\nTo Name: ");
     kb.nextLine();
     System.out.print("To Street: ");
     kb.nextLine();
     System.out.print("To City: ");
     kb.nextLine();
     System.out.print("To State: ");
     kb.nextLine();
     System.out.print("To Zip: ");
     kb.nextInt();
     kb.nextLine();
     System.out.print("From Name: ");
     kb.nextLine();
     System.out.print("From Street: ");
     kb.nextLine();
     System.out.print("From City: ");
     kb.nextLine();
     System.out.print("From State: ");
     kb.nextLine();
     System.out.print("From Zip: ");
     kb.nextInt();
     kb.nextLine();
     System.out.print("Letter weight: ");
     kb.nextDouble();
     kb.nextLine();

     Letter temp = new Letter(toName, toAddress, toCity, toState, toZip, fromName, fromAddress, fromCity, fromState, fromZip, weight);

     int x = array.length - 1;
     temp[x] = temp;
  }
  return temp[];

when i try to return temp [] it gives me an error of . Not too sure on what's going on to make it do that.

class' expected
      return temp[];
Bob
  • 43
  • 1
  • 1
  • 7

3 Answers3

1

Errors in code: You have a

Letter temp = new Letter ...

Rename it.

Also change

return temp[];

to

return temp;

Errors in design: use ArrayList<Letter> instead of array. It has add method. http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html

Rustem Mustafin
  • 957
  • 1
  • 11
  • 23
  • @Bob please dont miss my edit about http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html Also mark as 'accepted answer' if all is ok now. – Rustem Mustafin Nov 23 '13 at 20:44
0

You don't need square brackets when referring to a variable. You need it only when declaring the variable type, and while setting/getting the value at a particular index.

So instead of:

return temp[];

Use:

return temp;

Also, there's one other error I see from your code:

Letter temp = new Letter(toName, toAddress, toCity, toState, toZip, fromName, fromAddress, fromCity, fromState, fromZip, weight);

int x = array.length - 1;
temp[x] = temp;

The variable temp is being used as both an array and as an individual variable. Choose a different name.

UltraInstinct
  • 43,308
  • 12
  • 81
  • 104
0

Correct code

public class Letter {

    public static Letter[] addLetter(Letter[] array, Scanner kb) {
        Letter[] temp = new Letter[array.length + 1];

        // ....

        Letter tempItem = new Letter(toName, toAddress, toCity, toState, toZip, fromName, fromAddress, fromCity, fromState, fromZip, weight);
        int x = array.length - 1;
        temp[x] = tempItem;

        return temp;
    }
}
MariuszS
  • 30,646
  • 12
  • 114
  • 155