• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with array of Variables

 
Behrouz Hosseini
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will find something about arrays in the Java™ Tutorials.
 
Soniya Ahuja
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic