java - How do I create a user-defined method that returns the length of a string? -


i trying write program takes user's input , outputs number of characters typed in. have creating method calculates amount of characters, call method in main output results. encouraged use loop, don't see how work. can calculate number of characters using length(), can't figure out how make method work. have far:

public static void main(string[] args) {  scanner scnr = new scanner(system.in);   string userinput = "";   system.out.println("enter sentence: ");  system.out.print("you entered: ");   userinput = scnr.nextline();  system.out.println(userinput);     return;  }   public static int getnumofcharacters(int usercount) {    int = 0;   string userinput = "";   usercount = userinput.length();   return usercount;  } } 

my method not returning length of string, gives me 0 or error.

right now, never calling "getnumofcharacters" method in main. way java programs work, calling main method , executing line per line lies there. need call method inside main method. on other hand, should stirng parameter, can length. this:

public static void main(string[] args) {     scanner scnr = new scanner(system.in);      string userinput = "";      system.out.println("enter sentence: ");       userinput = scnr.nextline();     system.out.print("you entered: ");     system.out.println(userinput);      int leninput = getnumofcharacters(userinput);      system.out.println("the length was: "+leninput+" characters"); }  public static int getnumofcharacters(string userinput) {     int len = userinput.length();     return len; } 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -