Java:java.util.NoSuchElementException:未找到行 [英] Java: java.util.NoSuchElementException: No line found
本文介绍了Java:java.util.NoSuchElementException:未找到行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个问题,不知道该怎么办。以下是异常:
线程main中的异常java.util.NoSuchElementException:在java中没有找到
。 < init>(Auto.java:32)
at Auto.main(Auto.java:22)
这是我的代码:
import java.util.Scanner;
public class Auto {
private String Marke1;
String Modell1;
String Farbe1;
doubleHöhe;
double Breite;
doubleLänge;
int Tueren = 4;
public static void main(String [] args){
System.out.println(Hello Auto!
Auto myFerrari = new Auto();
myFerrari.einfacheNachricht();
Auto myOpel = new Auto();
myOpel.einfacheNachricht();
}
public Auto(){
Scanner r = new Scanner(System.in);
System.out.println(Bitte geben Sie die Marke ihres Autos an);
Marke1 = r.nextLine()
System.out.println(Bitte geben Sie das Modell ihres Autos an);
Modell1 = r.nextLine();
System.out.println(Bitte geben Sie die Farbe ihres Autos an);
Farbe1 = r.nextLine();
System.out.println(Bitte geben Sie an,wiehoch ihr Auto ist);
Höhe= r.nextDouble();
System.out.println(Bitte geben Sie an,wie breit ihr Auto ist);
Breite = r.nextDouble();
System.out.println(Bitte geben Sie an,wie lang ihr Auto ist);
Länge= r.nextDouble();
r.close();
}
public Auto(String aMarke1,String aModell1,String aFarbe1,doubleaHöhe,double aBreite,doubleaLänge){
Marke1 = aMarke1;
Modell1 = aModell1;
Farbe1 = aFarbe1;
Höhe=aHöhe;
Breite = aBreite;
Länge=aLänge;
}
public int getSitzreihen(){
return Tueren = 2;
}
public void einfacheNachricht(){
System.out.println(-------------------- ----------------------);
System.out.println(Automarke:+ Marke1);
System.out.println(Modell:+ Modell1);
System.out.println(Farbe:+ Farbe1);
System.out.println(Sitzreihen:+ getSitzreihen());
System.out.println(Volumen:+(Höhe* Breite *Länge)+m²);
System.out.println(Ihr+ Farbe1 +er+ Marke1 ++ Modell1 +hat ein Volumen von+(Höhe* Breite *Länge)+
System.out.println(--------------------------------------- --- \\\
);
}
}
解决方案关闭
Scanner
会导致底层InputStream
被关闭。由于只有一个System.in
,任何新创建的Scanner
对象将无法从同一个流读取:r.close();
此行应该删除
I have a problem and don't know what to do. Here's the exception:
Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Unknown Source) at Auto.<init>(Auto.java:32) at Auto.main(Auto.java:22)
And that's my Code:
import java.util.Scanner; public class Auto { private String Marke1; String Modell1; String Farbe1; double Höhe; double Breite; double Länge; int Tueren = 4; public static void main(String[] args) { System.out.println("Hello Auto!"); Auto myFerrari = new Auto(); myFerrari.einfacheNachricht(); Auto myOpel = new Auto(); myOpel.einfacheNachricht(); } public Auto() { Scanner r = new Scanner(System.in); System.out.println("Bitte geben Sie die Marke ihres Autos an"); Marke1 = r.nextLine() System.out.println("Bitte geben Sie das Modell ihres Autos an"); Modell1 = r.nextLine(); System.out.println("Bitte geben Sie die Farbe ihres Autos an"); Farbe1 = r.nextLine(); System.out.println("Bitte geben Sie an, wiehoch ihr Auto ist"); Höhe = r.nextDouble(); System.out.println("Bitte geben Sie an, wie breit ihr Auto ist"); Breite = r.nextDouble(); System.out.println("Bitte geben Sie an, wie lang ihr Auto ist"); Länge = r.nextDouble(); r.close(); } public Auto(String aMarke1, String aModell1, String aFarbe1, double aHöhe, double aBreite, double aLänge) { Marke1 = aMarke1; Modell1 = aModell1; Farbe1 = aFarbe1; Höhe = aHöhe; Breite = aBreite; Länge = aLänge; } public int getSitzreihen() { return Tueren = 2; } public void einfacheNachricht() { System.out.println("------------------------------------------"); System.out.println("Automarke: " + Marke1); System.out.println("Modell: " + Modell1); System.out.println("Farbe: " + Farbe1); System.out.println("Sitzreihen: " + getSitzreihen()); System.out.println("Volumen: " +(Höhe * Breite * Länge) +"m²" ) ; System.out.println("Ihr " + Farbe1 + "er " + Marke1 + " " + Modell1 + " hat ein Volumen von "+(Höhe * Breite * Länge) +"m²"); System.out.println("------------------------------------------\n"); } }
解决方案Closing a
Scanner
causes the underlyingInputStream
to be closed also. As there is only oneSystem.in
, any newly createdScanner
objects will not be able to read from the same stream:r.close();
This line should be removed
See: Do not create multiple buffered wrappers on a single InputStream
这篇关于Java:java.util.NoSuchElementException:未找到行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文