2012-02-28 9 views
1

私はクラスでIDEを使用することは許可されていないので、これを前にする必要があります。コンパイルして実行するにはTextPadを使用する必要があります。どんな助けでも大歓迎です。Odd JAVAコンパイルエラー?

ドライバファイルと2つのクラスファイルの3つのファイルがあります。 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.manName()'の代わりに 'item.getmanName()'が必要でした。 – birryree

答えて

8

getmanName()という名前を付けたが、エラー行にmanName()という名前を付けた。

2

あなたは、単純なタイプミスのエラーを得た:あなたはitem.getmanName()なくmanName()

2

manNameはあなたがmanName以来方法manName()

として、それを参照している、属性でありたい

は、あなたが使用する必要がありprivateですあなたが呼び出したアクセサーgetmanName()

あなたのcod eはitem.getmanName()

となります。このメソッドのより良い慣用名は、UpperCamelCase Javaの命名規則に従うと、getManName()になります。

1

あなたはmanName()を呼び出しているが、あなたの方法はgetmanName()私の知る限りItemクラスにはmanName方法はありません見ることができるよう

0

です。 GetterメソッドはgetmanNameと呼ばれます。一般的に、getterメソッドはから始まり、という単語を取得し、その後は値を取得するクラス属性名を配置する必要があるというルールがあります。コンパイルエラーを与える

変更ライン:

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

は今、それが動作するはずです。