• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A small Java program calculate next or previous date error

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this from IO to beginners forum
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic