Print the total number of string characters

My question is, what method should I use if I want to get the total number of characters entered by the user? without using arrays, I tried using .length (), but it did not return all the characters from the first and last name, it only returned the first name.

Here is an example of my code. (Please don't laugh, I'm really new to programming :))

System.out.print("Enter your first and last name: "); String yourName = keyboard.next(); System.out.println("Your name has a total of " + yourName.length() + " numbers"); 

what happened if i enter, say "Neo Matrix", he will return only 3.

I appreciate any help. thanks!

+6
source share
2 answers

The next() method only reads the first word of your name. Instead, try nextLine() .

+9
source

You can use Scanner.nextLine() to read an entire line.

next() performs String tokenization according to spaces, so you only get the first word - as expected.

+3
source

Source: https://habr.com/ru/post/913158/


All Articles