日期:2014-05-20  浏览次数:20886 次

华为的一道上机题,我用java写怎么老是莫名其妙的异常呢?
问题描述:  
在给定字符串中找出单词( “单词”由大写字母和小写字母字符构成,其他非字母字符视为单词的间隔,如空格、问号、数字等等;另外单个字母不算单词);找到单词后,按照长度进行降序排序,(排序时如果长度相同,则按出现的顺序进行排列),然后输出到一个新的字符串中;如果某个单词重复出现多次,则只输出一次;如果整个输入的字符串中没有找到单词,请输出空串。输出的单词之间使用一个“空格”隔开,最后一个单词后不加空格。
要求实现函数:


Java code
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class StringSort {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.next();
        
        int subLength = 0;
        StringBuilder sb = new StringBuilder();
        Set<MyString> strs = new TreeSet<MyString>();
        for(int i=0;i<s.length();i++){
            char c = s.charAt(i);
            if((c<='z'&&c>='a')||(c<='Z'&&c>='A')){
                subLength++;
                sb.append(c);
            }
            else
            {
                if(subLength!=0){
                    System.out.println(sb.toString()+" "+subLength);
                    strs.add(new MyString(sb.toString(), subLength));
                    sb.delete(0,sb.capacity());
                    subLength = 0;
                }
            }
        }
        
        if(subLength!=0){
            System.out.println(sb.toString()+" "+subLength);
            strs.add(new MyString(sb.toString(), subLength));
        }
        
        java.util.Iterator<MyString> it = strs.iterator();
        while(it.hasNext()){
            MyString myString = it.next();
            System.out.println(myString.subStr);
        }
    }
}

class MyString implements Comparable<MyString>{
    String subStr;
    int subLength;
    
    public MyString(String subStr, int subLength) {
        this.subStr = subStr;
        this.subLength = subLength;
    }

    @Override
    public int compareTo(MyString o) {
        if(this.subLength>o.subLength)
            return -1;
        else if(this.subLength>o.subLength)
            return 1;
        else
            return 0;
    }

    @Override
    public boolean equals(Object obj) {
        return this.subStr.equals(((MyString)obj).subStr);
    }
    
    
    
}



调试的时候每次运行到遍历treeset的时候就会跳到Thread的异常里面去,实在不解

------解决方案--------------------
package com.xyj.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class Test6 {
private static List<String> list = new LinkedList<String>();

public static void main(String[] args) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(System.in));
String str = reader.readLine();

List<String> list = getWordList(str);

List<String> reList = sortWordByLength(list);
for(Iterator<String> iter=reList.iterator(); iter.hasNext();) {
System.out.println(iter.next());
}

} catch (IOException e) {
e.printStackTrace();
} finally {
if(reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

public static List<String> getWordList(String str) {
StringBuilder sb = new StringBuilder();
for(int i=0; i<str.length(); i++) {
char c = str.charAt(i);
if(c<='z'&&c>='a' || c<='Z'&&c>='A') {
sb.append(c);
} else {
if(sb.length()!=0 && !"".equals(sb.toString().trim())) {
list.add(sb.toString());
}

sb = new StringBuilder();