Results 1 to 3 of 3
  1. #1
    Sly Cooper is offline Member
    Join Date
    Jan 2011
    Posts
    10
    Rep Power
    0

    Default 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);


    }

    }

    }

    }

    }
    }
    Attached Files Attached Files

  2. #2
    luckymasie is offline Member
    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Default 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 View Post
    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);


    }

    }

    }

    }

    }
    }

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Newbie JCreator NoSuchElementException Help!

    Quote Originally Posted by luckymasie View Post
    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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Java Error: NoSuchElementException
    By xpngamer in forum New To Java
    Replies: 6
    Last Post: 03-19-2009, 07:37 PM
  2. Replies: 5
    Last Post: 02-27-2009, 05:19 AM
  3. Exception in thread "main" java.util.NoSuchElementException
    By vileoxidation in forum New To Java
    Replies: 5
    Last Post: 09-17-2008, 07:29 AM
  4. Replies: 4
    Last Post: 06-08-2008, 02:19 PM
  5. Replies: 5
    Last Post: 05-14-2008, 01:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •