这个项目是一个可编辑的字典程序。简短和甜蜜我试图循环已经添加到字典中的单词,并将用户输入与扫描仪与已添加的单词进行比较。 我的问题是,无论输入多么准确,我创建的异常总是被调用。 我不确定异常是错误还是字符串没有正确比较。从名为" dictionary.txt"的文件中读取字典单词。如果有帮助,该程序使用输入/输出。这是代码.... 请帮忙!!
import java.util.ArrayList;
public class Dictionary {
ArrayList <Word> Words;
public Dictionary(){
Words = new ArrayList <Word>();
}
public void addWord(String name, String definition){
Word word = new Word(name, definition);
Words.add(word);
}
public String findWord(String name){
String word = "";
for (int i = 0; i < Words.size(); i++){
if (name.equalsIgnoreCase(Words.get(i).getName())){
Words.get(i);
}
word += Words.get(i).getName();
}
return word;
}
public String findDefinition(String name){
String def = "";
for (int i = 0; i < Words.size(); i++){
if (name.equalsIgnoreCase(Words.get(i).getName())){
Words.get(i);
}
def += Words.get(i).getDefinition();
}
return def;
}
public String toString(){
String temp = "";
for (int i = 0; i < Words.size(); i++ ){
temp += Words.get(i).getName() + " - " + Words.get(i).getDefinition() + "\n";
}
return temp;
}
public class Word {
String name;
String definition;
public Word(String name, String definition){
this.name = name;
this.definition = definition;
}
public String getName(){
return name;
}
public String getDefinition(){
return definition;
}
}//End Word
}//End dictionary
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;
public class Read {
private static Scanner in;
public static void main(String[] args) throws FileNotFoundException {
File Read = new File ("Dictionary.txt");
in = new Scanner (Read);
//ArrayList called save
Dictionary save = new Dictionary();
while (in.hasNext()){
String w1 = in.next();
String w2 = in.nextLine();
save.addWord(w1, w2);
}
System.out.println(save);
String newLine = System.getProperty("line.separator");
System.out.println("Press 1 for Menu");
Scanner en = new Scanner(System.in);
int choice = en.nextInt();
while (choice == (1)){
System.out.println("What would you like to do?"
+ newLine + "1 - List all Words and Definitions"
+ newLine + "2 - List definition for word"
+ newLine + "3 - Change definition"
+ newLine + "4 - Add new word"
+ newLine + "5 - Exit");
int input = en.nextInt();
switch(input){
case 1: System.out.println(save);
break;
case 2: System.out.println("Which word definition would you like to see?");
try{
String type = en.next();
if (type.equalsIgnoreCase(save.findWord(type))){
System.out.println("The word you chose is: " + save.findWord(type));
}
else{
throw new InvalidNameException();
}
}
catch (InvalidNameException n){
}
finally {System.out.println("Sorry, that word cannot be found.");
}
break;
case 3: System.out.println("Which word definition would you like to change?");
try{
String def = en.next();
if (def.equalsIgnoreCase(save.findWord(def))){
System.out.println("What will be the new definition for: " + save.findWord(def));
String define = en.next();
save.addWord(def, define);
}
else{
throw new InvalidNameException();
}
}
catch (InvalidNameException n){
}
finally {System.out.println("Sorry, that word cannot be found.");
}
break;
case 4: System.out.println("Enter your new word");
String wrd = en.next();
System.out.println("What is the definition of this word?");
String define = en.next();
save.addWord(wrd, define);
break;
case 5: PrintStream fin = new PrintStream(new File("Dictionary.txt"));
fin.println(save);
break;
}
}
}
}//End Read
public class InvalidNameException extends Exception{
public InvalidNameException() {
System.out.println("Not a valid word");
}
}//End exception
I changed te findWord metod to this:
public Word findWord(String name){
Word word = new Word("","");
for (int i = 0; i < Words.size(); i++){
if (name.equalsIgnoreCase(Words.get(i).getName())){
word = Words.get(i);
}
}
return word;
}
but now there is an issue with the object types in my Read class in this area:
try{
String def = en.next();
if (def.equalsIgnoreCase(save.findWord(def))){
System.out.println("What will be the new definition for: " + save.findWord(def));
String define = en.next();
save.addWord(def, define);
}
答案 0 :(得分:0)
Scanner#next()
只返回下一个标记。你想要的是Scanner#nextLine()
。
答案 1 :(得分:0)
你的findWord / findDef错了,
public String findWord(String name){
String word = "";
for (int i = 0; i < Words.size(); i++){
if (name.equalsIgnoreCase(Words.get(i).getName())){
Words.get(i);
}
word += Words.get(i).getName();
}
return word;
}
if块实际上没有做任何有用的事情,尝试将下面的行移动到if块中(对两种方法都这样做)