0

I am new to Java (along with programming in general). I was working on a personal project where a user types one character to which it is converted to another character. More specifically, a user would type a romanization of a Japanese character to which the Japanese hiragana equivalent is outputted. I am using two separate classes at the moment:

RomaHiraCore.java

import java.util.*;

public class RomaHiraCore
{
public static void main(String[] args)
    {
    Table.initialize();  // Table.java needed!
    Map<String, String> table = Table.getTable();
    Scanner roma = new Scanner(System.in);
    System.out.println("Romaji: ");
    String romaji = roma.nextLine().toLowerCase();

    if (table.containsKey(roma))
    {
        System.out.println(table.get(roma));
    }
    else
    {
        System.out.println("Please enter a valid character (e. g. a, ka)");
    }
    roma.close();
    }
}

Tables.java

import java.util.*;
public class Table
{
    private static Map<String, String> table = new LinkedHashMap<>();
    public static Map<String, String> getTable()
    {
    return table;
    }
public static void initialize()
    {
    // a - o
    table.put("a", "あ");
    table.put("i", "い");
    table.put("u", "う");
    table.put("e", "え");
    table.put("o", "お");
    // ka - ko
    table.put("ka", "か");
    table.put("ki", "き");
    table.put("ku", "く");
    table.put("ke", "け");
    table.put("ko", "こ");
    }
}

If anyone can point me in the right direction, I would greatly appreciate it. I've attempted to go over the documentation, but I can't seem to grasp it (maybe I'm overthinking it). When I run the program, it allows me to enter a character; however, it will only continue to the "else" statement rather than scan Table.java to see if the input matches any of the values listed. Either I'm overlooking something or need to use an entirely different method altogether.

  • 3
    I think you wanted to write table.containsKey(romaji), as romaji is String which you provide. – Hubert Bratek Sep 07 '18 at 21:43
  • Yup, this is a simple typo. If you replace `roma` with `romaji` in `.containsKey` and `.get`, it works fine. – that other guy Sep 07 '18 at 21:46
  • Regarding the reasons why this doesn't cause a compile-time error (which would have saved you from the runtime bug -- and asking the question): [What are the reasons why Map.get(Object key) is not (fully) generic](https://stackoverflow.com/questions/857420/what-are-the-reasons-why-map-getobject-key-is-not-fully-generic). – Mick Mnemonic Sep 07 '18 at 21:51
  • Sorry for wasting your time with such a simple typo. At least you didn't spend an entire hour looking at documentation (just to realize that it was a typo hour the entire time). (: I will definitely check out why using Map.get() doesn't give me a compile-time error (really would have been nice in this case). – Allan Boswell Sep 07 '18 at 22:10

2 Answers2

1

In your map, you have String keys, and the String which you provide is in the romaji variable. So your if should look like this: if (table.containsKey(romaji)). What is more, in this situation I think that using LinkedHashMap doesn't give you anything, simple HashMap would be as good as LinkedHashMap(even better), because you don't need to maintain insertion order of your characters.

Hubert Bratek
  • 894
  • 10
  • 30
  • Thank you as well! You're right, there's no need for me to use LinkedHashMap when HashMap would be more ideal in this situation. – Allan Boswell Sep 07 '18 at 22:07
0

In your main method, you are currently storing the string value inputted by the user however you never access that variable anywhere else.

You wrote table.containsKey(roma) however roma is the Scanner object and not the string they entered so you should be checking if that string is a valid key by using table.containsKey(romaji).

Next, in your else clause, you ask them to reenter an input but never provide them the chance to because you just terminate the scanner.

What you should be doing is something more like this.

String romaji = roma.nextLine().toLowerCase();

while (true) {
    if (table.containsKey(romaji)) {
        System.out.println(table.get(romaji));
        break;
    }
    else {   
        System.out.println("Enter a valid char:";
        romaji = roma.nextLine().toLowerCase();
    }
}
roma.close();
faris
  • 692
  • 4
  • 18
  • Thank you so much. I can't believe I overlooked that by inserting roma instead of romaji. And thank you for the extra information about restarting the scanner after an incorrect character is entered. :) – Allan Boswell Sep 07 '18 at 22:05
  • Yeah you have to be especially careful with naming variables very similarly. – faris Sep 07 '18 at 22:06