This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A small Java program calculate next or previous date error

 
Mathew Brown
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends;

I am a newbie in Java and took some classes to learn it. I try to write a small program requesting user input a date (integer) next or previous date (String). Then it calculates the next or the previous date according to "next" or "previous" entered. I need to use a if .. else loop for this. I wrote something like this:



When I rum this program it skips the comparison block:

if ( ( date < firstDay ) && (date > lastDay) ) {

System.out.println("Enter a date between 1-30");
System.exit(-2);
}

So for example if I enter a number like 50 or 0 it won't generate an error message which I am expecting.

In addition after I enter a number it asks me to enter "next" or "previous" . It stops execution if I don't enter "next" or "previous" which is good. But in case if I enter next or previous it will always say there is no previous date or there is no next date but won't ever calculate finalDate.

I'm suspecting there is some kind of mistake in my logical comparisons but couldn't really figure out what it is. Any comment will be highly appreciated.

Thanks

 
Tony Docherty
Bartender
Posts: 2958
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this from IO to beginners forum
 
Junilu Lacar
Bartender
Posts: 7466
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no such thing as an if-else loop : if-else statements are a one-pass thing, they only get executed one time. If you want repeat something, you'll need to use a for-loop, a while-loop, or do-while loop.
 
Mathew Brown
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:There is no such thing as an if-else loop : if-else statements are a one-pass thing, they only get executed one time. If you want repeat something, you'll need to use a for-loop, a while-loop, or do-while loop.


I agree it's easier to do so. But this is a "if then" chapter assignment so I need to find a way to do it just using those.
 
Tony Docherty
Bartender
Posts: 2958
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a linear problem, why do you need to loop at all?

As to your problem:

How can 'date' ever be less than firstDay and greater than lastDay given they are 1 and 30 respectively?
BTW why are they of type Integer and not int?
 
Junilu Lacar
Bartender
Posts: 7466
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mathew Brown wrote:
... in case if I enter next or previous it will always say there is no previous date or there is no next date but won't ever calculate finalDate.

I'm suspecting there is some kind of mistake in my logical comparisons but couldn't really figure out what it is. Any comment will be highly appreciated.


your suspicion is correct. read the lines of code I quoted again and understand what it's doing, then you'll understand your mistake.
 
Mathew Brown
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh! I messed with || && . Actually I thought || and && or

Sorry guys.

Thanks for your comments. After changing logical operators, my program now is working fine.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic