Exception in thread "main"

java.util.NoSuchElementException

Page 1 of 1

13 Replies - 1009 Views - Last Post: 20 December 2009 - 04:41 PM Rate Topic: -----

#1 Priyanka98  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 16-December 09

Exception in thread "main"

Posted 20 December 2009 - 02:46 PM

I have written a code and it says that there are no errors. However, I am getting this message when I execute it.

--------------------Configuration: <Default>--------------------
XQACCH
Exception in thread "main" java.util.NoSuchElementException
	at java.util.StringTokenizer.nextToken(StringTokenizer.java:332)
	at VhsFileInputDemo.computing(VhsFileInputDemo.java:58)
	at VhsFileInputDemo.dataLoop(VhsFileInputDemo.java:51)
	at VhsFileInputDemo.main(VhsFileInputDemo.java:27)

Process completed.



I am importing data from an external data file for my program:
XQAC ZVM
PDAE AV
SNIC SY

My program is supposed to take this data and make the words plural and then add suffixes and output the results.
Here is my program:
import java.io.*;

import java.util.StringTokenizer;

public class VhsFileInputDemo
{
  // variables necessary for file input
  private static FileInputStream inFile;
  private static InputStreamReader inReader;
  private static  BufferedReader reader;

  //  StringTokenizer variable used to separate line into different data elements
  private static StringTokenizer strTknzer;

  // program variables to hold data
  static boolean isVowel;
  static int lengthWord, lengthSuffix;
  static String line, currentWord, currentSuffix, suffix, plural, currentCharacter, secondCharacterFromEnd, lastCharacter;

  // throws IOException will have the operating system handle any problems
  // that may occur while attempting to acquired data from the file.

  public static void main (String args[]) throws IOException
  {
  initFile();
  dataLoop();
  computing();
  isVowel();
  pluralization();

  // closing the data file
  inFile.close();
  }

  // preparing the file for input
  public static void initFile() throws IOException
  {
  inFile = new FileInputStream ("c:\\!!JavaDownload\\vowels.txt"); // notice the double slash marks
  inReader = new InputStreamReader(inFile);
  reader = new BufferedReader(inReader);
  }

  public static void dataLoop() throws IOException
  {
  for(line = reader.readLine(),
  strTknzer = new StringTokenizer(line);
  line != null;
  line = reader.readLine())
	{
	computing();
	pluralization();
	}
  } // end of data loop

  public static void computing() throws IOException
  {
  currentWord = strTknzer.nextToken();
  currentSuffix = strTknzer.nextToken();
  lengthWord = currentWord.length();
  lengthSuffix = currentSuffix.length();
  } // end of computing

  public static void isVowel() throws IOException
  {
  if (currentCharacter.equals("A") == true || currentCharacter.equals("C") == true || currentCharacter.equals("S") == true || currentCharacter.equals("L") == true)
	isVowel = true;

	else isVowel = false;
  } // end of isVowel

  public static void pluralization() throws IOException
  {
  secondCharacterFromEnd = currentWord.substring(lengthWord-2,lengthWord-1);
  lastCharacter = currentWord.substring(lengthWord-1);
  currentCharacter = secondCharacterFromEnd.toString();
  isVowel();
	if (isVowel == true)
	{
	currentCharacter = lastCharacter;
	isVowel();
	  if (isVowel == true)
	  System.out.println(currentWord+lastCharacter+"H");

	  else
	  {
	  System.out.println(currentWord+"GH");
	  currentCharacter = secondCharacterFromEnd;
	  isVowel();
	  }
	}

   else
   {
   currentCharacter = lastCharacter;
   isVowel();
	 if (isVowel == true)
	 {
	 plural = currentWord.substring(0, lengthWord-1);
	 System.out.println(plural+"G");
	 }

	 else
	 System.out.println(currentWord+"GH");
	 }

  } // end of pluralization

} // end of public class


This post has been edited by Priyanka98: 20 December 2009 - 02:46 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Exception in thread "main"

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Exception in thread "main"

Posted 20 December 2009 - 03:11 PM

if there is no next line it doesn't return null, it returns that exception

You need to use hasNext() or hasNextLine() instead
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10477
  • View blog
  • Posts: 38,835
  • Joined: 27-December 08

Re: Exception in thread "main"

Posted 20 December 2009 - 03:14 PM

View PostPriyanka98, on 20 Dec, 2009 - 05:46 PM, said:

I have written a code and it says that there are no errors. However, I am getting this message when I execute it.

--------------------Configuration: <Default>--------------------
XQACCH
Exception in thread "main" java.util.NoSuchElementException
	at java.util.StringTokenizer.nextToken(StringTokenizer.java:332)
	at VhsFileInputDemo.computing(VhsFileInputDemo.java:58)
	at VhsFileInputDemo.dataLoop(VhsFileInputDemo.java:51)
	at VhsFileInputDemo.main(VhsFileInputDemo.java:27)

Process completed.



