Hello, I am new to Java and would like some help. I have to write a small application that converts a user inputted sentence to a string array. The string is 'cleaned' from special characters before splitted into an array (Only numbers and alphabetical characters are allowed). I have managed to clean the string so far but when I am trying to count the elements (words) the result does not match. I wrote the following:

String strNoSymbSentence = objOne.lettersOnly(strInpSentence);

            String[] clnArraySentence = strNoSymbSentence.split(" ");

            for (int i = 0; i < clnArraySentence.length; i++)
            {
                for (int j = 0; j < clnArraySentence[i].length(); j++)
                {
                    if (Character.isLetterOrDigit(clnArraySentence[i].charAt(j))
                            & clnArraySentence[i].length() == 1)
                    {
                        intOneLetterWord++;
                    } else if (Character.isLetterOrDigit(clnArraySentence[i].charAt(j))
                            & clnArraySentence[i].length() == 2)
                    {
                        intTwoLetterWord++;
                    } else if (Character.isLetterOrDigit(clnArraySentence[i].charAt(j))
                            & clnArraySentence[i].length() == 3)
                    {
                        intThreeLetterWord++;
                    } else if (Character.isLetterOrDigit(clnArraySentence[i].charAt(j))
                            & clnArraySentence[i].length() == 4)
                    {
                        intFourLetterWord++;
                    }
                }
            }

            JOptionPane.showMessageDialog(null, strInpSentence + "\n" + strNoSymbSentence
                    + "\n\n1 Lettered Word - " + intOneLetterWord
                    + "\n2 Lettered Word - " + intTwoLetterWord
                    + "\n3 Lettered Word - " + intThreeLetterWord
                    + "\n4 Lettered Word - " + intFourLetterWord
                    + "\n5 Lettered Word - " + intFiveLetterWord
                    + "\n6 Lettered Word - " + intSixLetterWord
                    + "\n7 Lettered Word - " + intSevenLetterWord
                    + "\n8 Lettered Word - " + intEightLetterWord
                    + "\n9 Lettered Word - " + intNineLetterWord
                    + "\n10 Lettered Word - " + intTenLetterWord
                    + "\n11 Lettered Word - " + intElevenLetterWord
                    + "\nnth Lettered Word - " + intMoreLetterWord,
                    "Sentence Breakdown", 1);

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.