Ok well apart from a little inconvenience, my program runs almost flawlessly given the guidelines of the assignment.

I'll tell my problem first and right after paste my code:

When you type in the sentence to be analyzed, only the punctuation mark at the very end doesn't get counted towards the character count of the last word, but let's say if I type in a "?" in the middle of the sentence it should be counted as a word.

So, for example "someWord." should be 9 characters if it's in the middle of a sentence and 8 characters if it marks the end of the sentence.

My code:

import java.util.Scanner;
 
public class Assignment_2 
{
 
    public static void main (String args[]) 
    	
    {   
    	
      Scanner userInput = new Scanner(System.in);
    	
      String a = "";
      System.out.println ("Please enter the sentence to analyse");
      a = userInput.nextLine();
      System.out.println ("");
      System.out.println ("Here are some statistics on your sentence:\n");
      
	  int textLength = a.length();
	  int numberOfWords = 0;
	  int countChar = 0;
	  int lengthOfLongestWord = -999;
	  int lengthOfShortestWord = 999;
      int lengthOfCurrentWord = 0;
	  boolean isWord = false;
 
		for (int i=0;i<textLength;i++)
		{
		   
		   if (a.charAt(i) == ' ' || a.charAt(i) == '.' || a.charAt(i) == '?' || a.charAt(i) == '!')  
		   {      
			  numberOfWords++;
			  isWord = true;
		   }
		   
		   else 			
		   {	
			  countChar++;
			  lengthOfCurrentWord++;
		   }
		
			  	
		    
		   while (isWord)
		    {
		     System.out.println("Word " + numberOfWords + " has " + countChar + " characters.");
		     
		     isWord = false;
		    
		     countChar = 0;
		     	
		     if (lengthOfCurrentWord > lengthOfLongestWord)
			 lengthOfLongestWord = lengthOfCurrentWord;
			  
			 if (lengthOfCurrentWord < lengthOfShortestWord)
			 lengthOfShortestWord = lengthOfCurrentWord;
			  			
			  			
			lengthOfCurrentWord = 0;
			  				  		
		    }
		}	
		 	System.out.println("");
		 	System.out.println("There are " + numberOfWords + " words.");
		 	System.out.println("The longest word has " + lengthOfLongestWord + " characters.");
		 	System.out.println("The shortest word has " + lengthOfShortestWord + " characters.");
		 	
    	}
    }

If I try to analyze (textLength-1, hence the last character of the sentence) in the if/else statement I get a horrible result because it analyzes the last character for every i.

So basically I need a punctuation mark to be counted if it's in the middle of a sentence, and not be counted if it's at the end... hope that makes sense.

Any suggestions?

String a = "";
System.out.println ("Please enter the sentence to analyse");
a = userInput.nextLine();


if (a.length!=0) {
  if ( (a.charAt(a.length-1) == '.') || (a.charAt(a.length-1) == '!') || 
            (a.charAt(a.length-1) == '?') || (a.charAt(a.length-1) == ' ') )  {
      a = a.substring(0,a.length-1);
  }
}


System.out.println ("");
System.out.println ("Here are some statistics on your sentence:\n");
int textLength = a.length();
int numberOfWords = 0;
int countChar = 0;
int lengthOfLongestWord = -999;
int lengthOfShortestWord = 999;
// . . .

So instead of getting what the user enters, you omit the last character (if it is a punctuation mark)

If you check the API: String there are methods for the String class like:
int indexOf(String str) ;
int indexOf(String str, int fromIndex);
In case they help you.

Hey thanks a lot! I will try this when I get home and update you with the results :)

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