I am importing data from an external data file for my program:
XQAC ZVM
PDAE AV
SNIC SY

  public static void dataLoop() throws IOException
  {
  for(line = reader.readLine(),
  strTknzer = new StringTokenizer(line);
  line != null;
  line = reader.readLine())
	{
	computing();
	pluralization();
	}
  } // end of data loop

  public static void computing() throws IOException
  {
  currentWord = strTknzer.nextToken();
  currentSuffix = strTknzer.nextToken();
  lengthWord = currentWord.length();
  lengthSuffix = currentSuffix.length();
  } // end of computing




It is saying that you are trying to reference the next token in the StringTokenizer (the suffix specifically), but it doesn't exist. The error comes from the computing() method when it is invoked in the datLoop() method.

I went ahead and moved the StringTokenizer instantiation to within the body of the for loop instead of within its parentheses, and it worked:
public static void dataLoop() throws IOException
  {
  for(line = reader.readLine();line != null; line = reader.readLine())
	{
	strTknzer = new StringTokenizer(line);
	computing();
	pluralization();
	}
  } // end of data loop


Was This Post Helpful? 1
  • +
  • -

#4 Priyanka98  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 16-December 09

Re: Exception in thread "main"

Posted 20 December 2009 - 03:28 PM

View Postmacosxnerd101, on 20 Dec, 2009 - 02:14 PM, said:

It is saying that you are trying to reference the next token in the StringTokenizer (the suffix specifically), but it doesn't exist. The error comes from the computing() method when it is invoked in the datLoop() method.

I went ahead and moved the StringTokenizer instantiation to within the body of the for loop instead of within its parentheses, and it worked:
public static void dataLoop() throws IOException
  {
  for(line = reader.readLine();line != null; line = reader.readLine())
	{
	strTknzer = new StringTokenizer(line);
	computing();
	pluralization();
	}
  } // end of data loop



I tried this method and I was able to outprint the plural of the words, however not the suffixes. I still got one error at the end.
This became my output:
--------------------Configuration: <Default>--------------------
XQACCH
PDAEGH
SNIG
Exception in thread "main" java.util.NoSuchElementException
	at java.util.StringTokenizer.nextToken(StringTokenizer.java:332)
	at VhsFileInputDemo.computing(VhsFileInputDemo.java:58)
	at VhsFileInputDemo.main(VhsFileInputDemo.java:27)

Process completed.


Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10477
  • View blog
  • Posts: 38,835
  • Joined: 27-December 08

Re: Exception in thread "main"

Posted 20 December 2009 - 03:37 PM

That sounds about right seeing as you don't have any code to print out the suffix. In terms of your error, you might want to use the countTokens() method from the StringTokenizer before you call the computing() method to make sure there are 2 tokens.

@KYA: I don't believe these File Input objects have a hasNext() or hasNextLine() method. The StringTokenizer countTokens() or hasMoreTokens() may be more helpful.

This post has been edited by macosxnerd101: 20 December 2009 - 03:41 PM

Was This Post Helpful? 0
  • +
  • -

#6 Priyanka98  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 16-December 09

Re: Exception in thread "main"

Posted 20 December 2009 - 03:40 PM

View PostKYA, on 20 Dec, 2009 - 03:11 PM, said:

if there is no next line it doesn't return null, it returns that exception

You need to use hasNext() or hasNextLine() instead


I tried this, but it kept saying that I was missing ';' and I would put that and it would say that ')' was missing and after I did it said ')' was wrong. It was an endless circle of errors.
This was my error:
--------------------Configuration: <Default>--------------------
C:\!!VHSJava\VHSFileInputDemo\VhsFileInputDemo.java:46: cannot find symbol
symbol  : method hasNext()
location: class VhsFileInputDemo
	line = hasNext();
		   ^
C:\!!VHSJava\VHSFileInputDemo\VhsFileInputDemo.java:46: incompatible types
found   : java.lang.String
required: boolean
	line = hasNext();
		 ^
2 errors

Process completed.


Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Exception in thread "main"

Posted 20 December 2009 - 03:42 PM

Bah, thought it was a scanner, it's a buffered reader, it does return NULL. Never mind.

So much for consistency Java. :/
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10477
  • View blog
  • Posts: 38,835
  • Joined: 27-December 08

Re: Exception in thread "main"

Posted 20 December 2009 - 03:45 PM

Of course you get an error- by calling hasNext() like you are doing, you are indicating that the method is defined within your class. However, since the method doesn't exist within your class, you get an error.
Was This Post Helpful? 0
  • +
  • -

#9 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Exception in thread "main"

Posted 20 December 2009 - 03:51 PM

Try this:

public static void dataLoop() throws IOException
  {
  for(line = reader.readLine();line != null; line = reader.readLine())
	{
	strTknzer = new StringTokenizer(line);
	if(strTknzer.countTokens() >= 2){ 
		  computing();
	 }
	 else //print some error message or whatnot
	pluralization();
	}
  } // end of data loop




