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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started