There is a space in the line, but when I run the program, it returns -1, which means that there are no spaces in the line. Here is the code:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String s = scan.next(); System.out.println(s.indexOf(' ')); } }
Scanner.next() returns the next token in the input file, and by default, tokens are separated by spaces. those. s guaranteed to be blank.
Scanner.next()
s
Perhaps you meant String s = scan.nextLine(); ?
String s = scan.nextLine();
This works great for me.
System.out.println("one word".indexOf(' '));
This is because of the following Scanner method. check this
The scanner reads text separated by spaces, which can be a line break, as well as a space character. This is why scan.next () will return a string without spaces. If you need line breaks, use scan.nextLine ()
Try
how scan.next () gets the next "word" that cannot contain spaces.
Use scan.nextLine() because scan.next() will read until it encounters a space (tab, space, input) so that it finishes receiving when it sees a space. You yourself could have guessed it by printing too! to be successful!
scan.nextLine()
scan.next()
Source: https://habr.com/ru/post/912121/More articles:As for the Google Chrome extension, how can I change the style of the table in the popup? - google-chromeDecimal value for approximating the irrational fraction - decimalGolang GAE - intID in mustache structure - google-app-engineRuby Ch9 Tutorial Exercise # 9 - Donβt Allow Admin to Remove Him - tddOpensource or library for implementing a store or application store - open-sourceThe complexity of images / panels / buttons from a menu in JPanel - javacpp cpp error code assembly - c ++Open the device contact list at the touch of a button - androidcheck if [i] = 2 * a [j] exists in unsorted array a? - algorithmAdd doctype to HTML via HTML Agility pack - c #All Articles