7 Replies - 7197 Views - Last Post: 05 April 2011 - 11:33 AM Rate Topic: -----

#1 MrAnimenerd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-April 11

Using StringBuilder and StringTokenizer to convert string text

Posted 05 April 2011 - 09:07 AM

I'm trying to create a program that uses a class and a driver program that converts string text to pig latin using the StringBuilder and StringTokenizer classes. I can get it to print out the message I just typed in, but it doesn't do the conversion; I get an error message like this at run-time:

[email protected]

I've included what I've done in the program, but I don't know how to fix this.

import java.util.StringTokenizer;
public class PigLatinConversion
{
    private String userInput;
    private StringTokenizer strToken;
    private StringBuilder str;
    private StringBuilder pig;
    //private ArrayList<String> token;
    
    public PigLatinConversion(String user)
    {
        this.userInput = user;
        strToken = new StringTokenizer(userInput, " ");
        str = new StringBuilder();
        pig = new StringBuilder();
    }
    
    public void setConversion(String user)
    {
        this.userInput = user;
        strToken = new StringTokenizer(userInput, " ");
        str = new StringBuilder();
        pig = new StringBuilder();
    }
    
    public String getSentenceConversion()
    {
        //String vowel;
        //String consonant;
        //int letter;
        
        while (strToken.countTokens() > 0)
        {
            str = new StringBuilder(strToken.nextToken());
            
            
            
            if(Voweltest(str.charAt(0)))
            {
                str.append("yay");
                
                pig.append(str);
                pig.append(" ");
            }
            else if(Voweltest(str.charAt(1))) 
            {               
                str.append(str.charAt(0));
                
                str.deleteCharAt(0);
                
                str.append("ay");
                
                pig.append(str);
                pig.append(" ");
                
            }
            else if(Voweltest(str.charAt(2)))
            {
                str.append(str.charAt(0));
                str.deleteCharAt(str.charAt(0));
                
                str.append(str.charAt(0));
                str.deleteCharAt(str.charAt(0));
                
                str.append("ay");
                
                pig.append(str);
                pig.append(" ");
            }
            else
            {
                str.append(str.charAt(0));
                str.deleteCharAt(str.charAt(0));
                
                str.append(str.charAt(0));
                str.deleteCharAt(str.charAt(0));
                
                str.append(str.charAt(0));
                str.deleteCharAt(str.charAt(0));
                
                str.append("ay");
                
                pig.append(str);
                pig.append(" ");
            }
               
        }
        
        return pig.toString();
    }
    
public boolean Voweltest(char character)
{
   if(character =='A' || character == 'E' || character == 'I' || character == 'O' || character == 'U')
   {
       return true;
   }else {
       return false;
   }
}
    
}



import java.util.Scanner;
public class PigLatinMain
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        //String input;
        String userSentence = " ";
        
        Scanner keyboard = new Scanner(System.in);
        
        
        
        //System.out.println("Would you like to have your sentence translated to Pig Latin?\n" + "(Yes to run the loop or No to exit)");
        //input = keyboard.next();
        
        while(userSentence.length() > 0)
        {
            System.out.print("Type in a sentence so that it may be converted/translated. ");
            userSentence = keyboard.nextLine();
            
            System.out.print(userSentence + "\n");
            
            PigLatinConversion pl = new PigLatinConversion(userSentence);
            
            System.out.print(pl + "\n");          
           
            
          //  System.out.print("This is your sentence in english: " + userSentence + "\n" + "This is your sentence in pig latin: " + pl.getSentenceConversion());
        }
    }
}



Ignore some of the code that I have quoted out, unless it can help fix the problem.

This post has been edited by MrAnimenerd: 05 April 2011 - 09:08 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Using StringBuilder and StringTokenizer to convert string text

#2 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12226
  • View blog
  • Posts: 45,301
  • Joined: 27-December 08

Re: Using StringBuilder and StringTokenizer to convert string text

Posted 05 April 2011 - 09:15 AM

The PigLatinConversion object does not override the Object toString() method. So when you print it directly, you get the memory location as the output. You do define the getSentenceConversion() method, which returns the String representation of the conversion, which you can print and get human friendly results.
Was This Post Helpful? 0
  • +
  • -

#3 MrAnimenerd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-April 11

Re: Using StringBuilder and StringTokenizer to convert string text

Posted 05 April 2011 - 09:53 AM

View Postmacosxnerd101, on 05 April 2011 - 09:15 AM, said:

The PigLatinConversion object does not override the Object toString() method. So when you print it directly, you get the memory location as the output. You do define the getSentenceConversion() method, which returns the String representation of the conversion, which you can print and get human friendly results.

Well, After posting this, I did some more tinkering with it. I have to it would print using the statement:

System.out.print(pl.getSentenceConversion() + "\n");


But then it gave me an error about my .deleteCharAt() methods after the else statment in my class, I think I fixed that, but now I'm getting this:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 2
at java.lang.AbstractStringBuilder.charAt(AbstractStringBuilder.java:177)
at program14piglatin.PigLatinConversion.getSentenceConversion(PigLatinConversion.java:68)
at program14piglatin.PigLatinMain.main(PigLatinMain.java:40)
Java Result: 1
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12226
  • View blog
  • Posts: 45,301
  • Joined: 27-December 08

Re: Using StringBuilder and StringTokenizer to convert string text

Posted 05 April 2011 - 10:06 AM

The String being parsed has fewer than two characters. That's why this line else if(Voweltest(str.charAt(2))) is causing your Exception.
Was This Post Helpful? 1
  • +
  • -

#5 MrAnimenerd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-April 11

Re: Using StringBuilder and StringTokenizer to convert string text

Posted 05 April 2011 - 10:28 AM

View Postmacosxnerd101, on 05 April 2011 - 10:06 AM, said:

The String being parsed has fewer than two characters. That's why this line else if(Voweltest(str.charAt(2))) is causing your Exception.

Could you explain?
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12226
  • View blog
  • Posts: 45,301
  • Joined: 27-December 08

Re: Using StringBuilder and StringTokenizer to convert string text

Posted 05 April 2011 - 11:29 AM

Strings work just like arrays this way- they are both indexed from 0 through length-1. So if you have "the", it has length 3 with indices 0-2. So if you try to access any index > 2, you will get an IndexOutOfBoundsException.
Was This Post Helpful? 0
  • +
  • -

#7 MrAnimenerd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 05-April 11

Re: Using StringBuilder and StringTokenizer to convert string text

Posted 05 April 2011 - 11:31 AM

View Postmacosxnerd101, on 05 April 2011 - 11:29 AM, said:

Strings work just like arrays this way- they are both indexed from 0 through length-1. So if you have "the", it has length 3 with indices 0-2. So if you try to access any index > 2, you will get an IndexOutOfBoundsException.

Thanks, I really appreciate the help, I was finally able to get the program done.
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12226
  • View blog
  • Posts: 45,301
  • Joined: 27-December 08

Re: Using StringBuilder and StringTokenizer to convert string text

Posted 05 April 2011 - 11:33 AM

Glad I could help! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1