views:

77

answers:

3

Can you help me out with this question please.

Question: Given the following array declarations

double readings[];
String urls[];
TicketMachine[] machines;

write assignments that accomplish the following tasks:

  • make the readings variable refer to an array that is able to hold sixty double values
  • make the urls variable refer to an array that is able to hold ninety String objects
  • make the machines variable refer to an array that is able to hold five TicketMachine objects

My answer:

//declare and instantiate object

double readings [] = new double [60];

String urls [] = new String [90];

TicketMachine machines [] = new TicketMachine [5];

The error I am getting is this:

Main.java:16: readings is already defined in main(java.lang.String[])

double readings [] = new double [60];
       ^

Main.java:17: urls is already defined in main(java.lang.String[])

String urls [] = new String [90];
       ^

Main.java:18: machines is already defined in main(java.lang.String[])

TicketMachine machines [] = new TicketMachine [5];
+10  A: 

Once you declare the variables, you don't need to mention their type again on future assignments.

Thus, if you do:

int i;
int i = 5;

then you've redeclared the type of i, which is an error. Instead, just do:

int i;
i = 5;

Or even better, you can combine the two into one statement:

int i = 5;

Since the variables in your particular example have already been declared as a particular type, then you can just do:

readings = ...;
urls = ...;
machines = ...;
John Feminella
+1  A: 

Use the clues that the compiler is giving you to track down the root cause of the problem. This is an important skill in becoming a competent programmer.

Amir Afghani
Erm...sure there is. He's declaring the variables twice. Did you read the error?
Adam Robinson
Now that you've edited your "answer", perhaps you should consider making this a comment on the question. There is no answer here. "Find the answer yourself" isn't an answer, valid as a suggestion as it might be.
Adam Robinson
I disagree Adam - sometimes the best answer is look a bit harder. I'll take the downvotes.
Amir Afghani
no that is good advice, but I had spent a long time on this question and didn't really understand it very well, that's why I asked in the end
@Amir: The point of the site is to answer questions. Saying "look harder" is not an answer, it's advice. As *perfectly legitimate* as it is, it **isn't an answer**.
Adam Robinson
A: 

you've already declared those variables, so now you can just instantiate them

readings = new double[60];
urls = new String[90];
machines = new TicketMachine[5];
mportiz08