0

I have this following input.

    3
    sam
    99912222
    tom
    11122222
    harry
    12299933
    sam
    edward
    harry
    mark
    john

and this is my code

public static void main(String[] argh) {
    Map<String, Integer> map = new HashMap<String, Integer>();
    List<String> keyList, outputList = new LinkedList<String>();
    Scanner in = new Scanner(System.in);
    int N = in.nextInt();
    for (int i = 0; i < N; i++) {
        String name = in.next();
        int phone = in.nextInt();
        in.nextLine();
        map.put(name, phone);
    }
    keyList = new ArrayList<String>(map.keySet());
    String s = in.nextLine();
    while (in.hasNext()) {
        if (keyList.contains(s)) {
            outputList.add(s + "=" + map.get(s));
        } else {
            outputList.add("Not found");
        }
        s = in.nextLine();
    }
    in.close();
    for (int i = 0; i < outputList.size(); i++) {
        System.out.println(outputList.get(i));
    }
}

My problem is that I could not determine whether the input has ended or not because while (in.hasNext()) { freezes after the last input. How could I close the scanner after the last input?

enter image description here

Pallav Jha
  • 3,409
  • 3
  • 29
  • 52
  • Can you not move `String s = in.nextLine();` as the first line of the while? – OneCricketeer Jan 09 '16 at 10:09
  • @cricket_007 even after that same thing happens – Pallav Jha Jan 09 '16 at 10:10
  • 1
    You have an infinite loop, that only ends when the standard input is ends. But standard input never ends. It's not like a file which has only a fixed number of lines. It constantly expects the user to type something. So you need to instruct the end user to type some special string that your code would recognize as then end of the input, and stop looping once you've read that special string. – JB Nizet Jan 09 '16 at 10:12
  • What about `hasNextLine` instead? – OneCricketeer Jan 09 '16 at 10:13
  • You should define a "stop" input-value (eg when given input = "-1") break while – Kostas Kryptos Jan 09 '16 at 10:13
  • Duplicate question was surprisingly hard to find . And I was sure there has been lots. – Basilevs Jan 09 '16 at 10:14
  • I cannot have any special input as the test cases are provided to me from hackerrank.com – Pallav Jha Jan 09 '16 at 10:29
  • @JBNizet This is not an infinite loop, condition in the while loop keeps on waiting or the input after taking the last input of the test case. – Pallav Jha Jan 09 '16 at 10:32
  • Sidenote: you don't need `keyList`. You can directly check if the map contains the key – OneCricketeer Jan 09 '16 at 10:32
  • @PallavJha that's pretty much the definition of an infinite loop: a loop that loops until something that will never happen happens. – JB Nizet Jan 09 '16 at 10:59

1 Answers1

1

Your while loop should be:

while (in.hasNextLine()) {
    String s = in.nextLine();
    if (s.isEmpty())
        break;
    if (keyList.contains(s)) {
        outputList.add(s + "=" + map.get(s));
    } else {
        outputList.add("Not found");
    }
}

That way an empty line in input denotes the end of input, meaning you just have to press Enter again to exit program.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • I have edited the question. After working on the last input, code freezes at the next while loop condition. – Pallav Jha Jan 09 '16 at 10:37
  • @PallavJha That is because the program doesn't know if more input will be provided. You haven't told it that you're done. Simply stopping typing on the keyboard is not a sign of completion, just a sign of being a slow typer, as far as program is concerned. You have to do *something* to say "I'm done". Here I'm suggesting a blank line means that. – Andreas Jan 09 '16 at 10:43
  • so you mean I have to try another approach other than Scanner – Pallav Jha Jan 09 '16 at 10:48
  • @PallavJha I mean that when receiving input from the console, there is no end-of-data, since the user can always type more input, so you need some way for the user to say "I'm done". That is not something specific to `Scanner`, *all* ways of reading input from the console has that problem. – Andreas Jan 09 '16 at 18:44
  • Thanks. the input test cases are from hackerrank so I cannot put any extra characters in the input to stop the loop. Is there a way to apply what you have said. – Pallav Jha Jan 09 '16 at 18:48