0

I started coding recently and I got stuck for several days already. So I need to get "value" from "key", which is in table like:

    one uno
    two dos
    three tres
    four cuatro

N.B.: words are separated by tab, therefore using "/t"

and what newbie me could accomplish looks something like this:

    import java.util.*;
    import java.io.*;
    public class test {
       public static void main(String[] args)throws FileNotFoundException, IOException {  
    Scanner scanner = new Scanner(new FileReader("C:\\Users\\Admin\\workspace\\test_dict\\src\\source.txt"));

        HashMap<String,String> map = new HashMap<String,String>();

        while (scanner.hasNextLine()) {
            String[] columns = scanner.nextLine().split("\t");

            map.put(columns[0],(columns[1]));
//enter a key word
   System.out.println("Enter a word");
   String[] columns = scanner.nextLine();
//ignoring upper and lower case
          columns=columns.toLowerCase();
// returns the element associated with the key
           System.out.println(map.get(columns[])); 

    //close files
       scanner.close();
        }
       }
    }

The problem is: I want to be able to type in the console the key word and get associated value to it. eg:

Enter a word:
    one
    uno

But I can't use map.get(columns[]); and when I try map.get(columns[0]); for some weird reason it shows me columns[1] so in console i got

Enter a word:
uno
Enter a word:
dos
Enter a word:
tres
Enter a word:
cuatro
Nurbek Kuantyrov
  • 167
  • 1
  • 11
  • `String[] columns = scanner.nextLine()` Why do you try to reuse the variable `columns` and why do you think that `#nextLine` returns an array? *"when I try `map.get(columns[0]);` for some weird reason it shows me `columns[1]`"* Why is that weird? `Map#get` returns the value associated to the passed key and this is obviously `columns[1]`, when you pass `columns[0]`. – Tom May 23 '16 at 09:38

2 Answers2

0
     scanner.nextLine();

the output of nextLine() is string not array of string so you need to do it like this

    String consoleInput = scanner.nextLine();
    System.out.println(map.get(consoleInput.toLowerCase())); 
Amer Qarabsa
  • 6,412
  • 3
  • 20
  • 43
0

There are several problems:

  • You tried to read the file and your input in a single while-Statement. So your translation will not be imported complete and you try already to search in it.
  • The type are not secure, because you are using the scanner variable for reading your file "and" the console input.

Here an example, how I tried would like to resolve it more clearly:

import java.util.*;
import java.io.*;

public class Test {
    public static void main(String[] args)throws Exception {

        Scanner scanner = new Scanner(new FileReader("C:\\Users\\Admin\\workspace\\test_dict\\src\\source.txt"));

        HashMap<String,String> map = new HashMap<String,String>();

        while (scanner.hasNextLine()) {
            // you are loading a String Array from file to into "columns[]"
            String[] columns = scanner.nextLine().split("\t");

            // The array of the line is splittet in inserted in your map
            map.put(columns[0],(columns[1]));

            // That is all, you have to do the input in the outside of your reading while statement

            //close files
            scanner.close();
        }

        // Using input from console: http://stackoverflow.com/questions/4644415/java-how-to-get-input-from-system-console
        String input = null;
        while ((input = getConsoleInput("Enter a word: ")) != null) {
            // Use only the input for key-Search
            System.out.println(map.get(input));
        }


    }

    // Helper method for reading input
    public static String getConsoleInput(String message) throws  IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print(message);
        String input = br.readLine();
        return input;
    }
}
Norbert Koch
  • 533
  • 6
  • 17
  • That really helped me! Console showed an error at first, but I moved "scanner.close();" and put it under console input and problem resolved. Everything works perfectly now! Thank you sir! – Nurbek Kuantyrov May 24 '16 at 08:59