62,615
社区成员
发帖
与我相关
我的任务
分享
import java.io.*;
import java.util.*;
public class FilterString{
public static void main(String[] args) throws IOException{
//输入需要处理的字符串数字
System.out.println("输入字符串:");
Scanner in = new Scanner(System.in);
String str=in.next();
List<String> list = new ArrayList<String>();
list.add(str);
for(String item:list){
System.out.println(item);
}
int i=0;
for(String item:str.split("-"))
{
i+=item.length();
}
for(String item:str.split("-"))
System.out.print(item);
System.out.println("\t"+(str.length()-i));
}
}
package houlei.csdn.text;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
/**
* 一个简单的程序,实现的功能是输入几行字符串,然后过滤字符 '-',根据一定规律,将字母转化成数字。<br/>
* 最后统计输入的不同字符串的个数。<p/>
* 例如:<br/> 输入<br/> 5 //表示将要输入的字符串个数<br/> --234-<br/> 2-3-4-<br/> qw-er-<br/>
* 1eron<br/> q-w-e--------r<br/> <br/> 统计结果:<br/> 234 2<br/> 5715 2<br/>
* 11544 1<br/>
*
* @author HouLei
*
*/
public class Countstr {
/**
* 保存输入的数据
*/
private static List<String> income = new ArrayList<String>();
/**
* 保存输出的数据
*/
private static Map<String, Integer> outgo = new TreeMap<String, Integer>();
/**
* 从控制台读入数据
*/
private static void readFromConcle() {
// 输入需要处理的字符串数字
System.out.println("Enter number:");
Scanner in = new Scanner(System.in);
int lines = in.nextInt();
in.nextLine();
for (int i = 0; i < lines; i++) {
String line = in.nextLine();
income.add(line);
}
}
/**
* 从文本文件中读入数据
*/
private static void readFromFile(String fileName)
throws FileNotFoundException {
Scanner in = new Scanner(new FileInputStream(fileName));
while (in.hasNextLine()) {
income.add(in.nextLine());
}
}
/**
* 通过替换的方式过滤字符串中的某些字符。
*
* @param regex
* 正则表达式
*/
private static List<String> filterIncome(String regex) {
List<String> result = new ArrayList<String>(income.size());
for (String line : income) {
result.add(line.replaceAll(regex, ""));
}
return result;
}
/**
* 过滤掉字符串中的‘-’符号
*/
private static List<String> filterIncome() {
return filterIncome("\\-");
}
/**
* 将字母转化成数字
*/
private static String degist(String str) {
StringBuilder buff = new StringBuilder();
for (char ch : str.toCharArray()) {
if (ch >= 'a' && ch < 'z') {
ch = (char) ('0' + (ch - 'a') / 3);
} else if (ch >= 'A' && ch < 'Z') {
ch = (char) ('0' + (ch - 'A') / 3);
} else if (ch == 'z' || ch >= 'Z') {
ch = '9';
}
buff.append(ch);
}
return buff.toString();
}
/**
* 核心方法,处理输入的数据
*/
private static void doService() {
List<String> in = filterIncome();
// 统计过滤后,相同字符串的个数。
Map<String, Integer> cache = new HashMap<String, Integer>();
for (String key : in) {
if (null == cache.get(key)) {
cache.put(key, 1);
} else {
cache.put(key, cache.get(key) + 1);
}
}
// 处理字符串
for (Map.Entry<String, Integer> e : cache.entrySet()) {
String degist = degist(e.getKey());
outgo.put(degist, e.getValue());
}
}
/**
* 打印结果
*/
private static void printOutgo() {
for (Map.Entry<String, Integer> e : outgo.entrySet()) {
System.out.println(e.getKey() + "\t" + e.getValue());
}
}
public static void main(String[] args) throws IOException {
if (args != null && args.length > 0) {
readFromFile(args[0]);
} else {
readFromConcle();
}
doService();
printOutgo();
}
}