•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 426,499 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,156 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: 367 | Replies: 2
![]() |
•
•
Join Date: May 2008
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
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:
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?
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:
JAVA Syntax (Toggle Plain Text)
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;
// . . .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
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- memory management in wndows 2000 (Windows NT / 2000 / XP / 2003)
- problem with output values (Assembly)
Other Threads in the Java Forum
- Previous Thread: Creating Hover Effect for JButton
- Next Thread: Case gives 100 errors, please help


Linear Mode