-2

So I'm very new to java and I'm trying to write a program that will print a correct fine for overdue books. I've read multiple questions like this and most of them involve misplacement of curly brackets, but I cannot find a bracket error anywhere. I keep getting multiple errors in my program, but most of the read "illegal start of type" or "illegal start of expression" Could someone help me with my code/give me some tips on bracket placement?

Here is my code:

public class BookFine
{ 
    public static void main(String[] args)
    {
        int daysLate = 0;
        int bookCost = 0;
        int result = 0;

        System.out.print("Enter how many days your book is overdue: ");
        int daysLate = IO.readInt();
        System.out.println("Days Late = " + daysLate);

        System.out.print("How much does your book cost(enter in cents): ");
        int bookCost = IO.readInt();
        System.out.println("Book Cost = " + bookCost);

        if (daysLate=<7)
        {
            result=daysLate*10;
        }
        else 
            if(daysLate>7)
            {
                result=(daysLate-7)*20+70;
            }

        if(daysLate>90)
        {
            result= bookCost+1000;
        }

        IO.outputStringAnswer(result);
    }
}
PhilDulac
  • 1,305
  • 20
  • 32
lazepanda
  • 11
  • 4
  • 1
    Use `int` for variable declaration, afterwards just use the variable name. – Compass Jun 17 '16 at 18:48
  • Another typo: `daysLate = <7` isn't valid, I think you mean `daysLate <= 7` – azurefrog Jun 17 '16 at 18:49
  • done. this is just a pragmatic fix or does it actually have some purpose? – lazepanda Jun 17 '16 at 18:50
  • You cannot redeclare a variable that has already been declared in the same scope, that is a compiler error. – Compass Jun 17 '16 at 18:51
  • Unrelated, but you're also recalculating result when it's 90+ days late. That's fine from a technical standpoint, but separating conditions like that makes things harder to think about. – Dave Newton Jun 17 '16 at 19:10
  • This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting. –  Jun 17 '16 at 19:12
  • Compiler errors show the exact line number that is wrong. Learn to read a stack trace it has the exact line number where the problem is. Learn to use the step debugger as well. –  Jun 17 '16 at 19:13
  • @JarrodRoberson, could you explain a bit more by what you mean? The purpose of that condition is to recalculate because when a book is over 90+ late the fine is waived and the borrower must pay for the book and $10 – lazepanda Jun 17 '16 at 19:38

4 Answers4

0

There is an issue regarding declaring variable twice in the program. I have corrected the code. Please refer below code.

public class BookFine {
public static void main(String[] args)
{   int daysLate = 0;
    int bookCost = 0;
    int result = 0;

    System.out.print("Enter how many days your book is overdue: ");
    daysLate = IO.readInt();
    System.out.println("Days Late = " + daysLate);

    System.out.print("How much does your book cost(enter in cents): ");
    bookCost = IO.readInt();
    System.out.println("Book Cost = " + bookCost);

    if (daysLate<=7)
    {
        result=daysLate*10;
    }
    else 
        if(daysLate>7)
        {
            result=(daysLate-7)*20+70;
    }

    if(daysLate>90)
    {
        result= bookCost+1000;
    }


    IO.outputStringAnswer(result);
}
}
0

You appear to have no brackets for your else branch

else 
if(daysLate>7)
    {
        result=(daysLate-7)*20+70;
}

if(daysLate>90)
{
    result= bookCost+1000;
}

Should be

else 
{
    if(daysLate>7)
    {
        result=(daysLate-7)*20+70;
    }

    if(daysLate>90)
    {
        result= bookCost+1000;
    }
}
-1

There were several errors in the code I was able to identify.

first the expression:

if(daysLate=<7)

is backwards. It should read

if(daysLate<=7)

Next, at the start of the code just under main you are declaring your variables "daysLate, bookCost". Then, after your line:

System.out.print("Enter how many days your book is overdue: ");

you are redeclaring the variables as:

int DaysLate

Remove the "int" portion on both daysLate and bookCost and it should run fine assuming you have an IO class defined somewhere.

Chris
  • 9
  • 3
-1

There's a few problems here.

  1. daysLate=<7; I assume that you mean to use the <= operator. Fixing this will resolve the specific error you're asking about
  2. int daysLate = IO.readInt();; The problem with this line is that you've already declared a variable named 'daysLate'. This can be fixed in one of two ways: either remove the 'int' at the start of this line, or remove the original declaration on line 4. (I prefer the former.)
  3. IO.readInt(); there is no class named IO, at least not in terms of what's imported by your code. There are a number of different ways that you can read input from the keyboard, however. If this is your intent (and it appears like it is), you might want to look up the documentation and examples for the java.util.Scanner class. I repeat, there are more than one ways to accomplish this, even if you don't want to use Scanner, so pick your poison :)
  4. IO.outputStringAnswer(result); Same as #3, except that this time it looks like you're trying to output the result somewhere. Perhaps System.out.println() is in order here.
  5. int bookCost = IO.readInt(); Same as #2 and #3. 'bookCost' is already defined in this scope, so you don't need to declare it again (remove 'int'). And again, you will need to write working keyboard input.
  6. Finally--and this isn't an error per se--you should work on your Indent Style. It will greatly help your code readability, which will in turn help you write better code. Code that you and your colleagues enjoy reading is good (and hopefully healthy) code. Most developers these days use the 1TBF style, from my experience.

Oh, and Welcome to Java!

nasukkin
  • 2,460
  • 1
  • 12
  • 19
  • Alright thanks a lot for your help this pretty much cleared up all my errors, but I am still left with the error that reads: "class,interface, or enum expected } I removed the two brackets at the end and now my code works, but I'm not really sure why...anyway you could explain the logic behind this? – lazepanda Jun 17 '16 at 19:15
  • @DanKatebi Hmm, without seeing the updated code, it's hard to say. However, that does sound like there is still a syntax error of some sort. What IDE are you using, if any? Any modern IDE worth its weight in salt will highlight these issues for you. – nasukkin Jun 17 '16 at 19:23
  • I think I am using netbeans. Also another comment to your #3 tip: I have IO.java in the same folder so these works. – lazepanda Jun 17 '16 at 19:33