开发者

Can not get the code to compile: ')' expected

开发者 https://www.devze.com 2023-02-25 04:16 出处:网络
I have reworked my java code for an Inventory program but, I still can not get the code to compile. import java.text.NumberFormat;

I have reworked my java code for an Inventory program but, I still can not get the code to compile.

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

class Television {

    private String itemNumber;
    private String productName;
    private double units;



    //constructor
  开发者_开发技巧  public Television (String itemNumber, String productName, double units, double unitprice) {
        setItemNumber(itemNumber);
        setProductName(productName);
        setUnits(units);
        setUnitPrice(unitPrice);

    }

    //accessor methods for class variables
    public String getItemNumber () {
        return itemNumber;
    }

    public void setItemNumber (String itemNumber) {
        this.itemNumber = itemNumber;
    }

    public String getProductName () {
        return productName;
    }

    public void setProductName (String productName) {
        this.productName = productName;
    }

    public double getUnits () {
        return units;
    }

    public void setUnits (double units) {
        this.units = units;
    }

    public double getUnitPrice () {
        return unitPrice;
    }

    public void setUnitPrice (double unitPrice) {
        this.unitPrice = unitPrice;
    }

}
public class InventoryPart1 {

    public static void main (String args[]) {

        NumberFormat nf = NumberFormat. getCurrencyInstance(Locale.US);

        //create an instance of the Television class

        Television samsung = new Television ("SAMSUNG 46", "Class 6400 Series", 9.3,1,599.99);

        //use the methods from class Television to output the inventory details.
        System.out.println("Item Number: " + samsung.getItemNumber());

        System.out.println("Product Name: " + samsung.getProductName());

        System.out.print("Number of Units: " + samsung.getUnits());

        System.out.print("Unit Price: " + nf.format(Samsung.getUnitPrice()));

        System.out.print("Inventory______ Total:_______"+

  nf.format(samsung.calculateInventoryTotal());

I am receiving the following error:

C:\Documents and Settings\AdminUser\MyDocuments\InventoryPart1.java:81: ')' expected nf.formatsamsung.calculateInventoryTotal)); ^ 1 error Tool completed with exit code 1


You are missing a parenthesis!

The last line should say:

System.out.print("Inventory______ Total:_______"+ nf.format(samsung.calculateInventoryTotal()));

(added a ) character at the end)

Good luck!

In fact, that's exactly what the error was saying: ')' expected means exactly that - the compiler was expecting a ) character and it wasn't there! :)


Do the following to compile your code, In your InventoryPart1 class.

  1. Put this call for Television class, just remove one argument as the constructor have only 4 parameters: Television samsung = new Television ("SAMSUNG 46", "Class 6400 Series", 9.3,599.99);
  2. Put one closing ')' as in above post suggested you are missing this: System.out.print("Inventory______ Total:_______"+ nf.format(samsung.calculateInventoryTotal()));
  3. change spelling of Samsung to samsung here: System.out.print("Unit Price: " + nf.format(Samsung.getUnitPrice()));

And in your Television class change this things listed :

  1. Put one instance variable private double unitPrice;
  2. change spelling of your parameter name unitprice to unitPrice: `public Television (String itemNumber, String productName, double units, double unitprice) { ...}
  3. Define one method as it calculates the total for you and return's totalAmt.public double calculateInventoryTotal() { //calculate total here; return totalAmt; }


  • unitprice doesn't match unitPrice in Television constructor call
  • unitPrice instance variable is missing
  • Television needs to be declared public
  • Television constructor doesn't match call in InventoryPart1 - one extra arg (or 1 missing param)
  • samsung variable mispelled as Samsung in InventoryPart1
  • calculateInventoryTotal method missing
  • should be

    System.out.print("Inventory_ Total:__" + nf.format(samsung.calculateInventoryTotal()));

You would probably find all these problems right away if you were using a decent IDE like Netbeans or Eclipse.

I'm assuming that InventoryPart1 and Television are in separate files InventoryPart1.java, Television.java -- if not, they should be.


As poundifdef pointed out (correctly), you are (first) missing a parenthesis. In your response, you say that you now get 5 errors instead of 1. So, I am more responding to that statement rather than your additional post.

One thing to possibly help you out in learning Java (because I made this same mistake when I was first beginning) is that the compile and runtime errors are EXTREMELY descriptive. I know seeing a ton of text come up on the screen can be intimidating, but take a second to read it and really parse what it says. Let's take a look at your current error (the parenthesis one).

C:\Documents and Settings\AdminUser\MyDocuments\InventoryPart1.java:81: ')' expected nf.formatsamsung.calculateInventoryTotal)); ^ 1 error Tool completed with exit code 1

So, this is basically saying:

  • InventoryPart1.java:81 - This is the line that your error is occurring on.
  • ')' expected - So, you are missing a closing parenthesis. This indicates you will probably have to count the parenthesis on that line (or, more likely, in that statement) and figure out where you didn't properly close the parenthesis.
  • nf.formatsamsung.calculateInventoryTotal)); - The compiler is showing you (approximately) where it thinks the problem is.

Once you have fixed this error, and you recompile, you may get more errors (and it sounds like you have from your other comments). The trick is to repeat the same process in parsing your errors and understanding what they are really saying. As was pointed out in another answer, a good IDE (Integrated Development Environment) such as Eclipse or NetBeans can do wonders in understanding your code and improving it. Other than that, it's a matter of stopping and spending some time thinking, "What is this error saying?" and "What is my code doing?" and "What am I really trying to do here?"

I know this didn't answer your question directly (and I did provide +1 to poundifdef) but I hope this does help you in your understanding of Java.

0

精彩评论

暂无评论...
验证码 换一张
取 消