Java 如何从字符串中提取子字符串(单词)?

Java 如何从字符串中提取子字符串(单词)?,java,substring,indexof,Java,Substring,Indexof,我试图输入一个四个单词的句子,然后能够使用indexOf和子字符串分别打印出每个单词。知道我做错了什么吗 编辑 这就是它应该看起来的样子吗?我已经运行了两次,收到了两个不同的答案,所以我不确定运行该程序的程序是否有故障,或者我的程序本身是否有故障 import java.util.Scanner; public class arithmetic { public static void main(String[] args) { Scanner in = new Scanner(Syst

我试图输入一个四个单词的句子,然后能够使用indexOf和子字符串分别打印出每个单词。知道我做错了什么吗

编辑

这就是它应该看起来的样子吗?我已经运行了两次,收到了两个不同的答案,所以我不确定运行该程序的程序是否有故障,或者我的程序本身是否有故障

import java.util.Scanner;
public class arithmetic {
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    String sentence;
    String word1, word2, word3, word4;
    int w1, w2, w3, w4;
    int p, p2, p3, p4;

    System.out.print("Enter a sentence with 4 words: ");
    sentence = in.nextLine();

    p = sentence.indexOf(" ");



    word1 = sentence.substring(0,p)+" ";
    w1 = 1 + word1.length();
    p2 = word1.indexOf(" ");
    word2 = sentence.substring(w1,p2);
    w2 = w1+1+word2.length();
    p3 = word2.indexOf(" ");
    word3 = sentence.substring(w2,p3);
    w3 = w1+w2+1+word3.length();
    p4 = word3.indexOf(" ");
    word4 = sentence.substring(w3,p4);
    w4 = w1+w2+w3+1+word4.length();

我至少看到两件事:

  • 你没有正确计算索引。第三个单词的起始索引应该类似于
    第一个单词的长度+1+第二个单词的长度+1
    ,但看起来您忽略了第一个单词的长度。类似地,当你得到第四个单词时,你会忽略前两个单词的长度
  • indexOf(“”)
    将只获取第一次出现的空格的索引。在获得第一个空间后,您将重用该索引,而不是使用其他空间的索引

  • 最后,在修复这两个之后,如果您知道单词将由空格分隔,那么您可能需要查看函数。使用它,您可以拆分句子,而无需查找所有空格。

    出于性能原因、可读性和bug,我几乎不建议不使用
    子字符串和
    索引。考虑下面的任何一个(所有这些都考虑单词作为非空白字符):


    ,int)那么,在第一次出现之后,如何获得空格的索引呢?@CooperTaylor查看文档,我看到另一个包含两个参数,第二个参数指定从哪个索引开始。该功能应该会有所帮助。:)
    public static void main (String[] args) throws java.lang.Exception
    {
        int wordNo = 0;
    
        System.out.println("using a Scanner (exactly 4 words):");
    
        InputStream in0 = new ByteArrayInputStream("a four word sentence".getBytes("UTF-8"));
        Scanner scanner = new Scanner(/*System.*/in0);
    
        try {
            String word1 = scanner.next();
            String word2 = scanner.next();
            String word3 = scanner.next();
            String word4 = scanner.next();
            System.out.printf("1: %s, 2: %s, 3: %s, 4: %s\n", word1, word2, word3, word4);
        } catch(NoSuchElementException ex) {
            System.err.println("The sentence is shorter than 4 words");
        }
    
        System.out.println("\nusing a Scanner (general):");
    
        InputStream in1 = new ByteArrayInputStream("this is a sentence".getBytes("UTF-8"));
    
        for(Scanner scanner1 = new Scanner(/*System.*/in1); scanner1.hasNext(); ) {
            String word = scanner1.next();
            System.out.printf("%d: %s\n", ++wordNo, word);
        }
    
    
        System.out.println("\nUsing BufferedReader and split:");
    
        InputStream in2 = new ByteArrayInputStream("this is another sentence".getBytes("UTF-8"));
    
        BufferedReader reader = new BufferedReader(new InputStreamReader(/*System.*/in2));
        String line = null;
        while((line = reader.readLine()) != null) {
            for(String word : line.split("\\s+")) {
                System.out.printf("%d: %s\n", ++wordNo, word);
            }
        }
    }