This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Modern JavaScript for the Impatient and have Cay Horstmann on-line!
See this thread for details.
Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

A small Java program calculate next or previous date error

 
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

 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this from IO to beginners forum
 
Marshal
Posts: 15870
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • 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: 3323
86
  • 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
Marshal
Posts: 15870
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • 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.
 
Every plan is a little cooler if you have a blimp. And a tiny ad.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic