• Post Reply Bookmark Topic Watch Topic
  • New Topic

Hi. My first question.  RSS feed

 
Stephen Carter
Greenhorn
Posts: 22
Chrome Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I would have registered earlier, but everytime I have seen a google search result that inlcuded this site, the questions were all 3, 4 or 5 years old, so I thought this site was dead... sorry!

Anyway, I do have a problem that two other forums have not been able to help me with.

Starting from lines 79-95 lies my problem. While there is no error in the code, and the && operator works perfectly, I do not want the message "tomorrow" to appear if a user enters a future day a week or more later. I will include two outputs at the bottom.
Thanks.







\Here is my problem



The whole purpose of the tomorrow code was to output tomorrow is Tuesday, instead of "Today is Monday and the future day is Tuesday."
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Why are you using % 6 when there are 7 days in the week?
 
Stephen Carter
Greenhorn
Posts: 22
Chrome Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

Why are you using % 6 when there are 7 days in the week?


Because 0 = Sunday and so on.


I am at


That's as far as I have gotten.

using blocks to label blocks of code?
block1 : {
code
/code
}

if (future + today >= 6)
break block1;
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use % 6 you will never get a result above 5.
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephen Carter wrote: . . .

using blocks to label blocks of code?
block1 : {
code
/code
}
. . .
Don't like break, labelled break even less. Others might disagree with me.
 
Stephen Carter
Greenhorn
Posts: 22
Chrome Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If you use % 6 you will never get a result above 5.

I put it at 7. I am still learning. I am taking a College Java course.
I hope to take the Introduction to Android programming this summer.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To answer your original question you need to keep a record of the actual number entered for the future date and only print the 'tomorrow' bit if the number entered is less than 7.

BTW instead of the block of if statements to print the day of the week you could have an array with the names of days of the week and then you would just need to use the 'today' and/or 'future' variables as an index into the array to get the appropriate day text.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephen Carter wrote:I put it at 7. I am still learning. I am taking a College Java course.

Have you covered arrays yet? Because, if so, you could put your 'days of the week' (the names) in a string array (String[]); and since Java indexes are 0-based, it will work nicely with your '%' operator.

Another thing: think about writing a few methods to help you out; right now all your logic is crammed into main(), which isn't generally a good way to work.

Third: You are using switch statements in some places and 'if' stacks (which should probably be (if...else if...) in others. The first (lines 20-33) could certainly be changed to use a switch statement, which might be clearer - but the main point is: be consistent.

Fourth: Please DontWriteLongLines; it makes your post very difficult to read. I've broken them up (which unfortunately invalidates your line numbers; the new range is 82-102).

Winston
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!