Friday 15 March 2013

java - write a code to find the number of words in a string using methods -


I was watching and I can not find anywhere how to count a word using the 3 methods I The code looks so far. I am losing on using the method, I can do it without the use of different methods and I am using only one. help please!

  public static zero main (string [] args) {scanner = new scanner (System.in); System.out.print ("Enter a string:"); String s = in.nextLine (); If (s.length ()> gt; {getInputString (S); } Else {System.out.println ("Error - string should not be blank."); System.out.print ("Enter a string:"); S = in.nextLine (); } // Fill the body with your code} // Given the scanner, give the user a signal for the string. If the user enters an empty // string, then report an error message and ask for an empty string without. // Return to String Calling Program Private Static String getInputString (Strings) {int count = getWordCount (); While (int i = 0; i   

Edit: I changed the code to

  Private Static String in getInputString (String s) {string word = meet; Return word Long (); } Private Static Inter wordCount (strings) {return s.split (""); }   

But I can not convert the string to an integer.

You have read the name of the method, and what should be implemented within this method To review the comments and return the value.

getInputString should be the method sign:

  Private static string getInputString (scanners) {string input string = ""; // Read the input string from the system // .... returnString; }   

getWordCount method must be signed:

  private static get getWordCount (string input) {int wordCount = 0; // Count the number of words in the input string // ... come back the word; }   

This main method should look something like this:

  public static zero main (string [] args) {/ Instant / call getInputString method of scanner variable ... you have guessed it ... Get input string // Call getWordCount method to get word count // Display word count}  < / Pre>  

No comments:

Post a Comment