aspose file tools*
The moose likes Beginning Java and the fly likes A small Java program calculate next or previous date error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A small Java program calculate next or previous date error" Watch "A small Java program calculate next or previous date error" New topic
Author

A small Java program calculate next or previous date error

Mathew Brown
Greenhorn

Joined: Jan 07, 2013
Posts: 7
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

Joined: Aug 07, 2007
Posts: 2290
    
  49
Moving this from IO to beginners forum
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

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.


Junilu - [How to Ask Questions] [How to Answer Questions]
Mathew Brown
Greenhorn

Joined: Jan 07, 2013
Posts: 7
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

Joined: Aug 07, 2007
Posts: 2290
    
  49
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

Joined: Feb 26, 2001
Posts: 4462
    
    6

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

Joined: Jan 07, 2013
Posts: 7
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.
 
jQuery in Action, 2nd edition
 
subject: A small Java program calculate next or previous date error