请教一个Java小问题,希望大家多帮忙!

martinlining 2004-04-17 07:40:42
其实不是一个很难的问题,可是我是新学Java,对java的数组,结构等不是很熟悉,希望大家多帮帮忙.

问题是这样的:
比如输入:
She love ?? you !!

HELL neck. THE you love the god!!!

程序会记录下每一个单词并且忽略任何非字母字符,而且多次出现的字符
只记录一次,并且把所有字符按字符顺序一行一个得排列出来.而且把所有的
字母都转成小写.那么对于上面的输入,输出是:
god
hell
love
neck
she
the
you


我自己觉得不是很难,可是现在脑子很乱,不知该怎么安排,希望各位高手帮帮忙,谢谢了!
...全文
49 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
martinlining 2004-04-18
  • 打赏
  • 举报
回复
太谢谢了 好好研究一下
swiminthesea 2004-04-18
  • 打赏
  • 举报
回复
mark一下^_^
sean_gao 2004-04-18
  • 打赏
  • 举报
回复
// 改版:

import java.util.*;

public class TestWordFetcher {

public static void main(String[] args) {
String str = "She love ?? you !!\n\nHELL neck. THE you love the god!!!\n";
System.out.println(">>>>>>>>>>Original string<<<<<<<<<");
System.out.println(str);
TreeSet res = fetchIt(str);
System.out.println(">>>>>>>>>>>>>>Result<<<<<<<<<<<<<<");
Iterator it = res.iterator();
while (it.hasNext()) System.out.println(it.next());
}

public static TreeSet fetchIt(String s) {
String neat = getNeaterString(s);
StringTokenizer st = new StringTokenizer(neat, " ");
int count = st.countTokens();
TreeSet ts = new TreeSet();
for (int i = 0; i < count; i++) {
ts.add(st.nextToken().toLowerCase());
}
return ts;
}

private static String getNeaterString(String str) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
if (isCharacter(str.charAt(i))) sb.append(str.charAt(i));
else sb.append(' ');
}
return sb.toString();
}

private static boolean isCharacter(char c) {
if (((int)c>=65&&(int)c<=90)||((int)c>=97&&(int)c<=122)) return true;
else return false;
}

}

// getNeaterString方法在返回值中将原字符串的非字母字符替换成空格。
// isCharacter方法用于判断字符是否是字母。
sean_gao 2004-04-18
  • 打赏
  • 举报
回复
import java.util.*;

public class TestWordFetcher {

public static void main(String[] args) {
String str = "She love ?? you !!\n\nHELL neck. THE you love the god!!!\n";
System.out.println(">>>>>>>>>>Original string<<<<<<<<<");
System.out.println(str);
TreeSet res = fetchIt(str);
System.out.println(">>>>>>>>>>>>>>Result<<<<<<<<<<<<<<");
Iterator it = res.iterator();
while (it.hasNext()) System.out.println(it.next());
}

public static TreeSet fetchIt(String s) {
String delim = ",.?!;'\" \t\r\n\f<>/\\|}{[]`1234567890-=+_)(*&^%$#@!~";
StringTokenizer st = new StringTokenizer(s, delim);
int count = st.countTokens();
TreeSet ts = new TreeSet();
for (int i = 0; i < count; i++) {
ts.add(st.nextToken().toLowerCase());
}
return ts;
}

}

// 上述实现中判断去除字符的方法比较弱,最好自己再另外写个方法处理。
martinlining 2004-04-17
  • 打赏
  • 举报
回复
我仔细看了一下 可是还是不太明白 我只是想知道在java里怎么定义数组 结构 tree等
最好能给几个例子 谢谢了
panpan221 2004-04-17
  • 打赏
  • 举报
回复
来学习一下
ai92 2004-04-17
  • 打赏
  • 举报
回复
我有两个类似的问题,不过是单个字符的比较
你可以参考一下
楼上的有理.
http://expert.csdn.net/Expert/topic/2945/2945504.xml?temp=.1984217
martinlining 2004-04-17
  • 打赏
  • 举报
回复
谢谢 能不能详细讲一下TreeSet的用法哪? 最好举个例子来说明一下怎么定义 怎么初始化
怎么赋值. 谢谢了!
buaaaladdin 2004-04-17
  • 打赏
  • 举报
回复
大致说一下我的想法,用StringTokenizer获取分割的单词,然后全部添加到一个TreeSet中,再用Iterator遍历输出。
martinlining 2004-04-17
  • 打赏
  • 举报
回复
没人帮忙吗?

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