Results 1 to 8 of 8
  1. #1
    wizar is offline Member
    Join Date
    Nov 2010
    Posts
    18
    Rep Power
    0

    Default Keep on getting one error or the other

    I've been either getting NoSuchElementException or NullPointerException errors when working with this code. Right now, I left it so that I have NoSuchElementException errors.

    For this program, I am getting a sentence input from the user, and for every word that has "ui" in it, I want to replace it with a 'w'. (instead of "quick", I need to have "qwick" as a result.

    Can anyone help me understand this?

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class LetterChange
    {
      public static void main (String [] args) throws IOException
      {
        BufferedReader objReader = new BufferedReader (new InputStreamReader (System.in));
        
        System.out.println ("Enter a sentence to change, enter 'exit' to exit:");
        String input = objReader.readLine();
        String result = "";
        String test= input;
        
        LetterSwitch changed = new LetterSwitch (test, result);// error here
        while (!input.equals("exit"))
        {
          System.out.println(changed.getResult());
          System.out.println ("Enter a sentence to change, enter 'exit' to exit: ");
          input = objReader.readLine();
        }
        if (input.equals("exit"))
        {
          System.out.println("End.");
        } 
      } 
    }
    
    
    class LetterSwitch
    {
      private String test;
      private String result= "";
      LetterSwitch(String subject, String differ)
      {
        test = subject;
        result = differ;
        addLetter (test); // error here
      }
      
      public void addLetter (String test)
      {
        StringTokenizer line = new StringTokenizer (test);
        StringBuffer changes = new StringBuffer ();
        
        while (line.hasMoreTokens())
        {
          String word= line.nextToken();
          int x = word.indexOf('u'); 
          if (word.charAt(x + 1) == 'i' )
          {
            word = word.replaceFirst ("ui", "w");
          }
          else 
          {
            word = word;
          }
        }
        changes.append(line.nextToken()); //error on this line
        result = changes.toString();
      }
      String getResult()
      {
        return result;
      }
    }
    These are the error messages I get:

    java.util.NoSuchElementException
    at java.util.StringTokenizer.nextToken(Unknown Source)
    at LetterSwitch.addLetter(LetterChange.java:59)
    at LetterSwitch.<init>(LetterChange.java:38)
    at LetterChange.main(LetterChange.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Java Code:
        while (line.hasMoreTokens())
        {
          String word= line.nextToken();
          int x = word.indexOf('u'); 
          if (word.charAt(x + 1) == 'i' )
          {
            word = word.replaceFirst ("ui", "w");
          }
          else 
          {
            word = word;
          }
        }
        changes.append(line.nextToken()); //error on this line
        result = changes.toString();
    While there are more tokens, get next token. When there are no more tokens, get the next token. See what you did there?
    EDIT: Also, that else is totaly extraneous, what is word = word supposed to acomplish? What happens if you come across a text like this:
    there is nothing for u
    Take a look at your method and see what happens when you hit the single letter word "u".
    Last edited by m00nchile; 01-01-2011 at 06:39 PM.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    wizar is offline Member
    Join Date
    Nov 2010
    Posts
    18
    Rep Power
    0

    Default

    Ok, thanks for pointing that out.
    Unless the character after the u is an i, the word should remain the same. I guess it's not necessary?

    And that gets a StringIndexOutOfBounds error, should I change the else statement then?

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Yes, delete the else statement altogether. Also, read up on the indexOf() method, specificaly what it returns if the character was not found in the string. Also, a simple modification to your
    Java Code:
    if ([B]x < word.length() &&[/B] word.charAt(x + 1) == 'i')
    if statement takes care of the string index out of bounds exception as well.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  5. #5
    wizar is offline Member
    Join Date
    Nov 2010
    Posts
    18
    Rep Power
    0

    Default

    Thanks for your help, I appreciate it. :)
    I tried changing the if statement, but I got the index out of bounds error again when I use 'u' as a single character in my input. So I'm a bit confused now.

    Here are the error messages I got:
    java.lang.StringIndexOutOfBoundsException: String index out of range: 1
    at java.lang.String.charAt(Unknown Source)
    at LetterSwitch.addLetter(LetterChange.java:50)
    at LetterSwitch.<init>(LetterChange.java:38)
    at LetterChange.main(LetterChange.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)

    EDIT: I fixed my method so I'm not getting this error anymore. Can anyone help me understand why I'm getting the other errors?
    Last edited by wizar; 01-02-2011 at 02:01 AM.

  6. #6
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    What errors are you still getting?
    Ever seen a dog chase its tail? Now that's an infinite loop.

  7. #7
    wizar is offline Member
    Join Date
    Nov 2010
    Posts
    18
    Rep Power
    0

    Default

    The errors I originally had in my first post.
    My bad, I should have been more specific.
    Thanks again for your help. :)

  8. #8
    wizar is offline Member
    Join Date
    Nov 2010
    Posts
    18
    Rep Power
    0

    Default

    I am currently working with adding a set method, which seems to eliminate the errors. I'm just having problem with getting an output.

    Is adding a set method wrong, or is something wrong with the code without the set method?
    Here's the code I have now:


    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class LetterChange
    {
      public static void main (String [] args) throws IOException
      {
        BufferedReader objReader = new BufferedReader (new InputStreamReader (System.in));
        
        System.out.println ("Enter a sentence to change, enter 'exit' to exit:");
        String input = objReader.readLine();
        String result = "";
        String test= input;
        String word = "";
        
        StringTokenizer line = new StringTokenizer(test);
        LetterSwitch changed = new LetterSwitch (test, result, word);
        changed.setWord(line.nextToken());
        
        while (!input.equals("exit"))
        {
          System.out.println(changed.getResult());
          System.out.println ("Enter a sentence to change, enter 'exit' to exit: ");
          input = objReader.readLine();
        }
        if (input.equals("exit"))
        {
          System.out.println("End.");
        } 
      } 
    }
    
    
    class LetterSwitch
    {
      private String test;
      private String result= "";
      private String word;
      LetterSwitch(String subject, String differ, String term)
      {
        test = subject;
        result = differ;
        word = term;
        addLetter (test, result, word);
      }
      
      String setWord (String term)
      {
        String word = term;
        return word;
      }
      public void addLetter (String test, String result, String word)
      {
        StringTokenizer line = new StringTokenizer (test);
        StringBuffer changes = new StringBuffer ();
        word = line.nextToken();
        while (line.hasMoreTokens())
        {
          int x = word.indexOf('u'); 
          if ((x < word.length()) && (word.charAt(x + 1) == 'i' ))
          {
            word = word.replaceFirst ("ui", "w");
          }
          changes.append(line.nextToken()); 
        }
        
        result = changes.toString();
      }
      String getResult()
      {
        return result;
      }
    }
    Last edited by wizar; 01-03-2011 at 12:39 AM.

Similar Threads

  1. java out of memory error-heap space error
    By elsanthosh in forum NetBeans
    Replies: 4
    Last Post: 06-15-2010, 09:31 AM
  2. Replies: 1
    Last Post: 03-10-2010, 01:53 PM
  3. Replies: 1
    Last Post: 12-05-2009, 07:28 AM
  4. Replies: 1
    Last Post: 10-26-2009, 04:38 PM
  5. Replies: 3
    Last Post: 01-23-2009, 08:53 AM

Posting Permissions

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