// need help with this how do i get the for loop to print out just
//the alphabetic words

{
	public static void main(String[] args)

	{
  		String string ;
		int index ;
		String	sentence;
		String 	newSentence; 
		String	word;
		int	startPos;
		int spacePos;
		int wordCount;
		boolean lastWord;
		boolean alphaWord;
		char letter;
		
		System.out.println("Enter a string");
		sentence = EasyIn.getString();
		
		for(index = sentence.length()-1 ;index >= 0 ; index--)
		{
		System.out.print(" "+  sentence.charAt(index) );
		}

		startPos = 0;
		wordCount = 0;
		lastWord = false;
		newSentence = "";
		
		do
		{
			
		
			spacePos = sentence.indexOf(" ", startPos);
			if (spacePos == -1)
			{
				lastWord = true;
				word = sentence.substring(startPos);
			}
			else
			{
				lastWord = false;
				word = sentence.substring(startPos, spacePos);
			}
					
			
			index = 0;
			alphaWord = true;
			while ((index <= word.length()-1) && (alphaWord == true))
			{
				letter = word.charAt(index);
				if ((letter >= 'a' && letter <= 'z') || (letter >= 'A' && letter <= 'Z'))
					index++;
				else
					alphaWord  = false;
			}
	
			
			
			if (alphaWord == true)
				{
					newSentence = newSentence + word + " ";
					wordCount++;
				}
		
			

			startPos = spacePos + 1;
		
		} while (lastWord != true);
		
		System.out.println();
		System.out.println(newSentence);
		System.out.println("The number of alphabetic words is: " + wordCount);
		
	}
	
	
}

Edited 6 Years Ago by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

Use regex to check whether a string is made up of alphanumeric characters or only letters then print out the ones you need.

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