Results 1 to 6 of 6
  1. #1
    wfsteadman is offline Member
    Join Date
    Jan 2013
    Location
    Texas
    Posts
    45
    Rep Power
    0

    Question Can't figoure out where the count is off

    Good morning folks,
    I have created a Pig Latin Translator and when I run it, I get an ArrayIndexOutOfBoundsException: 12

    When I input a sentence: I was typing this: Dave is 45, fred is 43
    Then I click the Translate Button, nothing happens, but when I click it again, I get the error:

    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
    at StringManipulation.PigLatinClass.translation(PigLa tinClass.java:35)
    at StringManipulation.GUIPanel$ButtonTranslate.action Performed(GUIPanel.java:90)

    I have attached my program as a zipped file and also put the different classes below.

    Could someone take a look at see where I am possibly messing up the counts.
    Any assistance would be greatly appreciated.

    Java Code:
    package StringManipulation;
    
    import javax.swing.JFrame;
    
    public class StringManipulationApp
    {
    
      public static void main(String[] args)
      {
       GUIPanel gp = new GUIPanel();
             gp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             gp.setSize(500,300);
             gp.setVisible(true); 
      }
    }
    Java Code:
    package StringManipulation;
    
    import java.util.*;
    import java.util.regex.Pattern;
    
    public class PigLatinClass
    {
    
      private String pigLatinCase;
      public int countToken = 0;
      private String start;
      private String end;
      private char charHolder;
      private int punctuation = 0;
    
      public void translation(String inputs)
      {
        StringTokenizer parseSentence = new StringTokenizer(inputs, ",.:;()[]{}\"!@#$%^&* ", true);
        int tokenCount = parseSentence.countTokens();
    
        //Create array to hold tokenized values inclusive of puncuation
        String[] breakSentence = new String[tokenCount];
    
        //Populate array breakSentence to hold the tokenized values for use
        while (parseSentence.hasMoreTokens())
        {
    
          breakSentence[countToken] = parseSentence.nextToken();
          countToken++;
        }
    
    
    
        for (int ctr = 0; ctr < tokenCount; ctr++)
        {
          boolean checkCase = Character.isUpperCase(breakSentence[ctr].charAt(0));
    
          punctuation = 0;
    
          int value = firstVowelLoc(breakSentence[ctr]);
          if (Pattern.matches("[,.:;(){}\"'!@#$%^&* ]", breakSentence[ctr]))
          {
            punctuation = 1;
          }
    
          if (value == 0 && punctuation < 1)
          {
            breakSentence[ctr] += "way";
            breakSentence[ctr] = breakSentence[ctr].toLowerCase();
            if (checkCase == true)
            {
              String firstChar = breakSentence[ctr].substring(0, 1).toUpperCase();
              String lastPart = breakSentence[ctr].substring(1);
              breakSentence[ctr] = firstChar + lastPart;
            }
          } else if (value == -1)
          {
            breakSentence[ctr] = breakSentence[ctr];
          } else if (value == 0 && punctuation == 1)
          {
            breakSentence[ctr] = breakSentence[ctr];
          } else
          {
            start = breakSentence[ctr].substring(0, value);
            end = breakSentence[ctr].substring(value);
            breakSentence[ctr] = end + start + "ay";
            breakSentence[ctr] = breakSentence[ctr].toLowerCase();
          }
          if (checkCase == true)
          {
            String firstChar = breakSentence[ctr].substring(0, 1).toUpperCase();
            String lastPart = breakSentence[ctr].substring(1);
            breakSentence[ctr] = firstChar + lastPart;
          }
        }
    
        for (int finCount = 0; finCount < tokenCount; finCount++)
        {
          pigLatinCase += breakSentence[finCount];
        }
      }
    
      public static int firstVowelLoc(String word)
      {
        for (int i = 0; i < word.length(); i++)
        {
          if (word.charAt(i) == 'a' || word.charAt(i) == 'e'
                  || word.charAt(i) == 'i' || word.charAt(i) == 'o'
                  || word.charAt(i) == 'u' || word.charAt(i) == 'A'
                  || word.charAt(i) == 'E' || word.charAt(i) == 'I'
                  || word.charAt(i) == 'O' || word.charAt(i) == 'U')
          {
            return i;
          }
    
          if (word.charAt(0) == '0' || word.charAt(0) == '1' || word.charAt(0) == '2' || word.charAt(0) == '3' || word.charAt(0) == '4' || word.charAt(0) == '5' || word.charAt(0) == '6'
                  || word.charAt(0) == '7' || word.charAt(0) == '8' || word.charAt(0) == '9')
          {
            return -1;
          }
    
        }
        return 0;
      }
    
      public String getPigLatinCase()
      {
        return pigLatinCase;
      }
    }
    Java Code:
    package StringManipulation;
    
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.event.ItemListener;
    import java.awt.event.ItemEvent;
    import javax.swing.*;
    
    public class GUIPanel extends JFrame
    {
    
      private JPanel layoutPanel;
      private JLabel instructions;
      private JTextField inputField;
      private JTextArea outputArea;
      private String inputString = "";
      private String outputString = "";
      private JButton translate;
      private JButton clear;
      public PigLatinClass pl = new PigLatinClass();
    
      public GUIPanel()
      {
        super("Pig Latin Translator");
        setLayout(new FlowLayout());
    
    
        instructions = new JLabel("Input Sentence below and click translate");
        add(instructions);
        inputField = new JTextField(40);
        add(inputField);
        translate = new JButton("Translate");
        add(translate);
        clear = new JButton("Clear");
        add(clear);
        outputArea = new JTextArea(5, 40);
        add(outputArea);
    
        GridBagConstraints layout = new GridBagConstraints();
        layout.insets = new Insets(3, 3, 3, 3);
        layoutPanel = new JPanel(new GridBagLayout());
        layout.gridx = 0;
        layout.gridy = 0;
        layoutPanel.add(instructions, layout);
        layout.gridx = 0;
        layout.gridy = 1;
        layoutPanel.add(inputField, layout);
        layout.gridx = 0;
        layout.gridy = 2;
        layoutPanel.add(translate, layout);
        layout.gridx = 0;
        layout.gridy = 3;
        layoutPanel.add(clear, layout);
        layout.gridx = 0;
        layout.gridy = 4;
        layoutPanel.add(outputArea, layout);
        add(layoutPanel);
    
        ButtonTranslate translateHandler = new ButtonTranslate();
        translate.addActionListener(translateHandler);
    
        ButtonClear clearHandler = new ButtonClear();
        clear.addActionListener(clearHandler);
      }
    
      private class ButtonTranslate implements ActionListener
      {
    
        public void actionPerformed(ActionEvent event)
        {
          if (inputField.getText().isEmpty())
          {
            outputArea.setText("Nothing there");
          } else
          {
            String test = inputField.getText();
            pl.translation(test);
            pl.getPigLatinCase();
          }
        }
      }
    
      private class ButtonClear implements ActionListener
      {
    
        public void actionPerformed(ActionEvent event)
        {
          outputArea.setText("");
          inputField.setText("");
          inputField.requestFocus();
        }
      }
    }
    Attached Files Attached Files

  2. #2
    Join Date
    Apr 2008
    Location
    Fort Wayne, Indiana
    Posts
    62
    Rep Power
    0

    Default Re: Can't figoure out where the count is off

    You're getting the array out of bounds exception because countToken is never reset to 0. You should be setting it to 0 every time you enter this function. Personallly i dont understand why you have it as a global variable, moving it into the function would also fix this issue.

  3. #3
    Join Date
    Apr 2008
    Location
    Fort Wayne, Indiana
    Posts
    62
    Rep Power
    0

    Default Re: Can't figoure out where the count is off

    Your only other issue is that you never set your output to the screen in your button listener. The output has to go somewhere.

  4. #4
    wfsteadman is offline Member
    Join Date
    Jan 2013
    Location
    Texas
    Posts
    45
    Rep Power
    0

    Default Re: Can't figoure out where the count is off

    Thank you for the pointer (no pun intended). I moved it inside the method and then in the button listener i added: outputArea.setText(pl.getPigLatinCase()); and it works great. Thanks again so much.

  5. #5
    Join Date
    Apr 2008
    Location
    Fort Wayne, Indiana
    Posts
    62
    Rep Power
    0

    Default Re: Can't figoure out where the count is off

    Hurray! When i ran this myself i put the input as "this is a test" and got the result "nullisthay isway away esttay". You may want to look into why "null" is at the beginning. Or maybe i did something wrong ;)

  6. #6
    wfsteadman is offline Member
    Join Date
    Jan 2013
    Location
    Texas
    Posts
    45
    Rep Power
    0

    Default Re: Can't figoure out where the count is off

    the null came because when I initialized the pigLatinCase, I didn't give it initial value of "". Fixed that so good to go.

Similar Threads

  1. count every 45 records and prfix the count number
    By dkr786 in forum New To Java
    Replies: 8
    Last Post: 02-15-2013, 08:31 PM
  2. purchase count
    By FOX427 in forum New To Java
    Replies: 2
    Last Post: 04-29-2012, 06:55 PM
  3. Count down
    By tj_wolf2 in forum New To Java
    Replies: 8
    Last Post: 03-14-2011, 06:07 PM
  4. count id's using js.
    By henkiej in forum New To Java
    Replies: 1
    Last Post: 05-02-2010, 12:28 PM
  5. Getting row count
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-11-2008, 09:49 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
  •