消失的Arraylist值

时间:2014-08-10 23:33:51

标签: java string arraylist

我正在编写一个Java程序,它将使用句子(或短语)并将其转换为计算机可以轻松读取的一组对象。我想用一个简单的单词来分隔程序,然后再将其扩展。

我的代码是这样的:

package Literary;

import java.util.ArrayList;

public class WordParser {

public static String[] getWords(String tempone){
    ArrayList<String> temptwo = new ArrayList();
    ArrayList<Character> tempthree = new ArrayList();
    for (int tempfour = 0; tempfour == tempone.length() - 1; tempfour++){
        if (tempone.charAt(tempfour) != ' '){
            tempthree.add(tempone.charAt(tempfour));
    } else {
        temptwo.add(getStringRepresentation(tempthree));
        tempthree.clear();
    }
    }
    String[] tempfive = new String[temptwo.size()];
    for (int tempfour  = 0; tempfour == tempfive.length - 1; tempfour++){
        tempfive[tempfour] = temptwo.get(tempfour);
    }
    return tempfive;
    }

    /** Courtesy of Vineet Reynolds on StackExchange.
     * 
     *  "You can iterate through the list and create the string."
     * 
     * @param list
     * @return 
     */

    public static String getStringRepresentation(ArrayList<Character> list){    
        StringBuilder builder = new StringBuilder(list.size());
        for(int i = 0; i == list.size() + 1; i++)
        {
            builder.append(list.get(i).charValue());
        }
        return builder.toString();
    }
}

它应该接收一个字符串作为输入,并返回一个由空格分隔的字符串列表。

但是当我运行我的主要课程时:

import Literary.WordParser;

public class Start {

    public static void main(String[] args) {
        String x = "There was once a sword in the stone";
        String[] tempstring = WordParser.getWords(x);
        for (int i = 1; i == tempstring.length; i++){
           System.out.println("Word " + i + " : " + tempstring[i]);
        }
    }
}

run:BUILD SUCCESSFUL (total time: 1 second)外,控制台告诉我任何内容。

如果有帮助,我正在使用Netbeans 8和Java 1.7。

5 个答案:

答案 0 :(得分:4)

看起来问题就在这里:

for (int i = 1; i == tempstring.length; i++) {

此for循环最多只运行一次:如果tempstring只有一个String长,则应打印出该单词。

但是,由于您的测试句有8个单词,因此无法打印出任何内容(前提是WordParser正常工作)。

您可能希望将此行更改为:(请注意<i之间的tempstring.length。)

for (int i = 1; i < tempstring.length; i++) {

这样它就会遍历tempstring中的所有项目。

答案 1 :(得分:1)

您的代码中存在多个问题:

1)for循环没有正确制作,它们永远不会执行。使用!=, > or <代替==

2)你不需要方法getWords()和getStringRepresentation()。像这样的方法已经用Java实现了。

所以最终的代码应该是这样的:

public class WordParser {

    public static String[] getWords(String tempone) {
        return tempone.split(" ");
    }

    public static void main(String[] args) {
        String x = "There was once a sword in the stone";
        String[] tempstring = WordParser.getWords(x);
        for (int i = 0; i < tempstring.length; i++) {
            System.out.println("Word " + (i+1) + " : " + tempstring[i]);
        }
    }
}

输出:

Word 1 : There
Word 2 : was
Word 3 : once
Word 4 : a
Word 5 : sword
Word 6 : in
Word 7 : the
Word 8 : stone

如果您有兴趣,我还修复了与上述相同的代码:

import java.util.ArrayList;


public class WordParser {

    public static String[] getWords(String tempone) {
        ArrayList<String> sarr = new ArrayList<String>();
        ArrayList<Character> tempthree = new ArrayList<Character>();
        String[] ansarr;

        if(tempone.charAt(tempone.length()-1) != ' ')
            tempone += " "; //Add white space to the end to catch the last word

        for (int i = 0; i < tempone.length(); i++) {
            if (tempone.charAt(i) != ' ') {
                tempthree.add(tempone.charAt(i));
            } else {
                sarr.add(tempthree.toString());
                tempthree.clear();
            }
        }

        ansarr = new String[sarr.size()];
        for (int i = 0; i < ansarr.length; i++) {
            ansarr[i] = sarr.get(i);
        }
        return ansarr;

    }

    public static void main(String[] args) {
        String x = "There was once a sword in the stone";
        String[] tempstring = WordParser.getWords(x);
        for (int i = 0; i < tempstring.length; i++) {
            System.out.println("Word " + (i+1) + " : " + tempstring[i]);
        }
    }
}

享受! :)

答案 2 :(得分:0)

我认为你应该使用似乎做同样事情的String.split(" ")

答案 3 :(得分:0)

更改主要方法如下, 它会起作用

public static void main(String[] args) {
        String x = "There was once a sword in the stone";
        String[] tempstring = WordParser.getWords(x);
        for (int i = 1; i <= tempstring.length; i++){
           System.out.println("Word " + i + " : " + tempstring[i - 1]);
        }
    }

对于您可以使用的WordParser,

public class WordParser 
{
    public static String[] getWords(String tempone)
    {
        return tempone.split(" ");
    }  
}

答案 4 :(得分:0)

首先,我建议使用split方法来分解句子 它被定义为:

 public String[] split(String regex, int limit)

您只需致电

即可
 String s1=new String("Random words in a sentence");
 String[] words=s1.split(" ");

为了将字符串分解为单词,您现在将拥有一个String 五个元素的数组,其中每个元素由一个单词组成

对于您的问题,您没有正确使用条件语句 您希望迭代位置的字符串数组的元素 小于stringname.length,不仅如果它的位置等于stringname.length

因此,您必须在代码的这些部分进行以下更改

例如:

  for (int i = 1; i == tempstring.length; i++)

应将其行更改为

  for (int i = 1; i < tempstring.length; i++)

并且WordParser.java文件中的各个位置也会出现此问题 另外要记住,您可能经常想要从索引0开始 索引1,因为java有它的&#39;第一个指标为0。