Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke, David J. Auer

7th edition

133544621, 133544626, 0-13-354462-1, 978-0133544626

More Books

Students also viewed these Databases questions

Question

=+derived from the assignment will balance the costs?

Answered: 1 week ago

Question

=+6 Who is the peer of the IA ?

Answered: 1 week ago