我有一个我已经完成的代码,它基本上打印出从歌曲文本文件中的歌词中搜索到的单词。我想弄清楚的一件事是如何制作,以便如果用户输入他们在歌曲中搜索的单词,它可以输入大写还是小写?
我尝试做等于忽略大小写,但它似乎不喜欢它。这是我的程序下面的代码:
/*
* 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");
}
}
}