이것을 실행하고이 특정 메서드를 호출하려고하면 NoSuchElementException
이 표시됩니다. Scanner
으로 직접 파일을 읽거나 인쇄하는 대신에 ArrayList
으로 변경하기 전에 정상적으로 작동했습니다. 스레드에서NoSuchElementException 파일에서 채워진 ArrayList를 인쇄 할 때
에서예외 "주"java.util.NoSuchElementException
내가 옵션 2 [
java.util.Scanner.throwFor (알 수없는 소스)에서
: 여기ArrayList
]을 선택할 때 그것이 말하는 것입니다 java.util.Scanner.next (알 소스)에 version4.version4.readDisplay에서
(version4.java:79) version4.version4.main (version4.java:27)
17,451,515,
내 코드 :
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
및/또는 방법을 해결하기 위해를 일으키는 원인을 알아?
을 시도 할 것 ... 몇 가지 일이 벌어있을 수 있습니다. –
그 부분은 예외를 던지고있는 것이 아니라 여전히 동일한 것을 가지고 있으며 Elliot의 수정 후에도 잘 작동합니다. – Corey