[code=Java][/code]import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.StringTokenizer;
public class DeleteDuplicate {
static void Reader() {
try {
BufferedReader br = new BufferedReader(new FileReader("1.txt"));
String S = br.readLine();
String[] s = new String[100];
while (S != null) {
StringTokenizer t = new StringTokenizer(S);
for (int i = 0; i < S.length(); i++) {
s[i] = t.nextToken();//运行时说空指针错误了...我不明白
}
for (int i = 0; i < (S.length() - 1); i++) {
if (!s[i].equalsIgnoreCase(s[1 + i])) {
Work(s[i]);
}
}
}
br.close();
} catch (IOException e) {
System.out.println("IO Exception " + e);
}
}
static void Work(String s) {
try {
FileWriter fw = new FileWriter("2.txt", true);
fw.write(s);
fw.write(" ");
fw.close();
} catch (IOException e) {
System.out.println("IO Error " + e);
}
}
public static void main(String[] agrs) {
Reader();
}
}
其中1.txt内容是:aabbbbbbbaaaaacccc
------解决方案--------------------
StringTokenizer
public StringTokenizer(String str)
Constructs a string tokenizer for the specified string. The tokenizer uses the default delimiter set, which is " \t\n\r\f": the space character, the tab character, the newline character, the carriage-return character, and the form-feed character. Delimiter characters themselves will not be treated as tokens.
for (int i = 0; i < S.length(); i++) {
s[i] = t.nextToken();//运行时说空指针错误了...我不明白
你既然用了token,为啥还要去循环S, 你的StringTokenizer只会以默认的" \t\n\r\f"去分割,而不是把每个字符分割
另外,while (S != null) 是不是个死循环
学者去debug吧,空指针异常debug很容易找到的
------解决方案--------------------
每个字符就不要用StringTokenizer了,你根本不需要用到它的功能
如果一定要用,你的for循环就不要再依赖S了,直接用t.hasNext来循环
"A B"长度是3
但是只有有两个token,想想就知道了
------解决方案--------------------
设一下断点或打印一下哪里异常了
自己调试吧
------解决方案--------------------
用 System.out.println试试哪里没有接受到东西吧。
------解决方案--------------------
不要用StringToken,现在已经很少 用了,用正则吧