Making program output string entered in program lowercase or uppercase

59 Views Asked by At

I have a code I have done which basically prints out words searched from lyrics in a song text file. The one thing I am trying to figure out is how to make so that if a user inputs the word they search for in a song, it can be entered uppercase or lower case?

I tried doing equals ignore case but it doesn't seem to like it. Here is the code below here of my program:

/*
 * 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

There are 1 best solutions below

3
Ashishkumar Singh On

You can do two things.

1) When you populate Map with words and its found, ensure that the word is in upper case

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

or when you get the word in your loop String word = filein.next(); just to word=word.toUpperCase()

2) At the time of searching for the word in map, make the word entered by user upper case

 if (map.containsKey(word.toUpperCase()))