需要帮助来获取java中的字符串

时间:2022-05-17 02:02:43

I want get first name when input the name with array and looping

我想在输入带有数组和循环的名称时获得名字

Example :

Enter the name :

1. Alvin Indra
2. Sada Rika

Output :

1. Alvin
2. Sada

Code:

public static void main(String[] args) {
    int n;
    String teman[],namadepan[];
    Scanner sc = new Scanner(System.in);
    Scanner x = new Scanner(System.in);

    System.out.print("Put how many friends : ");
    n = sc.nextInt();

    teman = new String[n];
    namadepan = new String[n];
    for(int i=0;i<n;i++){
        System.out.print("Friend Of-"+(i+1)+" : ");
        teman[i] = x.nextLine();
    }
    System.out.print("\n");
    System.out.println("First Name : ");
    for(int i=0;i<n;i++){
        if(teman[i] == ' '){ // this is where I need help
            System.out.println((i+1)+". "+teman[i].substring(0,i));
        }
    }
}

2 个解决方案

#1


1  

Here you go

干得好

String name = "John Smith";
System.out.println(name.split(" ")[0]);

Using this will replace that second for loop, you don't need to cycle through each letter to look for a space you can just use the split method on the string and specifiy the character in which to split the string up and then call the first element.

使用它将替换第二个for循环,您不需要循环遍历每个字母以查找空格,您可以在字符串上使用split方法并指定要将字符串拆分的字符然后调用第一个字符元件。

Updated complete implementation

import java.util.Scanner;

public class testest {
    public static void main(String[] args){
        int n;
        String teman[],namadepan[];
        Scanner sc = new Scanner(System.in);
        Scanner x = new Scanner(System.in);

        System.out.print("Put how many friends : ");
        n = sc.nextInt();

        teman = new String[n];
        namadepan = new String[n];

        for(int i=0;i<n;i++){
            System.out.print("Friend Of-"+(i+1)+" : ");
            teman[i] = x.nextLine();
        }

        System.out.print("\n");

        for(int i=0;i<n;i++){
            System.out.println("First Name : ");
            System.out.println(teman[i].split(" ")[0]);
            System.out.print("\n");
        }
    }
}

#2


1  

You are probably looking for using contains and indexOf methods of String class in java, to use them as :

您可能正在寻找在java中使用String类的contains和indexOf方法,以将它们用作:

if (teman[i].contains(" ")) {
    System.out.println((i + 1) + ". " + teman[i].substring(0, teman[i].indexOf(" ")));
}

#1


1  

Here you go

干得好

String name = "John Smith";
System.out.println(name.split(" ")[0]);

Using this will replace that second for loop, you don't need to cycle through each letter to look for a space you can just use the split method on the string and specifiy the character in which to split the string up and then call the first element.

使用它将替换第二个for循环,您不需要循环遍历每个字母以查找空格,您可以在字符串上使用split方法并指定要将字符串拆分的字符然后调用第一个字符元件。

Updated complete implementation

import java.util.Scanner;

public class testest {
    public static void main(String[] args){
        int n;
        String teman[],namadepan[];
        Scanner sc = new Scanner(System.in);
        Scanner x = new Scanner(System.in);

        System.out.print("Put how many friends : ");
        n = sc.nextInt();

        teman = new String[n];
        namadepan = new String[n];

        for(int i=0;i<n;i++){
            System.out.print("Friend Of-"+(i+1)+" : ");
            teman[i] = x.nextLine();
        }

        System.out.print("\n");

        for(int i=0;i<n;i++){
            System.out.println("First Name : ");
            System.out.println(teman[i].split(" ")[0]);
            System.out.print("\n");
        }
    }
}

#2


1  

You are probably looking for using contains and indexOf methods of String class in java, to use them as :

您可能正在寻找在java中使用String类的contains和indexOf方法,以将它们用作:

if (teman[i].contains(" ")) {
    System.out.println((i + 1) + ". " + teman[i].substring(0, teman[i].indexOf(" ")));
}