Hello. One races, the user can enter 2 words, and the other races 3, and how to determine how many words he entered? Only str2 can be added, that is, for example, he entered one of the races:

first third

And another race:

first second third

Here for example my code:

Scanner g = new Scanner(System.in); String str1 = g.next(); String str2 = g.next(); String str3 = g.next(); if(str2.isEmpty()) { //у нас только есть str1 и str3 } else { //нам повезло, у нас есть три слова } 

Only it does not work for me.

  • one
    Pretty natural. How do you imagine an empty word? Try to write such a sequence of characters. - avp
  • @timedo Another RA W learn Russian! - Barmaley

1 answer 1

You can take a string, and then break into words

ps words separated by spaces