or == 2 not sure how many you expect at a time, two i imagine looking at the code

This post has been edited by KYA: 20 December 2009 - 03:52 PM

Was This Post Helpful? 1
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10477
  • View blog
  • Posts: 38,835
  • Joined: 27-December 08

Re: Exception in thread "main"

Posted 20 December 2009 - 03:58 PM

View Postmacosxnerd101, on 20 Dec, 2009 - 06:37 PM, said:

That sounds about right seeing as you don't have any code to print out the suffix. In terms of your error, you might want to use the countTokens() method from the StringTokenizer before you call the computing() method to make sure there are 2 tokens.

@KYA: I don't believe these File Input objects have a hasNext() or hasNextLine() method. The StringTokenizer countTokens() or hasMoreTokens() may be more helpful.


@KYA: Looks like a familiar solution. I like it. :^:
Was This Post Helpful? 0
  • +
  • -

#11 Priyanka98  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 16-December 09

Re: Exception in thread "main"

Posted 20 December 2009 - 04:19 PM

I inputted the suggestions.

For the getData loop I have:
public static void dataLoop() throws IOException
  {
  for(line = reader.readLine();
  line != null;
  line = reader.readLine())
  {
  strTknzer = new StringTokenizer(line);
	if(strTknzer.countTokens() >= 2)
	{
	computing();
	}

	else
	{
	System.out.println("Error");
	}

  pluralization();
  }
  } // end of data loop




But, I get this error:
--------------------Configuration: <Default>--------------------
Plural : XQACCH
Plural : PDAEGH
Plural : SNIG
Exception in thread "main" java.util.NoSuchElementException
	at java.util.StringTokenizer.nextToken(StringTokenizer.java:332)
	at VhsFileInputDemo.computing(VhsFileInputDemo.java:68)
	at VhsFileInputDemo.main(VhsFileInputDemo.java:27)

Process completed.



Looking at the error, it has to do with the Computing.
Which is:
public static void computing() throws IOException
  {
  currentWord = strTknzer.nextToken();
  currentSuffix = strTknzer.nextToken();
  lengthWord = currentWord.length();
  lengthSuffix = currentSuffix.length();
  } // end of computing


I'm not sure how to fix this though. I have it saying nextToken() in the computing, but I don't see why it does not work with count tokens >= 2.
Was This Post Helpful? 0
  • +
  • -

#12 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Exception in thread "main"

Posted 20 December 2009 - 04:28 PM

View Postmacosxnerd101, on 20 Dec, 2009 - 03:58 PM, said:

View Postmacosxnerd101, on 20 Dec, 2009 - 06:37 PM, said:

That sounds about right seeing as you don't have any code to print out the suffix. In terms of your error, you might want to use the countTokens() method from the StringTokenizer before you call the computing() method to make sure there are 2 tokens.

@KYA: I don't believe these File Input objects have a hasNext() or hasNextLine() method. The StringTokenizer countTokens() or hasMoreTokens() may be more helpful.


@KYA: Looks like a familiar solution. I like it. :^:



Sorry, didn't mean to steal your thunder. Figured I'd just make amends for my lack of reading comprehension earlier.
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10477
  • View blog
  • Posts: 38,835
  • Joined: 27-December 08

Re: Exception in thread "main"

Posted 20 December 2009 - 04:31 PM

Same problem, different location. This time, it is being invoked directly from main() instead of from dataLoop(). By the time dataLoop() finishes running, you have reached the end of the StringTokenizer. You then go and run computing(), which tries to access the StringTokenizer. Honestly, your problem is that you set each method to call the next one inside the class definition (dataLoop() calls computing() which calls pluralization(), etc.), then you set up this same logic in your main() method. So in short, you are chasing your tail so to speak and getting errors because of it.

@KYA: Don't worry about it. No harm, no foul. :)

This post has been edited by macosxnerd101: 20 December 2009 - 04:33 PM

Was This Post Helpful? 0
  • +
  • -

#14 Priyanka98  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 16-December 09

Re: Exception in thread "main"

Posted 20 December 2009 - 04:41 PM

View Postmacosxnerd101, on 20 Dec, 2009 - 03:31 PM, said:

Same problem, different location. This time, it is being invoked directly from main() instead of from dataLoop(). By the time dataLoop() finishes running, you have reached the end of the StringTokenizer. You then go and run computing(), which tries to access the StringTokenizer. Honestly, your problem is that you set each method to call the next one inside the class definition (dataLoop() calls computing() which calls pluralization(), etc.), then you set up this same logic in your main() method. So in short, you are chasing your tail so to speak and getting errors because of it.

@KYA: Don't worry about it. No harm, no foul. :)


Okay. So, I took some things out of the main() method and it worked perfectly!
public static void main (String args[]) throws IOException
  {
  initFile();
  dataLoop();
  // closing the data file
  inFile.close();
  }



Thank you everybody for your help! I've "thanked" you both. My program FINALLY works.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1