2012-02-28 153 views
1

我需要在此前言,我不允許在類中使用IDE,所以我必須使用TextPad進行編譯和運行。任何幫助將不勝感激。奇怪的JAVA編譯錯誤?

有三個文件,一個驅動程序文件和兩個其他類文件。 Lab4文件是驅動程序文件,類文件是「ITEM」和「NAME」。

當我編譯驅動程序「Lab4」文件時,出現以下錯誤。

 F:\Java\Lab 4A\Lab4.java:86: error: cannot find symbol 
     System.out.println ("Manufacturer's Name: " + item.manName()); 
                ^
     symbol: method manName() 
     location: variable item of type Item 

「Lab4」文件中的代碼是在這裏:

import java.util.Scanner; 

    public class Lab4 
    { 
    public static void main (String[] args) 
    { 
      String firstName = ""; 
      String lastName = ""; 
      String manName = ""; 
      float cost = 0.00f; 
      int itemNum = 0; 


     // Instantiate class files 
     // Variables are declared because variables are expected in name.java file 
     Name name = new Name("Bob", "Jones"); 
     Item item = new Item(985534, 9.99f, "Lewis Mfg"); 


     // Create a scanner 
     Scanner input = new Scanner(System.in); 


     // Recieve user input for variables 
     System.out.println ("Please enter the customer's first name: "); 
     firstName = input.next(); 
     while (firstName.length() < 1) 
      { 
      System.out.println ("Enter a valid first name"); 
      firstName = input.next(); 
      } 
      name.setfirstName(firstName); 
      firstName = ""; 

     System.out.println ("Please enter the customer's last name: "); 
     lastName = input.next(); 
     while (lastName.length() < 1) 
      { 
      System.out.println ("Enter a valid last name: "); 
      lastName = input.next(); 
      } 
      name.setlastName(lastName); 
      lastName = ""; 

     System.out.println ("Please enter the item number: "); 
     itemNum = input.nextInt(); 
     while (itemNum < 1) 
      { 
      System.out.println ("Enter a valid item number: "); 
      itemNum = input.nextInt(); 
      } 
      item.setitemNum(itemNum); 
      itemNum = 0; 

     System.out.println ("Please enter the item's cost (including decimal): "); 
     cost = input.nextFloat(); 
     while (cost < 1) 
      { 
      System.out.println ("Enter a valid cost amount: "); 
      cost = input.nextFloat(); 
      } 
     item.setcost(cost); 
      cost = 0; 

     System.out.println ("Please enter the manufacturer's name: "); 
     manName = input.next(); 
     while (manName.length() < 1) 
      { 
      System.out.println ("Enter a valid manufacturer's name"); 
      manName = input.next(); 
      } 
      item.setmanName(manName); 
      manName = ""; 


     // Outputs the data entered by the user and stored in the other classes,    Name and Item 
      System.out.println ("First Name: " + name.getfirstName() + "\n"); 
      System.out.println ("Last Name: " + name.getlastName() + "\n"); 
      System.out.println ("Item Number: " + item.getitemNum() + "\n"); 
     System.out.println ("Item Cost: " + item.getcost() + "\n"); 
     System.out.println ("Manufacturer's Name: " + item.manName()); 

     System.out.println ("\n\nThe customer and item information have been   entered.\n"); 

    } 
    } 

「ITEM」被錯誤引用的文件中的代碼是在這裏:

 public class Item 
{ 
    // Create private variables that cannot be accessed directly 
    private int itemNum; 
    private float cost; 
    private String manName; 

    // Create instances of the private variables that can be set by the methods 
    public Item (int itemNum, float cost, String manName) 
      { 
      this.itemNum = itemNum; 
      this.cost = cost; 
     this.manName = manName; 
     } 

    // Gets the variable values from the user via the driver class 
    public int getitemNum() 
     { 
     return itemNum; 
     } 

    public float getcost() 
     { 
     return cost; 
     } 

    public String getmanName() 
     { 
     return manName; 
     } 


    // Sets the variable amounts into the private variables after validating the input  form 
    public boolean setitemNum(int itemNum) 
     { 
     if (itemNum < 0) 
     return false; 
     else 
     this.itemNum = itemNum; 
     return true; 
     } 

    public boolean setcost(float cost) 
     { 
     if (cost < 0) 
     return false; 
     else 
     this.cost = cost; 
     return true; 
     } 

    public boolean setmanName(String manName) 
     { 
     if (manName.length() < 0) 
     return false; 
     else 
      this.manName = manName; 
     return true; 
     } 
    } 

任何幫助都會很棒!

+1

你可能想要'item.getmanName()',而不是'item.manName()'。 – birryree 2012-02-28 21:24:37

回答

8

您將您的方法命名爲getmanName(),但在錯誤行上調用manName()

2

你有簡單的拼寫錯誤錯誤:

你想item.getmanName()而不是manName()

2

manName是一個屬性,你引用它作爲一種方法manName()

因爲manNameprivate你必須使用您所稱爲的訪問者getmanName()

給出您的鱈魚的適當參考e將是item.getmanName()

該方法的更好的慣用名稱將是getManName()繼適當的UpperCamelCase命名約定爲Java。

1

要調用manName(),但你的方法是getmanName()

0

至於我可以看到有在Item類沒有manName方法。 Getter方法稱爲getmanName。通常有一條規則,告訴getter方法應該以獲得字開始,在此之後,您應該放置您想獲得值的類屬性名稱。

行更改,讓編譯錯誤:

System.out.println ("Manufacturer's Name: " + item.getmanName()); 

現在,它應該工作。