ich zur Zeit ein NoSuchElementException
bekommen, wenn durch meine for-Schleife ein zweites Mal gehen. Ich konnte ohne Probleme eine Schleife machen, bevor ich einige Änderungen an meinem Code vorgenommen habe. Ich musste die Benutzereingabe in der Methode setRiskAmount()
verschieben, anstatt das gleiche Scanner
Objekt in dieser Klasse zu verwenden, um diesen Eingang zu lesen. Da habe ich angefangen, den Fehler zu bekommen. Ich weiß, dass die Ausnahme ausgelöst wird, wenn nichts aus der Scanner
gelesen werden kann, ich bin nur nicht sicher, warum in diesem Szenario oder wie ich meinen Code anpassen.NoSuchElementException während Scanner-Objekt und Iterieren in einer Schleife
UPDATE: Wenn ich den gesamten Code in meiner setRiskAmount()
Methoden entfernen, bekomme ich nicht mehr die Fehler, und bei meiner nächsten Iteration kann ich Benutzereingaben lesen. Ich habe es auf eine andere Art und Weise gemacht und es hat perfekt funktioniert. Ich habe die Änderung vorgenommen, um bestimmte Anforderungen für meine Bewerbung zu erfüllen.
@Override
void setRiskAmount() {
// Ask for user input using a Scanner
Scanner input = new Scanner(System.in);
System.out.print("Please enter risk amount for Property Insurance >> ");
riskAmount = input.nextFloat();
input.close();
}
Bitte geben Sie Versicherungsart (Eigentum, Auto, Reise) oder BEENDEN
Property Bitte geben Sie Risikobetrag für Sachversicherungen >> 25000 verlassen ****** ************ IMMOBILIENVERSICHERUNG Der Satz beträgt: 0,25 Der Risikobetrag beträgt 25000,0 Die Prämie für die Sachversicherung beträgt: 6250,0 ***************** * Bitte geben Sie den Versicherungstyp (Eigenschaft, Auto, Reise) oder QUIT ein, um zu beenden >> Ausnahme im Thread "Haupt" java.util. NoSuchElementException: keine Linie bei java.util.Scanner.nextLine (Unknown Source) gefunden bei UseInsurance.main (UseInsurance.java:25)
public class UseInsurance {
public static void main(String[] args) {
// The Scanner object will be used for user input
Scanner input = new Scanner(System.in);
String t = "";
// Create the user interface
// Loop will continue looping until the user decides to exit
for (int i = 10; i > 0; i++) {
System.out.println("Please enter insurance type (Property, Auto, Travel) or QUIT to exit >> ");
t = input.nextLine();
if (t.equals("Property")) {
// Create an instance of PropertyInsurance, set the object name and type, ask for user input for risk amount
// calculate and add to the total quote. Display the results.
PropertyInsurance pInsurance = new PropertyInsurance("Property");
pInsurance.setInsuredObjectName();
pInsurance.setRiskAmount();
InsuranceAgentApp.totalPremium += pInsurance.getPremium();
pInsurance.display();
} else if (t.equals("Auto")) {
// Create an instance of AutomobileInsurance, set the object name and type, ask for user input for risk amount
// calculate and add to the total quote. Display the results.
AutomobileInsurance aInsurance = new AutomobileInsurance("Auto");
aInsurance.setInsuredObjectName();
aInsurance.setRiskAmount();
InsuranceAgentApp.totalPremium += aInsurance.getPremium();
aInsurance.display();
} else if (t.equals("Travel")) {
// Create an instance of TravelInsurance, set the object name and type, ask for user input for risk amount
// calculate and add to the total quote. Display the results.
TravelInsurance tInsurance = new TravelInsurance("Travel");
tInsurance.setInsuredObjectName();
tInsurance.setRiskAmount();
InsuranceAgentApp.totalPremium += tInsurance.getPremium();
tInsurance.display();
} else if ((t.equals("QUIT")) || (t.equals("quit")) || (t.equals("Quit"))) {
// Exit the loop upon the user typing QUIT, quit or Quit
break;
} else {
// If none of the above options are selected, display a message
// and loop again.
System.out.println("Invalid input");
}
}
// Display the amount of the total quote
System.out.println("The total quote is " + InsuranceAgentApp.totalPremium);
// Close the Scanner object
input.close();
}
}
Das tat es. Vielen Dank! – user2057488