简体   繁体   English

如何在Java字符串中搜索空间?

[英]How to search for space in a Java String?

I am quite new to programming and I am writing this code to count a string (length) to a point when I encounter a space. 我对编程很陌生,我正在编写此代码以在遇到空格时计算字符串(长度)到一个点。 The aim is - when the user enters his/her name AND surname, the program should split the name from surname and count how many letters/characters were there in the name (and surname). 目的是-当用户输入自己的姓名和姓氏时,程序应将姓名与姓氏分开,并计算姓名(和姓氏)中有多少个字母/字符。

My code doesn't seem to reach/execute the " if-statement ", if I enter two strings (name & surname) separated by space (output: Your name is: (empty space) and it has 0 letters. However, if I enter only one string, the if-statement , it gets executed. 如果我输入两个用空格分隔的字符串(名称和姓氏),我的代码似乎无法达到/执行“ if-statement ”(输出:您的名字是:(空空格),并且有0个字母)。但是,如果我只输入一个字符串if-statement ,它将被执行。

What I am doing wrong? 我做错了什么?

My example code: 我的示例代码:

public class Initials {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in); 
    String nameAndSurname, nameOnly;
    int c = 0, count = 0;
    System.out.println("Enter your full name please:");
    nameAndSurname = scan.nextLine();
    int space = nameAndSurname.indexOf(' ');
    for(int x = 0; x<=nameAndSurname.length()-1; x++) {
        c++;
        if(nameAndSurname.indexOf(x) == space) //if there is a space
        { 
            count = c; //how many characters/letters was there before space
            System.out.println(count);
        } 
    }
    nameOnly = nameAndSurname.substring(0, count);
    System.out.println("Your name is: " + nameOnly.toUpperCase() + " and it has " + count + " letters");
    scan.close();
}
 if(nameAndSurname.indexOf(x) == space)

This line isn't doing what you think it is doing. 这条线没有按照您认为的去做。

It's getting a char (character) from the index of x, and comparing it to the value of space. 它从x的索引中获取一个char(字符),并将其与space的值进行比较。 Space is an integer, so you are comparing the character at position x to the integer position of the first space. 空格是整数,因此您要比较位置x处的字符与第一个空格的整数位置。 In this case, the letter at position x is cast into an integer, and then compared to the actual number value of the first space! 在这种情况下,将位置x处的字母转换为整数,然后与第一个空格的实际数字值进行比较!

To fix the program, replace your entire if statement with this. 要修复程序,请用此替换整个if语句。

if (nameAndSurname.charAt(x) == ' ') //if there is a space
{
    count = c-1; //how many characters/letters was there before space
    System.out.println(count);
}

Extra: 额外:

Since the way you've solved this problem is a bit overkill, I've posted another solution below which solves it in a way that is easier to read. 由于您解决此问题的方法有些过头,因此我在下面发布了另一种解决方案,该解决方案以更易于阅读的方式进行了解决。 Also it won't break if you put in more or less than 1 space. 如果放置的空间少于或少于1个,它也不会损坏。

Scanner scan = new Scanner(System.in);
String nameAndSurname;
System.out.println("Enter your full name please:");
nameAndSurname = scan.nextLine().trim();
int indexOfFirstSpace = nameAndSurname.indexOf(' ');
if (indexOfFirstSpace > -1) {
    String firstName = nameAndSurname.substring(0, indexOfFirstSpace);
    System.out.println("Your first name is " + firstName.toUpperCase());
    System.out.println("It is " + firstName.length() + " characters long.");
}

Why bother with all that code? 为什么要打扰所有这些代码? Just skip the for-loop, have an 只需跳过for循环,

if (space != -1) nameOnly = nameAndSurname.substring(0,space);

and if you really want to know the amount of letters, it is space+1 如果您真的想知道字母的数量,则为空格+1

No need for all that complicated stuff. 不需要所有复杂的东西。

You can verify if your string has space before start the loop, something like this: 您可以在开始循环之前验证字符串是否有空间,如下所示:

public class Initials {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in); 
    String nameAndSurname, nameOnly;
    int c = 0, count = 0;
    System.out.println("Enter your full name please:");
    nameAndSurname = scan.nextLine();
    int space = nameAndSurname.indexOf(' ');
    if(space == -1) {
          System.out.println("Your name has no spaces");
    } else {
    for(int x = 0; x<nameAndSurname.length(); x++) {
        c++;
        if(nameAndSurname.indexOf(x) == space) //if there is a space
        { 
            count = c; //how many characters/letters was there before space
            System.out.println(count);
        } 
    }
    nameOnly = nameAndSurname.substring(0, count);
    System.out.println("Your name is: " + nameOnly.toUpperCase() + " and it has " + count + " letters");

}
scan.close();

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM