去哪儿笔试解决方案
2014-03-04 22:41
169 查看
有两个文件context.txt和words.conf,请尝试将他们合并成为一段文字,并打印出来。 这两个文件内容如下: context.txt
“并不是每个人都需要$(qunar)自己的粮食,$(flight.1)每个人都需要做自己穿的$(flight.2),我们说着别人发明的$(hotel),使用别人发明的数学......我们一直在$(tuan)别人的成果。使用人类的已有经验和知识$(travel.1)来进行,是一件$(travel.2)的事情”
word.conf
flight=也不是:衣服
qunar=种植
hotel=语言
tuan=使用
travel=发明创造:很了不起
解决方案:
package qunaer;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class Test2 {
public String readContextTxt(){
FileReader fr=null;
BufferedReader br=null;
StringBuilder sb = new StringBuilder();
String returnValue=null;
try {
fr = new FileReader("context.txt");
br = new BufferedReader(fr);
String line;
while((line=br.readLine())!=null){
sb.append(line);
}
returnValue=sb.toString();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
} finally{
try {
if(br != null){
br.close();
}
if(fr != null){
fr.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(returnValue);
return returnValue;
}
public Map<String,String> readWordConf(){
FileReader fr = null;
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
Map map = new HashMap();
try{
fr = new FileReader("word.conf");
br = new BufferedReader(fr);
String line;
while((line = br.readLine()) != null){
sb.append(line);
System.out.println(line);
String [] str = line.split("\\=");
map.put(str[0], str[1]);
}
}catch(Exception e){
e.printStackTrace();
}
return map;
}
public Map<String,String[]> getWordConf(Map<String,String> map){
Map<String,String[]> returnValue = new HashMap<String,String[]>();
for(Map.Entry<String, String> entry:map.entrySet()){
String key = entry.getKey();
String value = entry.getValue();
System.out.println("vvvvalue:"+value);
String [] str = value.split("\\:");
System.out.println("llllength:"+str.length);
returnValue.put(key, str);
}
System.out.println(returnValue);
return returnValue;
}
public String replaceValue(Map<String,String[]> map){
String txt = this.readContextTxt();
while(txt.indexOf("$(")!=-1){
int start = txt.indexOf("$(");
int end = txt.indexOf(")");
String sub = txt.substring(start+2, end);
String []arr = sub.split("\\.");
System.out.println("sub:"+sub+arr[0]);
String replace = null;
for(Map.Entry<String, String[]> entry:map.entrySet()){
String key = entry.getKey();
String []value = entry.getValue();
System.out.println("key:"+key+" value.length:"+value.length);
if(key.equals(arr[0])){
if(arr.length>1){//表示含有小数点
replace = value[Integer.parseInt(arr[1])-1];
}else{
replace = value[0];
}
System.out.println("sub:"+sub+" replace:"+replace);
txt = txt.replaceAll(sub, replace);
txt = txt.replaceFirst("\\$\\(", "");
txt = txt.replaceFirst("\\)", "");
break;
}
}
}
System.out.println(txt);
return txt;
}
public static void main(String []args){
Test2 t2 = new Test2();
Map map = t2.readWordConf();
Map<String,String[]> arrMap = t2.getWordConf(map);
String val = t2.replaceValue(arrMap);
}
}
“并不是每个人都需要$(qunar)自己的粮食,$(flight.1)每个人都需要做自己穿的$(flight.2),我们说着别人发明的$(hotel),使用别人发明的数学......我们一直在$(tuan)别人的成果。使用人类的已有经验和知识$(travel.1)来进行,是一件$(travel.2)的事情”
word.conf
flight=也不是:衣服
qunar=种植
hotel=语言
tuan=使用
travel=发明创造:很了不起
解决方案:
package qunaer;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class Test2 {
public String readContextTxt(){
FileReader fr=null;
BufferedReader br=null;
StringBuilder sb = new StringBuilder();
String returnValue=null;
try {
fr = new FileReader("context.txt");
br = new BufferedReader(fr);
String line;
while((line=br.readLine())!=null){
sb.append(line);
}
returnValue=sb.toString();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
} finally{
try {
if(br != null){
br.close();
}
if(fr != null){
fr.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(returnValue);
return returnValue;
}
public Map<String,String> readWordConf(){
FileReader fr = null;
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
Map map = new HashMap();
try{
fr = new FileReader("word.conf");
br = new BufferedReader(fr);
String line;
while((line = br.readLine()) != null){
sb.append(line);
System.out.println(line);
String [] str = line.split("\\=");
map.put(str[0], str[1]);
}
}catch(Exception e){
e.printStackTrace();
}
return map;
}
public Map<String,String[]> getWordConf(Map<String,String> map){
Map<String,String[]> returnValue = new HashMap<String,String[]>();
for(Map.Entry<String, String> entry:map.entrySet()){
String key = entry.getKey();
String value = entry.getValue();
System.out.println("vvvvalue:"+value);
String [] str = value.split("\\:");
System.out.println("llllength:"+str.length);
returnValue.put(key, str);
}
System.out.println(returnValue);
return returnValue;
}
public String replaceValue(Map<String,String[]> map){
String txt = this.readContextTxt();
while(txt.indexOf("$(")!=-1){
int start = txt.indexOf("$(");
int end = txt.indexOf(")");
String sub = txt.substring(start+2, end);
String []arr = sub.split("\\.");
System.out.println("sub:"+sub+arr[0]);
String replace = null;
for(Map.Entry<String, String[]> entry:map.entrySet()){
String key = entry.getKey();
String []value = entry.getValue();
System.out.println("key:"+key+" value.length:"+value.length);
if(key.equals(arr[0])){
if(arr.length>1){//表示含有小数点
replace = value[Integer.parseInt(arr[1])-1];
}else{
replace = value[0];
}
System.out.println("sub:"+sub+" replace:"+replace);
txt = txt.replaceAll(sub, replace);
txt = txt.replaceFirst("\\$\\(", "");
txt = txt.replaceFirst("\\)", "");
break;
}
}
}
System.out.println(txt);
return txt;
}
public static void main(String []args){
Test2 t2 = new Test2();
Map map = t2.readWordConf();
Map<String,String[]> arrMap = t2.getWordConf(map);
String val = t2.replaceValue(arrMap);
}
}
相关文章推荐
- 去哪儿笔试解决方案二(java)
- 去哪儿笔试解决方案二(java)
- java去哪儿笔试解决方案四
- 名企笔试:去哪儿2015研发笔试题(寻找Coder)2017-03-24 算法爱好者
- 去哪儿2015笔试题JAVA实现
- 去哪儿笔试:统计字符
- 去哪儿2017校园招聘笔试题——获得文件扩展名filename extension
- 去哪儿笔试题(2012年成都站)- 马上要笔试了,做做历年的题。
- 去哪儿在线笔试题目之自动驾驶
- 去哪儿笔试题——排序数组循环移位后查找
- 去哪儿笔试:filename extension
- 去哪儿2017校园招聘笔试题——统计字符串中最先出现三次的字符
- 去哪儿笔试:座位安排问题
- 去哪儿笔试:部分和问题 java
- 去哪儿2015笔试题:寻找字符串的差异
- C/C++基础笔试题1.1.2(十问解决方案运营商)
- 去哪儿笔试题
- 2014亚马逊在线笔试题目及解决方案(MMChess问题)
- 去哪儿笔试题 首个重复字符
- 去哪儿2013笔试编程