2016-04-19 6 views
0

私はJavaを使い慣れていないので、変数を 'null'または新しいObjectの値にリセットする必要がなく、メソッドを動作させることができません。私が必要とするのは、ユーザー入力を次のメソッドに引き継ぐことです。変数値を新しいメソッドに引き継ぐ

public static void addItem() 
{ 
    Scanner console = new Scanner(System.in); 

    String desc, id, str=""; 
    double price = 0, setUpPrice = 0, unitCost = 0, inventoryCost = 0; 
    int stock = 0, demand = 0; 
    Product product = new Product(); 


    System.out.print("Please enter product description between 3 to 10 characters...: "); 
    desc = console.next(); 
    desc = desc.toLowerCase(); 
    product.setName(desc); 


    /*if (desc.length < 3 || desc.length > 10) 
    { 
     System.out.print("This Input is incorrect, Please try again."); 
    }*/ 
    System.out.print("Please enter price in $ : "); 
    price = console.nextDouble(); 
    System.out.print("Please enter set up price. $ : "); 
    setUpPrice = console.nextDouble(); 
    System.out.print("Please enter unit- cost. $ : "); 
    unitCost = console.nextDouble(); 
    System.out.print("Please enter the inventory cost. $ : "); 
    inventoryCost = console.nextDouble(); 
    System.out.print("Please enter the amount in stock : "); 
    stock = console.nextInt(); 
    System.out.print("Please enter the demand of the product : "); 
    demand = console.nextInt(); 


    // need code here to repeat the fuction again if y is pressed. 
    // if n is pressed need to return to inventory management interface. 

} 

public static void prodData() 
{ 
    Product product = new Product(); 

    System.out.println("The information for the product is : "); 
    System.out.println("Product description : "+product.getName()); 
} 
+0

を次のようにグローバルおよびアクセスとして、製品のVARを宣言し、そうあなたはおそらく、それをパラメータとして渡したいわけ? – Andreas

+0

一般に、オブジェクト指向プログラミングでは、このようなものに対する最良の解決策は、プライベートインスタンスフィールドを持つクラスを宣言することです。メソッドはインスタンスフィールドを設定し、 "次のメソッド"はそれを読み取ることができます。しかし、あなたの実際の質問は具体的にはかなり短いので、私はあなたが "入力"や "次の方法"などで何を指しているのか本当に分かっていません。 – ajb

+0

申し訳ありませんが、私が取得しようとしているのは、ユーザーがaddItem()領域に入力したコードからです。そこで入力した内容をprodData()領域に表示したいのですが、 product item = additem()とprodData()の両方の新製品で、ユーザ入力が取り除かれたことを意味します –

答えて

0

新しい方法に持ち越さでは

public static Product product; 

public static void addItem() { 
    product = new Product(); 
    Scanner console = new Scanner(System.in); 

    String desc, id, str=""; 
    double price = 0, setUpPrice = 0, unitCost = 0, inventoryCost = 0; 
    int stock = 0, demand = 0; 

    System.out.print("Please enter product description between 3 to 10 characters...: "); 
    desc = console.next(); 
    desc = desc.toLowerCase(); 
    product.setName(desc); 


    /*if (desc.length < 3 || desc.length > 10) 
    { 
     System.out.print("This Input is incorrect, Please try again."); 
    }*/ 
    System.out.print("Please enter price in $ : "); 
    price = console.nextDouble(); 
    System.out.print("Please enter set up price. $ : "); 
    setUpPrice = console.nextDouble(); 
    System.out.print("Please enter unit- cost. $ : "); 
    unitCost = console.nextDouble(); 
    System.out.print("Please enter the inventory cost. $ : "); 
    inventoryCost = console.nextDouble(); 
    System.out.print("Please enter the amount in stock : "); 
    stock = console.nextInt(); 
    System.out.print("Please enter the demand of the product : "); 
    demand = console.nextInt(); 

    // need code here to repeat the fuction again if y is pressed. 
    // if n is pressed need to return to inventory management interface. 

} 

public static void prodData() { 
    System.out.println("The information for the product is : "); 
    System.out.println("Product description : "+product.getName()); 
} 
+0

その答えをありがとう、私はあなたのビットを追加しましたが、今私は取得しているとjava.lang.NullPointerExceptionのエラー。 –

+0

@RaznishSmithスタックトレースを共有できますか? –

関連する問題