1 Attachment(s)
Newbie JCreator NoSuchElementException Help!
I'm trying to run a project that involves using no global variables other than the import ones seen below. It's supposed to read a text file with three lines of words. (I've attached it) and then scan them and add a certain suffix or turn the words plural depending on their endings. I have the project running but it seems that it won't read the next line in the input file, and gives me this error.
--------------------------------------------------------------------------
XQAC ZVM
Original String: XQAC ZVM
Plural: XQACCH
Suffix: XQCZVM
Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenize r.java:332)
at VowelsRUsNoGlobals2.main(VowelsRUsNoGlobals2.java: 47)
Process completed.
--------------------------------------------------------------------------
The output should look like that first bit of the out put but with three different lines. I tried following the NoSuchElementException, but I am new and still having trouble. Here is the code, any and all help is greatly appreciated!
--------------------------------------------------------------------------
import java.io.*;
import java.util.StringTokenizer;
import java.util.HashSet;
public class VowelsRUsNoGlobals2
{
public static void main (String args[]) throws IOException
{
String suffix = new String();
String suffixR = new String();
String word = new String();
String wordR = new String();
String wordPlural = new String();
String wordSuffix = new String();
String firstLetter = new String();
String lastLetter = new String();
String testLetter = new String();
String vowelSeries = new String();
String consonantSeries = new String();
String line = new String();
FileInputStream inFile;
InputStreamReader inReader;
BufferedReader reader;
StringTokenizer strTkn;
HashSet vowels = null;
int leftmostIndex = 0;
inFile = new FileInputStream ("C://!!VHSAPCSData/Vowels.txt");
inReader = new InputStreamReader (inFile);
reader = new BufferedReader (inReader);
line = null;
line = reader.readLine();
strTkn = new StringTokenizer(line);
while (line != null) {
if (line == null) {
break;
}
word = strTkn.nextToken();
suffix = strTkn.nextToken();
System.out.println (line);
vowels = new HashSet();
vowels.add("A");
vowels.add("C");
vowels.add("S");
vowels.add("L");
wordPlural = word;
wordSuffix = word;
for (int i = 0; i < word.length(); i++) {
wordR = word.substring(i, i+1) + wordR;
}
for (int i = 0; i < suffix.length(); i++) {
suffixR = suffix.substring(i, i+1) + suffixR;
}
if (vowels.contains(wordR.substring(0,1)) == false) {
if (vowels.contains(wordR.substring(1,2)) == false) {
lastLetter = wordR.substring(0,1);
wordPlural = wordR.concat(lastLetter);
wordPlural = wordR.concat("H");
System.out.print("plural: " + wordR);
if (vowels.contains(suffixR.substring(1,2)) == false) {
vowelSeries = word.substring(word.length()-leftmostIndex, word.length());
vowelSeries = vowelSeries.substring(1, vowelSeries.length());
wordSuffix = wordSuffix.substring(0, leftmostIndex);
wordSuffix = wordSuffix.concat(vowelSeries);
wordSuffix = wordSuffix.concat(suffix);
System.out.println("Suffix: " + wordSuffix);
}else{
firstLetter = suffix.substring(0,1);
wordSuffix = suffix.concat(firstLetter).concat(word);
System.out.println("Suffix: " + wordSuffix);
}
}else{
System.out.println("Original String: " + line);
wordPlural = word.concat("GH");
System.out.println("Plural: " + wordPlural);
wordSuffix = word.concat(suffix);
System.out.println("Suffix: " + wordSuffix);
}
}else{
if (vowels.contains(wordR.substring(1,2)) == true) {
for (int i = 0; i < wordR.length(); i++) {
if (vowels.contains(wordR.substring(i, i+1))) {
leftmostIndex++;
}else{
break;
}
}
System.out.println("Original String: " + line);
lastLetter = wordR.substring(0,1);
wordPlural = word;
wordPlural = wordPlural.concat(lastLetter);
wordPlural = wordPlural.concat("H");
System.out.println("Plural: " + wordPlural);
if (vowels.contains(suffixR.substring(1,2)) == false) {
vowelSeries = word.substring(word.length()-leftmostIndex, word.length());
vowelSeries = vowelSeries.substring(1, vowelSeries.length());
wordSuffix = wordSuffix.substring(0, leftmostIndex);
wordSuffix = wordSuffix.concat(vowelSeries);
wordSuffix = wordSuffix.concat(suffix);
System.out.println("Suffix: " + wordSuffix);
}else{
firstLetter = suffix.substring(0,1);
wordSuffix = suffix.concat(firstLetter).concat(word);
System.out.println("Suffix: " + wordSuffix);
}
}else{
System.out.println("Original String: " + line);
wordPlural = word.substring(0, word.length()-1);
wordPlural = wordPlural.concat("G");
System.out.println("Plural: " + wordPlural);
if (vowels.contains(suffixR.substring(1,2)) == false) {
firstLetter = suffix.substring(0,1);
wordSuffix = wordSuffix.concat(firstLetter);
wordSuffix = wordSuffix.concat(suffix);
System.out.println("Suffix: " + wordSuffix);
}else{
suffix = suffix.substring(1, suffix.length());
wordSuffix = word;
wordSuffix = word.concat(suffix);
System.out.println("Suffix: " + wordSuffix);
}
}
}
}
}
}
Re: Newbie JCreator NoSuchElementException Help!
I believe I have a solution to your problem. Instead of using the import statement you did for the infile, try doing this:
Add these three import statements:
import java.util.*;
import java.io.*;
import java.lang.*;
Use this for importing your file
Scanner infile = new Scanner(new File ("Vowels.txt"));
from there use statements such as
int x = infile.nextInt();
String s = infile.nextLine();
and others to call your text file. It leaves much less room for error.
Quote:
Originally Posted by
Sly Cooper
I'm trying to run a project that involves using no global variables other than the import ones seen below. It's supposed to read a text file with three lines of words. (I've attached it) and then scan them and add a certain suffix or turn the words plural depending on their endings. I have the project running but it seems that it won't read the next line in the input file, and gives me this error.
--------------------------------------------------------------------------
XQAC ZVM
Original String: XQAC ZVM
Plural: XQACCH
Suffix: XQCZVM
Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenize r.java:332)
at VowelsRUsNoGlobals2.main(VowelsRUsNoGlobals2.java: 47)
Process completed.
--------------------------------------------------------------------------
The output should look like that first bit of the out put but with three different lines. I tried following the NoSuchElementException, but I am new and still having trouble. Here is the code, any and all help is greatly appreciated!
--------------------------------------------------------------------------
import java.io.*;
import java.util.StringTokenizer;
import java.util.HashSet;
public class VowelsRUsNoGlobals2
{
public static void main (String args[]) throws IOException
{
String suffix = new String();
String suffixR = new String();
String word = new String();
String wordR = new String();
String wordPlural = new String();
String wordSuffix = new String();
String firstLetter = new String();
String lastLetter = new String();
String testLetter = new String();
String vowelSeries = new String();
String consonantSeries = new String();
String line = new String();
FileInputStream inFile;
InputStreamReader inReader;
BufferedReader reader;
StringTokenizer strTkn;
HashSet vowels = null;
int leftmostIndex = 0;
inFile = new FileInputStream ("C://!!VHSAPCSData/Vowels.txt");
inReader = new InputStreamReader (inFile);
reader = new BufferedReader (inReader);
line = null;
line = reader.readLine();
strTkn = new StringTokenizer(line);
while (line != null) {
if (line == null) {
break;
}
word = strTkn.nextToken();
suffix = strTkn.nextToken();
System.out.println (line);
vowels = new HashSet();
vowels.add("A");
vowels.add("C");
vowels.add("S");
vowels.add("L");
wordPlural = word;
wordSuffix = word;
for (int i = 0; i < word.length(); i++) {
wordR = word.substring(i, i+1) + wordR;
}
for (int i = 0; i < suffix.length(); i++) {
suffixR = suffix.substring(i, i+1) + suffixR;
}
if (vowels.contains(wordR.substring(0,1)) == false) {
if (vowels.contains(wordR.substring(1,2)) == false) {
lastLetter = wordR.substring(0,1);
wordPlural = wordR.concat(lastLetter);
wordPlural = wordR.concat("H");
System.out.print("plural: " + wordR);
if (vowels.contains(suffixR.substring(1,2)) == false) {
vowelSeries = word.substring(word.length()-leftmostIndex, word.length());
vowelSeries = vowelSeries.substring(1, vowelSeries.length());
wordSuffix = wordSuffix.substring(0, leftmostIndex);
wordSuffix = wordSuffix.concat(vowelSeries);
wordSuffix = wordSuffix.concat(suffix);
System.out.println("Suffix: " + wordSuffix);
}else{
firstLetter = suffix.substring(0,1);
wordSuffix = suffix.concat(firstLetter).concat(word);
System.out.println("Suffix: " + wordSuffix);
}
}else{
System.out.println("Original String: " + line);
wordPlural = word.concat("GH");
System.out.println("Plural: " + wordPlural);
wordSuffix = word.concat(suffix);
System.out.println("Suffix: " + wordSuffix);
}
}else{
if (vowels.contains(wordR.substring(1,2)) == true) {
for (int i = 0; i < wordR.length(); i++) {
if (vowels.contains(wordR.substring(i, i+1))) {
leftmostIndex++;
}else{
break;
}
}
System.out.println("Original String: " + line);
lastLetter = wordR.substring(0,1);
wordPlural = word;
wordPlural = wordPlural.concat(lastLetter);
wordPlural = wordPlural.concat("H");
System.out.println("Plural: " + wordPlural);
if (vowels.contains(suffixR.substring(1,2)) == false) {
vowelSeries = word.substring(word.length()-leftmostIndex, word.length());
vowelSeries = vowelSeries.substring(1, vowelSeries.length());
wordSuffix = wordSuffix.substring(0, leftmostIndex);
wordSuffix = wordSuffix.concat(vowelSeries);
wordSuffix = wordSuffix.concat(suffix);
System.out.println("Suffix: " + wordSuffix);
}else{
firstLetter = suffix.substring(0,1);
wordSuffix = suffix.concat(firstLetter).concat(word);
System.out.println("Suffix: " + wordSuffix);
}
}else{
System.out.println("Original String: " + line);
wordPlural = word.substring(0, word.length()-1);
wordPlural = wordPlural.concat("G");
System.out.println("Plural: " + wordPlural);
if (vowels.contains(suffixR.substring(1,2)) == false) {
firstLetter = suffix.substring(0,1);
wordSuffix = wordSuffix.concat(firstLetter);
wordSuffix = wordSuffix.concat(suffix);
System.out.println("Suffix: " + wordSuffix);
}else{
suffix = suffix.substring(1, suffix.length());
wordSuffix = word;
wordSuffix = word.concat(suffix);
System.out.println("Suffix: " + wordSuffix);
}
}
}
}
}
}
Re: Newbie JCreator NoSuchElementException Help!
Quote:
Originally Posted by
luckymasie
I believe I have a solution to your problem. Instead of using the import statement you did for the infile, try doing this:
Add these three import statements:
import java.util.*;
import java.io.*;
import java.lang.*;
It is never necessary to import any class from the java.lang package, as they are automatically imported.
Please don't post wrong advice, and please don't resurrect old threads. This one's past it's 'best by' date by over a year.
db
THREAD CLOSED