【问题标题】:.equalsIgnoreCase Strings are not comparing correctly.equalsIgnoreCase 字符串没有正确比较
【发布时间】:2015-08-03 20:36:33
【问题描述】:

这个项目是一个可编辑的字典程序。简短而甜蜜,我试图遍历已添加到字典中的单词,并将用户输入与扫描仪进行比较,以与已添加的单词进行比较。 我的问题是,无论输入多么准确,我创建的异常总是被调用。 我不确定异常是错误的还是字符串没有正确比较。字典单词是从名为“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);
                }

【问题讨论】:

标签: java file-io arraylist exception-handling string-comparison


【解决方案1】:

Scanner#next() 只返回下一个令牌。你要的是Scanner#nextLine()

【讨论】:

    【解决方案2】:

    你的 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 块中(对两种方法都这样做)

    【讨论】:

    • 我知道这是基于 OP 代码,但不要在循环内使用 stringResult += newPart,因为每次创建新的 StringBuilder 时都需要复制当前的 stringResult,附加到它 @ 987654324@,将其转换为字符串并存储回stringResult。而是使用一个 StringBuilder 并且每次都附加到它的新部分。当你完成并且你需要 String 使用 toString() 方法。
    • 我只是复制了他的代码,并没有改变任何东西,我知道这是一种不好的做法,我只是把他推向了正确的方向,而不是给他代码
    • 是的,我知道这是 OP 代码,但我必须为可能还不知道它的未来用户编写它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 1970-01-01
    • 1970-01-01
    • 2020-09-16
    • 2016-01-03
    相关资源
    最近更新 更多