我正在尝试解析文本文件并将第一个单词存储为HashMap的键,并将剩余的单词存储为其值。我试图做的是创建一种翻译器,用户将用英语(键)输入一个单词,它将返回值(西班牙语翻译,可能不止一个单词)。这是一个文本文件的示例(注意:我也不能存储以char'#'开头的行中的字符串):
###########################################################################
#Copyright 1999 The Internet Dictionary Project/Tyler Chambers
###########################################################################
a un, uno, una[Article]
aardvark cerdo hormiguero
aardvark oso hormiguero[Noun]
aardvarks cerdos hormigueros
aardvarks osos hormigueros
ab prefijo que indica separacio/n
aback hacia atrás
更新:我已将代码更新到目前为止的内容。我很亲密。我遇到的一个问题是处理具有多种西班牙语翻译的(英语)单词。我遇到的问题是处理这些键和值。当一个单词有多个翻译时我想要发生的事情如下: 输出: 英语:aardvark 西班牙语:1.cerdo hormiguero 2.oso hormiguero [名词]
我遇到的另一个问题是,当用户输入未翻译的单词时,会显示一条消息,说明无法翻译。我用了 if(request.getParameter(“inputtext”)!= null) 检查但不起作用。任何帮助,将不胜感激! 包lab1;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/Translate")
public class Translate extends HttpServlet {
private static final long serialVersionUID = 1L;
public static String realPath;
private BufferedReader reader;
public HashMap<String, String> lists = new HashMap<String, String>();
public Translate() {
super();
// TODO Auto-generated constructor stub
}
public void init(ServletConfig config) throws ServletException {
super.init(config);
realPath = this.getServletContext().getRealPath("/WEB-INF/Spanish.txt");
File file = new File(realPath);
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "ISO8859-1"));
String line = null;
StringBuilder rslt = new StringBuilder();
if(reader != null){
while ((line = reader.readLine()) != null) {
if ((line.length()>0)&&(line.charAt(0) != '#')){
String [] tokens = line.split("\\s", 2);
if (lists.containsKey(tokens[0])) {
//handle the duplicates
} else {
lists.put(tokens[0].replaceAll("\\s",""), line.substring(tokens[0].length()).trim());
}
}
}
Iterator iterator = lists.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next().toString();
String value = lists.get(key).toString();
//System.out.println("KEY: " + key + " \nVALUE:" + value);
}
}
//System.out.println(rslt.toString());
} catch (FileNotFoundException e) {
System.out.println("Blah1");
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>English to Spanish Translator</title>");
out.println("<style>h1,h2,form,p{text-align:center;color:white}body{background-color:black;}</style></head>");
out.println("<body>");
out.println("<h1>Welcome to the Online Translator</h1>");
out.println("<h2>Please Enter a Word in English:</h2>");
out.println("<form method='post'><input id='inputtext' name='inputtext'></inputText><input type='submit' value='Translate' name='submit'></input></form>");
out.println("</body></html>");
out.close();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
String value = (String) lists.get(request.getParameter("inputtext"));
if (request.getParameter("submit") != null) {
if(request.getParameter("inputtext") != null){
// word is found
out.println("<html>");
out.println("<head><title>English to Spanish Translator</title>");
out.println("<style>h1,h2,p{text-align:center;color:white}body{background-color:black;}</style></head>");
out.println("<body>");
out.println("<h1>Here is your Translation of the word: " + request.getParameter("inputtext") + "</h1>");
out.println("<h2>" + value + "</h2>");
out.println("<p><a href='./Translate'>Translate Another Word</a></p>");
out.println("</body></html>");
out.close();
} else if(request.getParameter("inputtext") == null){
// word is not in translator
out.println("<html>");
out.println("<head><title>English to Spanish Translator</title></head>");
out.println("<body>");
out.println("<h1>The Word " + request.getParameter("inputtext") + " is not in the translation</h1>");
out.println("<p><a href='./Translate'>Translate Another Word</a></p>");
out.println("</body></html>");
out.close();
}
}
}
}
答案 0 :(得分:3)
解析这条线的方式是错误的 试试这个(你可以用更好的方式处理空间,这只是一个简单的例子):
if(reader != null){
while ((line = reader.readLine()) != null) {
if ((line.length()>0)&&(line.charAt(0) != '#')){
String[] tokens = line.split("\\s+", 2);
lists.put(tokens[0], tokens[1]);
}
}
Iterator iterator = lists.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next().toString();
String value = lists.get(key).toString();
System.out.println("KEY: " +key + " VALUE:" + value);
}
}
输出结果为:
KEY:aardvarks VALUE:osos hormigueros
KEY:a VALUE:un,uno,una [Article]
KEY:aback VALUE:haciaatrás
KEY:ab VALUE:prefijo que indica separacio / n
KEY:aardvark VALUE:oso hormiguero [Noun]
但是,上述解决方案的问题在于它无法处理重复的KEY,如果KEY相同,则新的VALUE将替换旧的VALUE。如果要解决此问题,请在将KEY-VALUE对放入哈希映射之前先调用containsKey()方法:
if ((line.length()>0)&&(line.charAt(0) != '#')){
String[] tokens = line.split("\\s+", 2);
if (lists.containsKey(tokens[0]) {
//handle the duplicates
} else {
lists.put(tokens[0], tokens[1]);
}
}
答案 1 :(得分:1)
首先,您使用!=
来比较字符串。这是一个很大的禁忌。请改用!string.equals(otherString)
。
==
和!=
比较字符串的引用而不是字符串本身的值,因此所有行都不相等。
你可以通过将字符与#字符进行比较来完全摆脱字符串:line.charAt(0) != '#'