import java.util.StringTokenizer;
class StringTokenizerExample1 {
public static void main(String args[]) {
StringTokenizer stok = new StringTokenizer("apple,apple?????|apple!apple orange%orange"," %!?,|");
while (stok.hasMoreTokens()) {
String str = stok.nextToken();
System.out.println(str);
}
}
}
输出结果
apple
apple
apple
apple
orange
orange"apple,apple?????|apple!apple orange%orange"
1.如何修改程序 可以将以上文字列 在Console(Run- Run Configurations -Arguments)下输入
因为我想列出的不仅是apple orange 还有别的 或者一段文字2. 如何能统计里面的apple和orange的个数呢?
输出这样的形式
apple 3
orange 2
class StringTokenizerExample1 {
public static void main(String args[]) {
StringTokenizer stok = new StringTokenizer("apple,apple?????|apple!apple orange%orange"," %!?,|");
while (stok.hasMoreTokens()) {
String str = stok.nextToken();
System.out.println(str);
}
}
}
输出结果
apple
apple
apple
apple
orange
orange"apple,apple?????|apple!apple orange%orange"
1.如何修改程序 可以将以上文字列 在Console(Run- Run Configurations -Arguments)下输入
因为我想列出的不仅是apple orange 还有别的 或者一段文字2. 如何能统计里面的apple和orange的个数呢?
输出这样的形式
apple 3
orange 2
可以使用String 的split
还可以用正则表达式
第二个 我看书上的意思是用StringTokenizer
而且是从Console(Run- Run Configurations -Arguments)下输入
有会用StringTokenizer 做的吗
你准备一个两个整数变量或者一个整数数组记录个数,初始为0
while (stok.hasMoreTokens()) {
String str = stok.nextToken();
System.out.println(str);
}
这个时候判断str的值,如果等于"orange"那么对应的变量自增,如果等于"apple"也一样
Map<String, Integer> map = new HashMap<String, Integer>();
while (stok.hasMoreTokens()) {
String str = stok.nextToken();
if(map.containsKey(str)){// 方法名可能记得不是太准备,以方法为准
map.put(str, 1);
} else {
// 值+1
}
}
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.Map.Entry;
class StringTokenizerExample1 {
public static void main(String args[]) {
String line = null;
BufferedReader reader = null;
System.out.print("input>");
try {
reader = new BufferedReader(new InputStreamReader(System.in));
line = reader.readLine();
reader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}finally{
try {
reader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
StringTokenizer stok = new StringTokenizer(line," %!?,|");
Map<String, Integer> counter = new HashMap<String, Integer>();
while (stok.hasMoreTokens()) {
String str = stok.nextToken();
System.out.println(str);
if(counter.get(str) != null){
counter.put(str, counter.get(str) + 1);
}else{
counter.put(str, 1);
}
}
for(Entry<String, Integer> entry : counter.entrySet()){
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
}
在Console(Run- Run Configurations -Arguments)下输入 该怎么改呢
题上面说的
请问line 是做什么用的呢 谢谢
不好意思,刚才没看详细,以为是从控制台输入呢。
1,删掉这些代码,
String line = null;
BufferedReader reader = null;
System.out.print("input>");
try {
reader = new BufferedReader(new InputStreamReader(System.in));
line = reader.readLine();
reader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}finally{
try {
reader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
2 把 StringTokenizer stok = new StringTokenizer(line," %!?,|");
改成 StringTokenizer stok = new StringTokenizer(args[0]," %!?,|"); 3 执行的时侯, java StringTokenizerExample1 "你的串就行了"控制台就是打开运行输入CMD后出来的那个黑窗口,可能以前的时侯,操作系统没界面,那就是人机交互的唯一途径,所以叫控制台吧。
输入输出相对的,流就是比特流。
我的串没用""
""是固定的形式吗 通过程序可以去掉不用输入?
Console输入整数的时候 不用""
我还是不太明白你说的,Console(Run- Run Configurations -Arguments)是什么意思,你是用Eclipse来写的,在run configuration 里面吗?
如果是的话,程序,
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.Map.Entry;
class StringTokenizerExample1 {
public static void main(String args[]) {
// String line = null;
// BufferedReader reader = null;
// System.out.print("input>");
// try {
// reader = new BufferedReader(new InputStreamReader(System.in));
// line = reader.readLine();
// reader.close();
// } catch (IOException e) {
// throw new RuntimeException(e);
// }finally{
// try {
// reader.close();
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
// }
StringTokenizer stok = new StringTokenizer(args[0]," %!?,|");
Map<String, Integer> counter = new HashMap<String, Integer>();
while (stok.hasMoreTokens()) {
String str = stok.nextToken();
System.out.println(str);
if(counter.get(str) != null){
counter.put(str, counter.get(str) + 1);
}else{
counter.put(str, 1);
}
}
for(Entry<String, Integer> entry : counter.entrySet()){
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
}
在arguments里,输入:"apple,apple?????|apple!apple orange%orange",包括引号。
已该可以,你试试吧