Java: write method that accpets string object as argument and returns word count -


the assignment java write method accepts string objects argument , returns number of words contains. demonstrate method in program asks user input string , passes method. number of words should displayed in screen. know close there errors.

public class wordcounter {      //asks , gets users input here     private static string getinput(scanner in)     {         string input;          //imported scanner here         scanner in = new scanner(system.in);          //ask user enter string here         system.out.println("enter string here: ");         input = in.nextline();          //create if/else statment find out if user entered input         if(input.length() > 0)         {             getinput(input);    //get word count         }         else         {             system.out.println("error -- must enter string!");             system.out.println("enter string here: ");             input = in.nextline();         }          return input;      }   //close public static string getinput here        //calculates number of words user inputs     public static int getwordcount(string input)     {         string[] result = input.split(" ");          return result.length;      }   //close public static int getwordcount here             //prints out number of words users input in string above     public static void main(string[] args)     {          //print out number of words within users string here         system.out.println("the number of words in string are: " + counter);      }   //close public static void main string args here 

i fixed quite few parts of code, , commented changed something.

import java.util.scanner;  public class wordcounter { //asks , gets users input here private static string getinput(scanner in) // java case-sensitive. string !=string {     string input;      //imported scanner here     // scanner in = new scanner(system.in); // cannot redefine variable in same scope, un-needed     //ask user enter string here     system.out.println("enter string here: ");     input = in.nextline();      //create if/else statment find out if user entered input     if(input.isempty()) // better form     {         system.out.println("error -- must enter string!");        return getinput(in);    //get input again (it's bad form use recursion here, not technically wrong)     }     else     {         return input;     } }   //close public static string getinput here    //calculates number of words user inputs private static int getwordcount(string input) {     string[] result = input.split(" ");     return result.length; }   //close public static int getwordcount here         //prints out number of words users input in string above public static void main(string[] args) {     //print out number of words within users string here     string input = getinput(new scanner(system.in)); //you needed call these methods!     int counter = getwordcount(input);     system.out.println("the number of words in string are: " + counter); }   //close public static void main string args here  } 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -