File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Problem with array of Variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with array of Variables" Watch "Problem with array of Variables" New topic

Problem with array of Variables

Behrouz Hosseini
Ranch Hand

Joined: Feb 15, 2010
Posts: 31
I have a question for you guys!
Is it possible we can make an array of variables?
I mean if you look at my code I tried to create an array of String which was supposed to be prompted after getting the user input.
as you can see I had to assigned all my doubles(day of week) into input.nextDouble().in this way I can get the user input correctly but the System.out.println("Your Hours for " + days[i] +":"); just print out the first line and scanner starts to tracking the inputs.

Your Hours for Tuesday:
You have worked 0.0 for Friday

now my question is how i can prompt with System.out.println("Your Hours for " + days[i] +":") after each input and how I can reduce the amount of
monday = input.nextDouble();
tuesday = input.nextDouble();
wednesday = input.nextDouble();
thursday = input.nextDouble();
friday = input.nextDouble();
saturday = input.nextDouble();
sunday = input.nextDouble();

Best regards
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46320
You will find something about arrays in the Java™ Tutorials.
Soniya Ahuja
Ranch Hand

Joined: Jul 20, 2008
Posts: 83
Hi Behrouz,

Just like an array of Strings you can create an array of double

ex double [] hoursOnDays = new double[7];

I have specified the size as 7 for seven days of week.

Now you can simply do something like

hoursOnDays[i] = input.nextDouble();
Just replace all the lines with this one line - the output should be as expected - One line will be printed followed by the console waiting for user input. This will run just 6 times because your for loop starts from 0 and ends when i< 6. If you want it to run 7 times, the condition should be i<7

SCJP 1.5 | SCWCD 5 | SCJP 6.0
[url][/url] - Online training for Java/JSPs and Servlets/SCJP/SCWCD
I agree. Here's the link:
subject: Problem with array of Variables
It's not a secret anymore!