2012-10-24 20 views
35

मैं जावा का उपयोग करने के लिए नया हूं, लेकिन मेरे पास सी # के साथ कुछ पिछला अनुभव है। जो मुद्दा मैं कर रहा हूं वह कंसोल से उपयोगकर्ता इनपुट पढ़ने के साथ आता है।java.util.No.SuchElementException - स्कैनर उपयोगकर्ता इनपुट

मैं कोड के इस भाग के साथ "java.util.NoSuchElementException" त्रुटि में चल रहा हूँ:

  • PromptCustomerQty
  • :

    payment = sc.next(); // PromptCustomerPayment function 
    

    मैं दो कार्यों कि उपयोगकर्ता इनपुट प्राप्त है PromptCustomerPayment

यदि मैं PromptCustomerQty को कॉल नहीं करता तो मैं ge नहीं करता यह त्रुटि नहीं है, जो मुझे विश्वास दिलाती है कि मैं स्कैनर के साथ कुछ गलत कर रहा हूं। नीचे मेरा पूरा कोड नमूना है। मैं किसी भी मदद की सराहना करता हूं।

public static void main (String[] args) { 

    // Create a customer 
    // Future proofing the possabiltiies of multiple customers 
    Customer customer = new Customer("Will"); 

    // Create object for each Product 
    // (Name,Code,Description,Price) 
    // Initalize Qty at 0 
    Product Computer = new Product("Computer","PC1003","Basic Computer",399.99); 
    Product Monitor = new Product("Monitor","MN1003","LCD Monitor",99.99); 
    Product Printer = new Product("Printer","PR1003x","Inkjet Printer",54.23); 

    // Define internal variables 
    // ## DONT CHANGE 
    ArrayList<Product> ProductList = new ArrayList<Product>(); // List to store Products 
    String formatString = "%-15s %-10s %-20s %-10s %-10s %n"; // Default format for output 

    // Add objects to list 
    ProductList.add(Computer); 
    ProductList.add(Monitor); 
    ProductList.add(Printer); 

    // Ask users for quantities 
    PromptCustomerQty(customer, ProductList); 

    // Ask user for payment method 
    PromptCustomerPayment(customer); 

    // Create the header 
    PrintHeader(customer, formatString); 

    // Create Body 
    PrintBody(ProductList, formatString); 
} 

public static void PromptCustomerQty(Customer customer, ArrayList<Product> ProductList) { 
    // Initiate a Scanner 
    Scanner scan = new Scanner(System.in); 

    // **** VARIABLES **** 
    int qty = 0; 

    // Greet Customer 
    System.out.println("Hello " + customer.getName()); 

    // Loop through each item and ask for qty desired 
    for (Product p : ProductList) { 

     do { 
     // Ask user for qty 
     System.out.println("How many would you like for product: " + p.name); 
     System.out.print("> "); 

     // Get input and set qty for the object 
     qty = scan.nextInt(); 

     } 
     while (qty < 0); // Validation 

     p.setQty(qty); // Set qty for object 
     qty = 0; // Reset count 
    } 

    // Cleanup 
    scan.close(); 
} 

public static void PromptCustomerPayment (Customer customer) { 
    // Initiate Scanner 
    Scanner sc = new Scanner(System.in); 

    // Variables 
    String payment = ""; 

    // Prompt User 
    do { 
    System.out.println("Would you like to pay in full? [Yes/No]"); 
    System.out.print("> "); 

    payment = sc.next(); 

    } while ((!payment.toLowerCase().equals("yes")) && (!payment.toLowerCase().equals("no"))); 

    // Check/set result 
    if (payment.toLowerCase() == "yes") { 
     customer.setPaidInFull(true); 
    } 
    else { 
     customer.setPaidInFull(false); 
    } 

    // Cleanup 
    sc.close(); 
} 

उत्तर

86

यह वास्तव में मुझे थोड़ी देर के लिए परेशान कर रहा है, लेकिन अंत में मुझे यही मिला।

जब आप पहली विधि में sc.close() पर कॉल करते हैं, तो यह न केवल आपके स्कैनर को बंद करता है बल्कि आपके System.in इनपुट स्ट्रीम को बंद करता है।

System.out.println(System.in.available()); 

तो, अब जब आप फिर से इन्स्तांत, Scanner दूसरी विधि में, यह नहीं किसी भी खुले System.in धारा और इसलिए मिल रहा है: आप के रूप में दूसरी विधि के शीर्ष पर अपनी स्थिति को मुद्रण द्वारा यह सत्यापित कर सकते हैं अपवाद।

मुझे शक है किसी भी तरह से बाहर फिर से खोलना है तो System.in है क्योंकि:

public void close() throws IOException --> Closes this input stream and releases any system resources associated with this stream. The general contract of close is that it closes the input stream. A closed stream cannot perform input operations and **cannot be reopened.**

आपकी समस्या के लिए केवल अच्छा समाधान अपने मुख्य विधि में Scanner आरंभ करने के लिए है, जो आपके दो में तर्क के रूप में पारित

main विधि से संबंधित कोड ब्लॉक:

Scanner scanner = new Scanner(System.in); 

// Ask users for quantities 
PromptCustomerQty(customer, ProductList, scanner); 

// Ask user for payment method 
PromptCustomerPayment(customer, scanner); 

