2016-04-19 8 views
0

Ich bin neu in Java und ich kann meine Methode nicht funktionieren lassen, ohne die Variable auf einen Wert von 'null' oder ein neues Objekt zurücksetzen zu müssen. Was ich brauche, ist, dass die Benutzereingabe in die nächste Methode übernommen wird.Variablenwert auf neue Methode übertragen

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

Mit über neue Verfahren tragen, so dass Sie meinen Sie es als Parameter übergeben wollen, vielleicht? – Andreas

+0

Im Allgemeinen ist in der OO-Programmierung die beste Lösung für etwas wie das Deklarieren einer Klasse mit privaten Instanzfeldern. Dann könnte eine Methode ein Instanzfeld setzen und die "nächste Methode" könnte es lesen. Ihre eigentliche Frage ist jedoch relativ kurz, daher weiß ich nicht genau, was Sie mit "Eingabe" oder "nächste Methode" oder irgendetwas sagen. – ajb

+0

Sorry, also was ich versuche zu bekommen ist aus dem Code der Benutzer gibt eine Eingabe in den Bereich addItem(), ich möchte dann zeigen, was sie dort in den prodData() Bereich eingegeben, aber im Moment habe ich die product product = neues Produkt sowohl in additem() als auch in prodData(), was bedeutet, dass die Benutzereingabe weggenommen wird –

Antwort

0

Erklären Sie Ihre Produkt var als global und Zugriff wie folgt

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

okay, danke für diese Antwort, ich habe dein Bit hinzugefügt, aber jetzt bekomme ich und Fehler von java.lang.NullPointerException. –

+0

@RaznishSmith können Sie die Stack-Trace teilen? –