User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 402,612 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,321 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting

Word and Character counting (small problem)

Join Date: May 2008
Posts: 2
Reputation: Alistair1 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Alistair1 Alistair1 is offline Offline
Newbie Poster

Word and Character counting (small problem)

  #1  
May 15th, 2008
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:



  1. import java.util.Scanner;
  2.  
  3. public class Assignment_2
  4. {
  5.  
  6. public static void main (String args[])
  7.  
  8. {
  9.  
  10. Scanner userInput = new Scanner(System.in);
  11.  
  12. String a = "";
  13. System.out.println ("Please enter the sentence to analyse");
  14. a = userInput.nextLine();
  15. System.out.println ("");
  16. System.out.println ("Here are some statistics on your sentence:\n");
  17.  
  18. int textLength = a.length();
  19. int numberOfWords = 0;
  20. int countChar = 0;
  21. int lengthOfLongestWord = -999;
  22. int lengthOfShortestWord = 999;
  23. int lengthOfCurrentWord = 0;
  24. boolean isWord = false;
  25.  
  26. for (int i=0;i<textLength;i++)
  27. {
  28.  
  29. if (a.charAt(i) == ' ' || a.charAt(i) == '.' || a.charAt(i) == '?' || a.charAt(i) == '!')
  30. {
  31. numberOfWords++;
  32. isWord = true;
  33. }
  34.  
  35. else
  36. {
  37. countChar++;
  38. lengthOfCurrentWord++;
  39. }
  40.  
  41.  
  42.  
  43. while (isWord)
  44. {
  45. System.out.println("Word " + numberOfWords + " has " + countChar + " characters.");
  46.  
  47. isWord = false;
  48.  
  49. countChar = 0;
  50.  
  51. if (lengthOfCurrentWord > lengthOfLongestWord)
  52. lengthOfLongestWord = lengthOfCurrentWord;
  53.  
  54. if (lengthOfCurrentWord < lengthOfShortestWord)
  55. lengthOfShortestWord = lengthOfCurrentWord;
  56.  
  57.  
  58. lengthOfCurrentWord = 0;
  59.  
  60. }
  61. }
  62. System.out.println("");
  63. System.out.println("There are " + numberOfWords + " words.");
  64. System.out.println("The longest word has " + lengthOfLongestWord + " characters.");
  65. System.out.println("The shortest word has " + lengthOfShortestWord + " characters.");
  66.  
  67. }
  68. }


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?
AddThis Social Bookmark Button
Reply With Quote  
All times are GMT -4. The time now is 12:41 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC