• 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

While loop error

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I am kind of new here so i'd like to introduce myself first.
I am Michael living in the Netherlands and just started a study computer science.
I also started the study with programming language Java.

I try to create a program just for learning purposes but the program get's stuck in a loop and i can't figure out why.
What the program suppose to do is the following,
When you input the month, day and year (all integer types) he need to output the input typed.
It also should know if the month have 30 days or 31 days and leapyear is also included.

What he is doing at the moment is when i input a date it will loop in reenter a valid input.
Also my Intellij IDE tells me that if ((yearInt < 1000) || (yearInt > 999)) is always true and i think there is the problem but still can't figure out why

Here is my code.
DateOk class:


Main class:



Thanks!

Kind regards,
Michael.
 
Marshal
Posts: 71070
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

What does line 14 mean?
 
Marcus Koning
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replying, it should prevent the user that makes the input can't enter a value less than 999 and should be greater than 1000 and already changed it to this : (yearInt < 999) || (yearInt > 1000)  there was my first mistake thanks!
 
Campbell Ritchie
Marshal
Posts: 71070
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a pleasure

You can shorten your switch but remember that fall‑through is unusual and needs to be recorded. I think the following form will work, but not certain.If you look closely at those cases, you will find another error.
It is better style to use ?: instead of line 25:-Same error not corrected.
 
Marcus Koning
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again for reply,

I don't really understand the last you said.

Could you please explain the meaning of this sentence?
return
BTW, when i put in in my intellij gives me a error that it can not resolve leapYear,

And my program is still in infinite loop
 
lowercase baba
Posts: 12933
65
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is called the trinary operator.  it's basically this:

 
Marcus Koning
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Fred, thanks for replying.

So if i understand it correctly, the ? stands for the if and the : stands for the else?

 
fred rosenberger
lowercase baba
Posts: 12933
65
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are stuck in an infinite loop, one of the best tools you can use is the System.out.println() method.  sprinkle them throughout your code, printing various variable values.  

Better, be more targetted with them...If you are stuck in an infinite loop, then your tryAgain is never getting updated.  

So that means your date.dateOK method is not returning true.  So put statements in that method.  Print what gets passed in.  Print something like 'case 1', 'case 2' in each possible case.  test EVERY POSSIBLE case, and see if it is working.

Better would be to write one case at a time.  comment out everything but case 1 and your default.  Have your default print "falling through to default for now..."

the less code you write/test at a time, the easier it is to be sure you test all of it.
 
fred rosenberger
lowercase baba
Posts: 12933
65
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Marcus Koning wrote:Hi Fred, thanks for replying.

So if i understand it correctly, the ? stands for the if and the : stands for the else?


basically yes
 
Marcus Koning
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for replying,

Just struggling the hole afternoon, still can't figure it out.
Infinite loop is gone, but now you can enter any input you want

here's my new piece of code:

DateOK class:


Main class:
 
fred rosenberger
lowercase baba
Posts: 12933
65
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this line confuses me:



so if I enter 200, it returns true since it is less than 999.

If I enter 2000, it returns true, since it is > 1000.

you stated above:

it should prevent the user that makes the input can't enter a value less than 999 and should be greater than 1000


This sounds redundant.  "can't' enter less than 999" is the same as "entering 1000 or more". Then you state "greater than 1000".  

What are you REALLY trying to do here?  If you want something greater (or equal to?) than 1000, just do that...don't over-complicate things.
 
Marcus Koning
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your totally right, already changed the 1000 to 2050. But still not working.
 
Saloon Keeper
Posts: 2738
133
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

fred rosenberger wrote:That is called the trinary operator.  it's basically this ...


I think you meant ternary operator (@OP if you are interested, here's the documentation)
 
salvin francis
Saloon Keeper
Posts: 2738
133
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP, a few points from my end..

Your class names should be nouns, so instead of DateOk, you can call it something like DateVerifier or DateChecker or similar ?
Next, your method name should be verbs, so instead of "dateOK", you can call it "verify" or "check" or even "isCorrect" or similar.
I suggest making your methods static and removing all field variables from your class.

 
fred rosenberger
lowercase baba
Posts: 12933
65
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"not working" doesn't really tell us much.  How is it not working? what are you entering? What is it doing vs. what did you expect it to do?

Don't just randomly change stuff.  The most critical thing to do right now is learn to THINK through the problem.  

WHAT EXACTLY do you want that line to do? Tell us in English.  What is allowed and what is not allowed?

It also helps if you post your actual code - but the only the relevant part to what is causing the problem.  Please cut-n-paste, since it's easy to make typos when doing it by hand.

 
fred rosenberger
lowercase baba
Posts: 12933
65
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:

fred rosenberger wrote:That is called the trinary operator.  it's basically this ...


I think you meant ternary operator (@OP if you are interested, here's the documentation)


yup.  sometimes I'm an idiot.  lol
 
If somebody says you look familiar, tell them you are in porn. Or in these tiny ads:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic