Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with array of Variables" Watch "Problem with array of Variables" New topic
Author

Problem with array of Variables

Behrouz Hosseini
Ranch Hand

Joined: Feb 15, 2010
Posts: 31
Hi
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:
20
15
17
12
0
20
18
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
Sheriff

Joined: Oct 13, 2005
Posts: 38064
    
  22
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]http://a2zjava.webs.com[/url] - Online training for Java/JSPs and Servlets/SCJP/SCWCD
http://soniyaahuja.webs.com
 
Consider Paul's rocket mass heater.
 
subject: Problem with array of Variables
 
Similar Threads
I do not know
Array stuff again...
null as key in HashMap ?
Calling a method from another class
Add days, but not weekend days