繁体   English   中英

.equalsIgnoreCase字符串比较不正确

[英].equalsIgnoreCase Strings are not comparing correctly

该项目是可编辑的字典程序。 简短而亲切的我试图遍历已经添加到字典中的单词,并将使用扫描仪输入的用户与已经添加的单词进行比较。 我的问题是,无论输入的准确性如何,总是会调用我创建的异常。 我不确定异常是否错误或字符串比较不正确。 字典单词是从名为“ 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);
                }

Scanner#next()仅返回下一个令牌。 您想要的是Scanner#nextLine()

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM