我试图在用户输入的字符串中找到 space,但是当我使用 indexof(" ") 时,它的值是 returns -1,我该如何解决这个问题?

I am trying to find a space in the string entered by the user but when I use indexof(" ") it returns -1 for the value, how do I fix this?

System.out.println("请输入您的名字和姓氏,用 space 分隔:"); 尝试(扫描仪扫描=新扫描仪(System.in)){ 字符串 s1 = scan.next();

            int space = s1.indexOf(" ");
        
                String First_Name = s1.substring(0,space);
                String init1 = First_Name.substring(0,1);
                String Last_Name = s1.substring(space+1,s1.length());
                String init2 = Last_Name.substring(0,1);
        
        
                    int First_N = First_Name.length();
                    int Last_N = Last_Name.length();
        
                        System.out.println("You entered the name "+s1+".");
                        System.out.println("Your first name "+First_Name+": has "+First_N+" characters.");
                        System.out.println("Your last name "+Last_Name+": has "+Last_N+" characters.");
                        System.out.println("Your initials are "+init1+init2+".");

你应该使用

String s1 = scan.nextLine();

而不是

String s1 = scan.next();