# Thread: need help with calculating something

1. Member
Join Date
Sep 2009
Posts
6
Rep Power
0

## need help with calculating something

ok, so im real new to this stuff and i think i got this right but when i go to run it, it gives me errors.

Java Code:
```//LibraryFineCalculator.java
// Program for calculating the fines for overdue items returned past their due date
// Displays the book name and the computed fines for each day past due.

import java.util.Scanner;
import java.util.Locale;
import java.text.NumberFormat;

public class Calculator {
public static void main(String args[]) throws Exception {

// Create a way to read input from the user
Scanner input = new Scanner( System.in );

//Print out the name of the program
System.out.println( "***************" );
System.out.println( "Welcome to the Library Fine Calculator" );
System.out.println( "***************" );

//Read user input from the console
System.out.print("Please enter the name of the item being returned: ");
String book_name = input.nextLine();

// Ask user for the days past due
System.out.print("Enter the number of days past due: ");
double past_due = input.nextDouble();

// Ask user for the daily fine for the book each day
System.out.print("Enter the amount of daily fine's: ");
//read number of hours worked this week
double daily_fine = input.nextDouble();

double total_due = past_due * daily_fine;

//Print report
System.out.println("book name: ");
System.out.println("total due: " + NumberFormat.getCurrencyInstance(Locale.US).format(daily_fine);
}
}```

Program output :

C:\\> java library calculator
***************
Welcome to the Library Fine Calculator
***************

book name: Need Help with Java
past due: 12
daily fine: .25
total due: \$3.00

C:\\> java library calculator
***************
Welcome to the Library Fine Calculator
***************

book name: Titanic
past due: 15
daily fine: .30
total due: 4.50

if someone could just help explain what im doing wrong that would be awesome.
Last edited by Fubarable; 09-30-2009 at 12:11 AM. Reason: Code tags added to aid in readability

2. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
14
Please use code tags if you have to post code.
What are the errors? What was your expected output?
Did you want to print the daily_fine at the end or the total_due?

3. Member
Join Date
Sep 2009
Posts
6
Rep Power
0
not sure how to post code tags sorry...

but what im trying to do is when you run this in command prompt. it will look like this

Welcome to the library fine calculator
please enter the name of the item being returned
Need help with java
Numbers of days past due:
12
Enter amount of daily fines
.25
Total amount due:
3.00

4. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
14
Could you highlight the difference between that and your program output then.

5. Originally Posted by mikec420
not sure how to post code tags sorry...
I've added tags to your first post. Please edit that post to see what I've done (or look at my signature below or the forum faq's). They're not hard to use and will increase the chance that someone will take the time to read your code and help you.

Luck.

6. Member
Join Date
Sep 2009
Posts
6
Rep Power
0
thxs for adding the code tags. im just lost with this situation. i know how to System.out.println "blah blah blah"

but i dont know how to get it to add numbers and calculate them. i need to figure out that i took a book out and its 25 cents per day past due. the book was returned 12 days past the due date so i need it to calculate and read . "Your book is 12 days past due, you owe \$3.00.

if there is a site that can explain it better to me i would really really appreciate it.

7. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
19
Common advise at this point is "how would you do this by hand"? I mean you say the fine should be \$3.00 - how did you calculate that? Is there some line of your code that does the same calculation?

Now, about the output: which line of code is supposed to output the total amount of fine that is due? Finally in my list of questions I'll repeat one from the first response: Did you want to print the daily_fine at the end or the total_due?

8. Member
Join Date
Sep 2009
Posts
6
Rep Power
0
i would multiply days past due by how much is due. and then i want to display total due at the end. but i do not know how to add in the parts of multiplying the daily fines with days past due and then it says total amount due.

but i want it to actually display both

this is what i want the output to display

Welcome to the library fine calculator
Please Enter the name of the book you are returning
Need Help With Java
Number of days past due:
12
"Enter amount of daily fines:
.25
Total amount due:
3.00

9. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
14
We are going nowhere. You don't seem to be able to read any of the responses posted so far.

10. Member
Join Date
Sep 2009
Posts
6
Rep Power
0
i need to find out how the code multiplys fine due for each day late and total days late.

starting from scratch.

Java Code:
```import java.util.*;
class LibraryFineCalculator{

public static void main(String ss[])
{
String nameOfTheItem=Java;
int numberOfDays=12;
float fine=.25;
int dueDays=0(?? not sure if this is right);
scanner sc=new Scanner(System.in);

System.out.println("Enter Item");
nameOfTheItem=sc.next();

System.out.println("Enter days due"); (i think this is right, not sure)
numberOfDays=sc.nextInt();

if(numberOfDays>12)
{
dueDays=numberOfDays-12;
}
fine=duedays*.25; //Fine per day .25

System.out.println("Checking in the item" +nameOfTheItem+" which was "+dueDays+" days overdue. The Total fine is \$"+fine);
}

//end method

//end LibraryFineCalculater```

i think this is right with the tutorials ive been reading but i keep getting an error of

LibraryFineCalculater.java:22: cannot find symbol
symbol : variable duedays
location: class LibraryFineCalculater
fine=duedays*.25; //fine per day .25
^
1 error

i am so lost and i hope this answers ur questions.

11. Member
Join Date
Sep 2009
Posts
6
Rep Power
0
Hey i got it. i figured it out to run...yayyy.. but now im trying to fine tune it

Java Code:
```//LibraryFineCalculator
// Week 2 Checkpoint 2.1
import java.util.*;
public class LibraryFineCalculator{
// main method begins java execution
public static void main(String[] args)
{
String nameOfTheItem;  //Enter in book you checked out
int numberOfDays;
double fine=.25;
int dueDays=12;
Scanner sc=new Scanner(System.in);

System.out.println("Welcome to the library fine calculator.");
//System.out.println()
System.out.println("Please enter the item you are returning.");

nameOfTheItem=sc.next();

System.out.println("Please enter the number of days past due:");
numberOfDays=sc.nextInt();

if(numberOfDays>12)
{
dueDays=numberOfDays-12;
}
fine *=dueDays; //Fine per day \$.25

System.out.println("You have checked in the item " +nameOfTheItem+" The item was "+dueDays+" days overdue. The total fine is \$"+fine);
}
}```

so i finally got it to run, except one thing. when i enter in a book with one word it works. when i try to enter in a book like, "how to program" i get an error. the error is

at java.util.Scanner.throwFor(scanner.java:840>
at java.util.Scanner.next<Scanner.java:1461>
at java.util.Scanner.nextInt<Scanner.java:2091
at java.util.Scanner.nextInt<Scanner.java:2050>
at LibraryFineCalculator.main<LibraryFineCalculator.j ava:22>

so i can only use one word, but how do i fix it to use more then one word.

also how do i seperate lines?

12. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
19
Java Code:
`nameOfTheItem=sc.next();`

You need to be sure you understand what this is doing. And why when you later call nextInt() you get the exception.

The first place to look for enlightenment is the Scanner API documentation. Have a look at what it says about next() and nextInt(). When it talks about "tokens" it means something like "words".

You can also have a look for a method that does more what you are looking for: ie return the next Line spaces and all so you can use that as the name Of The Item.

13. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
19
also how do i seperate lines?
Remember there is a \n character that you can include as part of a string. So a string like "The quick brown fox jumped\nover\nthe lazy dog" would include a couple of newlines:

Java Code:
```The quick brown fox jumped
over
the lazy dog```
(and there are fancier ways with %n and friends - but it is enough to use \n whereever you would like a new line to be typed.)

14. ## Re: need help with calculating something

Hijack post deleted and old thread has been locked.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•