使程序输出字符串以小写或大写形式输入

时间:2020-05-14 10:21:33

标签: java intellij-idea ignore-case

我有一个完成的代码,该代码基本上可以打印出从歌曲文本文件中的歌词中搜索到的单词。我要弄清楚的一件事是如何使用户输入他们在歌曲中搜索的单词时可以大写还是小写?

我尝试做equals忽略大小写,但似乎不喜欢它。这是我程序下面的代码:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package wordsinsong;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

/**
 *
 * @author p.armstrong
 */
public class WordsInSong {

    /**
     * @param args the command line arguments
     */
    /*
    There is a chance the program will not find out file. We want to make sure when this happens
    Java knows how to address it, in this case we use throws FileNotFoundException. Remember this is not
    good implementation and is used here to qickly "solve" the error
    */
    public static void main(String[] args) throws FileNotFoundException {
        
        /*
        Scanner is not limited to the keyboard, it can scan anything it can iterate through
        Here we provide a file object. Remember if we want to refer to a file like this
        we must make sure it is in our project root directory
        */
        Scanner filein = new Scanner(new File("chop.txt"));
        
        
        /*
        Our HashMap takes two arguments, one for the key (String) and one for the 
        values we want thaty string to refer to (ArrayList)
        */
        HashMap<String, ArrayList<Integer>> map = new HashMap();
        
        int count = 1;      //to count our current position
        
        /*
        We can use Scanners predicate methods to look ahead and have our loop terminate
        where there is nolonger anything to read (there is no "hasNext()"
        */
        while(filein.hasNext()){
          
            /* 
            We get each token from our file. By default Scanner seperates tokens by
            spaces. 
            */
            String word = filein.next();
            
            /*
            We set up our ArrayList. Note we do not instantiate it yet, because
            we do not know which version we want; a new one, or one that already
            exists
            */
            ArrayList<Integer> l;
            
            /*
            We now look at our map and ask, if the current word already in the map?
            If it is, we want to get the ArrayList currently associated with that key.
            Otherwise we need to make a new ArrayList to associate with our new key.
            */
            if(map.containsKey(word)){
                l = map.get(word);
            } else {
                l = new ArrayList();
            }
            /*
            Next we add our current position to our array list, and add replace our current
            version of our key pari (String -> ArrayList) with the new version. 
            */
            l.add(count);
            map.put(word, l);            
            count++;                //we increment our word count position
        }
        
        System.out.println(map);    //prints out map - not really needed
        
        
        /*
        Since we are done scanning a file, we repurpose our Scanner to read keyboard input
        */
        filein = new Scanner(System.in);
        System.out.println("Enter word to search for: ");
        String word = filein.next();
        
        /*
        We check if the word they entered is in our map, and if it is we get the
        associated array list, then we can perform our desired functions on it
        */
        if(map.containsKey(word)){
            ArrayList<Integer> l = map.get(word);
             System.out.println(word.equalsIgnoreCase + " appears " + l.size() + " at locations " + l);
        } else {
            /*
            Otherwise our word is no tin the map, let the user know!
            */
            System.out.println("Word is not in song");
        }

        
    }
    
}

1 个答案:

答案 0 :(得分:1)

您可以做两件事。

1)当用单词填充Map及其找到的单词时,请确保单词是大写字母

  map.put(word.toUpperCase(), l);

或者当您在循环String word = filein.next();中得到单词时,仅word=word.toUpperCase()

2)在地图中搜索单词时,使用户输入的单词大写

 if (map.containsKey(word.toUpperCase()))
相关问题