• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Error in program

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey! The program is suppose to print the fine for the library materials based on the amount of days its late. There is a fine on how late the material is but the max fine is $5.00. People older than the age of 70 have a max fine of $0.00 while people under the age of 18 and older than the age of 5 have a max fine of $1.00 That means everyone else must have a fine of $5.00 or less. However whenever I input the age between 18 to 69 the fine is always 0.00. Please explain what I am doing wrong. Thanks

 
Bartender
Posts: 10983
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

18 is >= 6
 
Greenhorn
Posts: 8
1
Netscape Windows Vista Notepad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey there, what might be causing it:

1. Some of your fines are commented out

2. The code "int fine =  item * overdue" sets the fine only at initialisation so it is always initialised as 0. It will not be used as a pattern. Did you program in C before? I think I saw such patterns in C.

3. You are casting double values to int by using (int). If you wanted to use it to make the use of the Scanner or value comparison easier you could use Cents instead. You can still output it to $ later.

4. The order of operations in your if clauses may be different from what you expect it to be.
For instance: age >= 6 || age < 18 && fine > 5
Could be computed as: (age >= 6) || (age < 18 && fine >5) // I think that is the order in Java, but I'm not sure
Or might as well be:  (age >= 6 || age < 18) && (fine > 5)
Better use brackets to force the order of operations and make it more clear.

5. Your IF- ELSE statements are a bit complicated and could be optimised. That would make it easier to find possible mistakes. If you want you could try to draw a diagram to check your logic.
NSD for instance: https://en.wikipedia.org/wiki/Nassi%E2%80%93Shneiderman_diagram
You can use pen and paper or http://structorizer.fisch.lu/
 
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In Java, "&&" binds more tightly than "||", so this is effectively:

I suspect you want:
 
Carey Brown
Bartender
Posts: 10983
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:I suspect you want:


I suspect it should be
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my updated code but still seem to have the problem

 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that It doesn't print out the fine statements. please Help. This is the update code..

 
Carey Brown
Bartender
Posts: 10983
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junaid Mahmud wrote:The problem is that It doesn't print out the fine statements. please Help.


What inputs are you giving it and what output are you getting?
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what do you mean exactly as inputs and outputs?
 
Mat Falk
Greenhorn
Posts: 8
1
Netscape Windows Vista Notepad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at your if statements.. there are statements that are never true:




There might be more, haven't seen them yet.
I'm trying to check your code in IntelliJ now, will be easier to spot possible errors.

I have a question:
The declaration "int fine = item * overdue;" in line 12 has no effect in Java, right? It looks like it only sets fine to 0 and fine stays at 0 until after line 76, right? I'm not sure how Scanner works, that's why I'm asking. As I said I think I have seen declaraton patterns in C where the assignment would work, if you put it in the precompiler or something.. but I don't think this works in Java. If fine stays at 0 then that means that the if-statements that asks for the fine variable always looks at 0 fine.
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay I will change it up and post the update. It should take no more than 5 minutes.
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I looked over and am a bit confused now? What would I change the value of item to? It is currently zero.
 
Knute Snortum
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mat: Please don't post complete solutions in Beginning Java.  We're here to provide help and guidance.  Short pieces of code for illustration purposes are fine.
 
Mat Falk
Greenhorn
Posts: 8
1
Netscape Windows Vista Notepad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:Mat: Please don't post complete solutions in Beginning Java.  We're here to provide help and guidance.  Short pieces of code for illustration purposes are fine.



Woops, sorry.. just picked it from the "Most recent" and didn't know about this rule yet.
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am still having problems
 
Knute Snortum
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you be specific about the problems you're having?  For instance, these inputs seems to produce the correct output:
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the updated code. The only problem I am having now is that it prints out the fine for citizens greater than 70 but the fine should always be 0. Please help.


 
Bartender
Posts: 732
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

the above statement is always true whatever the value of age.
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have figured that out, but now the only probably that seems to appear is that It will not print out the fine adjustment reason.


 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This line ...

Junaid Mahmud wrote:



can *never* execute, as the condition can never be true.

Henry
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still don't understand why its not printing out! This is what I have now.



 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay I have figured it out the ABSOLUTELY last mistake I have is that if the fine is greater or equal to 5 the fine adjustment reason should be "exceeds max value" However I keep ending up with "Other" Thanks!


 
Knute Snortum
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The && operator binds more tightly than ||.  This means that the above code effectively is this:

Is that what you wanted?
 
reply
    Bookmark Topic Watch Topic
  • New Topic