Question: I am getting the NoSuchElementException when reading from a | delimited text file trying to run the program I think this part what causes the

I am getting the NoSuchElementException when reading from a "|" delimited text file trying to run the program

I think this part what causes the error

public void readFromFile(String file) { operas.clear(); //clear the ArrayList try { //Read while there is data Scanner input = new Scanner(new File(file)); //alternative to FileReader & BufferedReader String line = ""; Opera music = null; StringTokenizer token = null; while(input.hasNextLine()) { line = input.nextLine(); music = new Opera(); // create an opera token = new StringTokenizer(line, "|"); while (token.hasMoreElements()) { music.setName(token.nextToken()); music.setComposer(token.nextToken()); music.setYear(Integer.parseInt(token.nextToken())); music.setCity(token.nextToken()); music.setSynopsis(token.nextToken()); music.setLink((token.nextToken())); } //add opera to arraylist operas.add(music); } input.close(); } catch(FileNotFoundException e) { JOptionPane.showMessageDialog(null, file + " does not exist", "File Input Error", JOptionPane.WARNING_MESSAGE); //Bring up JFileChooser to select file in current directory JFileChooser chooser= new JFileChooser("src/FamousOperas"); //Filter only txt files FileNameExtensionFilter filter = new FileNameExtensionFilter( "Txt Files", "txt"); chooser.setFileFilter(filter); int choice = chooser.showOpenDialog(null); if (choice == JFileChooser.APPROVE_OPTION) { File chosenFile = chooser.getSelectedFile(); file = "src/FamousOperas/" + chosenFile.getName(); //System.out.println("file = " + file); readFromFile(file); } else//wierd I/O error { JOptionPane.showMessageDialog(null, "Unable to read file", "File Input Error", JOptionPane.WARNING_MESSAGE); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!