Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to read an empty token and display it in the output??

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy How to read an empty token and display it in the output??

    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;
    				}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to read an empty token and display it in the output??

    java.util.NoSuchElementException
    Look at the API doc for the StringTokenizer class. There is a method that will tell you the number of tokens and another method that will tell you if there is another token available. The program should use those methods to test if there is a token available so it won't get the error.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: September 28th, 2013, 07:36 PM
  2. Radio button .. display empty screen ?? plz Help
    By smasm in forum AWT / Java Swing
    Replies: 4
    Last Post: April 7th, 2012, 07:21 PM
  3. Read input, read file, find match, and output... URGENT HELP!
    By MooseHead in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 3rd, 2012, 11:01 AM
  4. Syntax error on token ";", { expected after this token please HELP
    By Creeper in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 1st, 2012, 03:12 PM
  5. Syntax error on token ";", @ expected after this token
    By MagicMojo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 16th, 2011, 07:48 AM

Tags for this Thread