이것을 실행하고이 특정 메서드를 호출하려고하면 NoSuchElementException
. ArrayList
파일에서 직접 읽거나 인쇄 하는 대신 Scanner
.
옵션 2 [ ArrayList
]를 선택할 때 표시되는 내용은 다음과 같습니다 .
스레드에서 예외 "주"java.util.NoSuchElementException
java.util.Scanner.throwFor (알 수없는 소스)에서
java.util.Scanner.next (알 수없는 소스)에서
version4.version4.readDisplay에서 (version4.java:79)
버전 4에서 .version4.main (버전 4.java:27)
내 코드 :
public class version4
{
public static void main(String[] args) throws FileNotFoundException
{
Scanner in = new Scanner(System.in);
boolean exit = false;
while (!exit)
{
System.out.println("1 Find an item.\n2 Display all items.\n3 Update item.\n4 Save item to disk.\n5 Quit.");
int choice = in.nextInt();
switch (choice){
case 1: System.out.println("You chose to find an item from file."); findItem(); break;
case 2: System.out.println("You chose to display all items."); readDisplay(); break;
case 3: System.out.println("You chose to update an item."); itemUpdate(); break;
case 4: System.out.println("You chose to save an item to disk."); itemAdd(); break;
case 5: exit = true; break;
default: System.out.println("That is not a valid option."); break;
}
}
System.out.println("Goodbye.");
}
public static void readDisplay() throws FileNotFoundException
{
// Open input file:
System.out.println("Reading 'read_record.txt'");
FileReader reader = new FileReader("read_record.txt");
Scanner fin = new Scanner(reader);
String str = null;
ArrayList<String> dvdfile = new ArrayList<String>();
while((str = fin.next()) != null){
dvdfile.add(str);
}
Iterator iter = dvdfile.iterator();
while (iter.hasNext())
{
String sku = (String) iter.next();
String title = (String) iter.next();
String length = (String) iter.next();
System.out.printf("%-10s %-15s %10s %n",sku,title,length);
}
// Close file:
fin.close();
}
}
누구든지 원인 NoSuchElementException
및 / 또는 해결 방법을 알고 있습니까?
몇 가지 일이있을 수 있습니다 ... 나는 이것을 시도 할 것입니다
if (fin != null && fin.hasNext()) { // do we have a scanner, is there anything to read?
while (fin.hasNext()) { // while there's something to read...
str = fin.next(); // read it.
if (str == null) { // end if it's null?
break;
}
dvdfile.add(str); // otherwise add it.
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다