I am trying to read a file and display the contents using tokenizer but the problem is some of the lines in my file have 6 tokens while some have 7 and when i try outputting the
file it gives me this error:
Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(Unknown Source)
at songsDisplay.displaySongs(songsDisplay.java:53)
at mainMenu.case1Menu(mainMenu.java:99)
at mainMenu.main(mainMenu.java:40)
Now i have tried everything i could think of to fix it i even tried replace && replaceAll but it gives me the same error could someone provide me with a solution
data = new Scanner(new File("example.txt")); String newline =""; int movieID = 0 ; String movieName = ""; String director = " "; String writer: =""; double duration = 0.0 ; String genre = ""; double rating = 0.0 ; while(data.hasNextLine()) { newline = data.nextLine(); StringTokenizer File = new StringTokenizer(newline, ","); int n = stFile.countTokens(); int i = 0; while(stFile.hasMoreTokens()) { movieID = Integer.parseInt(stFile.nextToken()); movieName = stFile.nextToken(); director = stFile.nextToken(); writer = stFile.nextToken(); duration = Double.parseDouble(stFile.nextToken()); genre = stFile.nextToken(); rating = Double.parseDouble(stFile.nextToken().replaceAll(" ", "0.0")); i=i+7; }