Aleksey Kontsevich Aleksey Kontsevich - 1 year ago 138
Java Question

Read console in Java in debug mode

Run following program in IntelliJ IDEA:

package org.stepic.java;

import java.util.ArrayDeque;
import java.util.Iterator;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
ArrayDeque<Integer> deque = new ArrayDeque<>();

//try (Scanner scan = new Scanner(System.in)) {
try (Scanner scan = new Scanner("1 2 3 4 5 6 7")) {
int i = 0;
while(scan.hasNext()){
if(scan.hasNextInt()) {
Integer number = scan.nextInt();
if((i++ % 2) == 1) deque.add(number);
} else {
scan.next();
}
}

Iterator<Integer> it = deque.descendingIterator();
while(it.hasNext()) {
Integer num = it.next();
System.out.printf("%s ", num.toString());
}
} catch(Exception e) {
}
}
}


If I run it, I can enter some in console then press Ctrl-D and it will be stored in scanner. If I do same thing in debugger, IDEA just skips line:

Scanner scan = new Scanner(System.in)


and I can't enter anything. In this case scanner buf is empty. How to enter text in console in debug mode?

Answer Source

Stdin works in debug mode (F5) correctly

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download