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
readingsvariable refer to an array that is able to hold sixtydoublevalues - make the
urlsvariable refer to an array that is able to hold ninetyStringobjects - make the
machinesvariable refer to an array that is able to hold fiveTicketMachineobjects
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];
Once you declare the variables, you don’t need to mention their type again on future assignments.
Thus, if you do:
then you’ve redeclared the type of
i, which is an error. Instead, just do:Or even better, you can combine the two into one statement:
Since the variables in your particular example have already been declared as a particular type, then you can just do: