Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我怎样才能把一个字符串分开并打印出来?_Java - Fatal编程技术网

Java 我怎样才能把一个字符串分开并打印出来?

Java 我怎样才能把一个字符串分开并打印出来?,java,Java,我有一个字符串str包含“苹果香蕉橙”。我想分别系统输出这些结果,因为它们都是存储在单个变量中的问题的选择。我该怎么做?上面的代码是我失败的尝试,因为子字符串不会更新str变量,而是创建一个新字符串。我无法使用循环,也无法使其动态化,因此无法使用它。您还需要更改str:- public void display() { super.display(); String str = super.getChoices(); System.out.println(str)

我有一个字符串
str
包含“苹果香蕉橙”。我想分别系统输出这些结果,因为它们都是存储在单个变量中的问题的选择。我该怎么做?上面的代码是我失败的尝试,因为子字符串不会更新
str
变量,而是创建一个新字符串。我无法使用循环,也无法使其动态化,因此无法使用它。

您还需要更改
str
:-

public void display()
{

    super.display();

    String str =  super.getChoices();

    System.out.println(str);

    while(!str.equals("")) 
    { 
        int a = str.indexOf(" ");
        System.out.println(str.substring(0, a));
        String sentence = str.replaceFirst(str, str.substring(a));
        System.out.println(sentence);
    }
} 

(未经测试,但你明白了)。您显然还需要检查
str
是否确实包含空格并退出。

如果我理解正确,您应该尝试使用


检查我的IP地址,看看这是否是一个真正的重复和垃圾邮件的问题。我怀疑这个问题的答案是否适合我的情况。你有一个包含单词“abo”的字符串,你问如何分别打印它们。因此,您希望将该字符串拆分为“”。这就是一切。重复一个问题与谁问这个问题无关。但问题是关于什么的。你最好花点时间来了解这里的情况。除此之外:你希望我们花时间帮助你;因此,请您花1分钟时间正确格式化/缩进您的输入;如果按空格分割字符串,它将返回一个数组,其中每个水果都是该数组的一项。您可以循环使用该数组
while(!str.equals("") ) { 
    int a = str.indexOf(" ");
    System.out.println(str.substring(0, a));
    String sentence = str.replaceFirst(str, str.substring(a));
    System.out.println(sentence);    
    str = str.substring(a); // Remove the bit of str that we've processed.

} 
StringTokenizer st=new StringTokenizer("Apple Banana Orange");

        while (st.hasMoreTokens()){
            System.out.println(st.nextToken());
        }