//close the scanner 
scanner.close(); 
विधियों, और इसे फिर से अपने मुख्य विधि जैसे में बंद

आपका तरीके:

public static void PromptCustomerQty(Customer customer, 
          ArrayList<Product> ProductList, Scanner scanner) { 

    // no more scanner instantiation 
    ... 
    // no more scanner close 
} 


public static void PromptCustomerPayment (Customer customer, Scanner sc) { 

    // no more scanner instantiation 
    ... 
    // no more scanner close 
} 

आशा यह आप की विफलता और संभव संकल्प के बारे में कुछ जानकारी मिलती है।

+4

प्रतिक्रिया के लिए धन्यवाद। दृश्यों के पीछे वास्तव में क्या हो रहा है इसकी आपकी व्याख्या बहुत उपयोगी है – fortune

+0

आपकी प्रतिक्रिया के लिए धन्यवाद। यह वास्तव में सच है! – d3vpasha

+0

ओह मेरे भगवान, मैं इतनी देर तक अटक गया जब तक कि मुझे यह पोस्ट नहीं मिला। आपको बहुत - बहुत धन्यवाद! तुम कमाल हो। –

15

समस्या

जब एक स्कैनर बंद कर दिया है, यह अपने इनपुट स्रोत बंद हो जाएगा स्रोत closeable इंटरफ़ेस लागू करता है, तो है।

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html

इस प्रकार scan.close()System.in बंद कर देता है।

इसे ठीक करने के लिए आप

Scanner scanstatic बना सकते हैं और PromptCustomerQty में यह बंद नहीं करते। नीचे कोड कोड।

public static void main (String[] args) { 

// Create a customer 
// Future proofing the possabiltiies of multiple customers 
Customer customer = new Customer("Will"); 

// Create object for each Product 
// (Name,Code,Description,Price) 
// Initalize Qty at 0 
Product Computer = new Product("Computer","PC1003","Basic Computer",399.99); 
Product Monitor = new Product("Monitor","MN1003","LCD Monitor",99.99); 
Product Printer = new Product("Printer","PR1003x","Inkjet Printer",54.23); 

// Define internal variables 
// ## DONT CHANGE 
ArrayList<Product> ProductList = new ArrayList<Product>(); // List to store Products 
String formatString = "%-15s %-10s %-20s %-10s %-10s %n"; // Default format for output 

// Add objects to list 
ProductList.add(Computer); 
ProductList.add(Monitor); 
ProductList.add(Printer); 

// Ask users for quantities 
PromptCustomerQty(customer, ProductList); 

// Ask user for payment method 
PromptCustomerPayment(customer); 

// Create the header 
PrintHeader(customer, formatString); 

// Create Body 
PrintBody(ProductList, formatString); 
} 

static Scanner scan; 

public static void PromptCustomerQty(Customer customer, ArrayList<Product> ProductList)    { 
// Initiate a Scanner 
scan = new Scanner(System.in); 

// **** VARIABLES **** 
int qty = 0; 

// Greet Customer 
System.out.println("Hello " + customer.getName()); 

// Loop through each item and ask for qty desired 
for (Product p : ProductList) { 

    do { 
    // Ask user for qty 
    System.out.println("How many would you like for product: " + p.name); 
    System.out.print("> "); 

    // Get input and set qty for the object 
    qty = scan.nextInt(); 

    } 
    while (qty < 0); // Validation 

    p.setQty(qty); // Set qty for object 
    qty = 0; // Reset count 
} 

// Cleanup 

} 

public static void PromptCustomerPayment (Customer customer) { 
// Variables 
String payment = ""; 

// Prompt User 
do { 
System.out.println("Would you like to pay in full? [Yes/No]"); 
System.out.print("> "); 

payment = scan.next(); 

} while ((!payment.toLowerCase().equals("yes")) && (!payment.toLowerCase().equals("no"))); 

// Check/set result 
if (payment.toLowerCase() == "yes") { 
    customer.setPaidInFull(true); 
} 
else { 
    customer.setPaidInFull(false); 
} 
} 

एक तरफ ध्यान दें पर, आप स्ट्रिंग तुलना के लिए == उपयोग नहीं करना चाहिए, बजाय .equals का उपयोग करें।

+0

लेकिन वे अलग-अलग तरीकों से सही हैं? जहां वे बनाए और बंद कर रहे हैं। –

+0

मूल कोड में वे हैं। निश्चित कोड में मैं दोनों विधियों के लिए एक स्कैनर उदाहरण का उपयोग करता हूं। –

+0

मुझे अभी भी परेशान है कि क्यों दूसरी विधि में 'sc.next() 'या' sc.nextLine() 'अपवाद भी अपवाद पैदा कर रहा है। –

0

इस लाइन के बाद:

qty = scan.nextInt(); 

हमेशा स्कैनर स्पष्ट करने के लिए एक और पंक्ति जोड़ें:

scan.nextLine(); 

इसके अलावा,

sc.nextLine(); 
बजाय

sc.next(); 
का उपयोग
+0

स्कैन .nextLine() जोड़ा गया; और sc.next() को sc.nextLine() में बदल दिया, लेकिन मुझे अभी भी त्रुटि मिल रही है। – fortune

संबंधित मुद्दे