1

I have both filenames (IDiscount and Loan) in a directory called certification.

This code works and compiles fine in eclipse but when I try and run from command line using notepad I get the error

    Loan.java:2: error: '.' expected
    import IDiscount;
                    ^
    1 error

IDiscount.java file:

    package certification;
    
    public interface IDiscount {
    
        void calculateDiscountedBillAmount(double billAmount, int discount);
    
    }
    

Loan.java file:

    package certification;
    import IDiscount;


public class Loan implements IDiscount {
    private int customerId;
    private int billId;
    private int discount;
    private double billAmount;
    private double discountedBillAmount;
    
    public Loan(int customerId, int billId, int discount, double billAmount)
    {
        this.customerId     = customerId;
        this.billId         = billId;
        this.discount       = discount;
        this.billAmount     = billAmount;
    }
            
    public void calculateDiscountedBillAmount(double billAmount, int discount)
    {
        System.out.println("Hello World");
    }

    public static void main(String[] args){
        Loan privateLoan = new Loan(101,1001,2,199.99);
        privateLoan.calculateDiscountedBillAmount(199.99,2);
    }
}

It works fine in eclipse but I am trying to get better by using command line and notepad only but the Loan file will not compile. I have narrowed the problem down to an issue with my import statement. I have tried import IDiscount.calculateDiscountedBillAmount; as well and that does not work either. Any thoughts?

andrewJames
  • 19,570
  • 8
  • 19
  • 51
  • 2
    `import IDiscount;` is a broken statement, it's not valid and also pointless. If `IDiscount` is in the same package, you don't need an `import` at all. Why Eclipse doesn't shout at you (or more likely: shouted at you, but you clicked "run anyway") is a different question. – Joachim Sauer Jul 13 '22 at 15:13
  • sounds reasonable. I still think it can't find or doesn't understand the IDiscount in someway because the IDiscount file compiles perfectly – questionsneedanswers Jul 13 '22 at 15:25
  • 2
    Of course, the `IDiscount.java` compiles, as there is nothing wrong with that file. The problem is in the other file. As Joachim Sauer said, `import IDiscount;` is invalid, as you can’t import classes from the unnamed package. Further, it would be wrong semantically as well, as `IDiscount` is in package `certification`, so a correct import statement would be `import certification.IDiscount;`, but as also said already, the class `Loan` is in the same package, hence, there is no need for an import statement. So just remove that import statement. – Holger Jul 13 '22 at 16:46
  • Holger I completely agree and I tried import certification.IDiscount; and other variations before I posted here. I don't know why it works perfectly fine in Eclipse but here it does not. I have checked claspath and everything else I can think of it just puzzles me. It also compiles fine as long as the implements IDiscount is not included which is also strange. – questionsneedanswers Jul 14 '22 at 05:04
  • Solved: https://stackoverflow.com/questions/29476985/i-cant-implement-interface-of-same-package-in-java – questionsneedanswers Jul 14 '22 at 05:29

0 Answers0