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 374,011 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,753 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
Views: 234 | Replies: 2
Reply
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  
Join Date: Dec 2007
Location: Greece
Posts: 383
Reputation: javaAddict is on a distinguished road 
Rep Power: 1
Solved Threads: 35
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Whiz

Re: Word and Character counting (small problem)

  #2  
May 15th, 2008
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.
I AM the 12th CYLON
Reply With Quote  
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

Re: Word and Character counting (small problem)

  #3  
May 15th, 2008
Hey thanks a lot! I will try this when I get home and update you with the results
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Java Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 10:53 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC