How to provide user input to java code during Debug mode in Visual Studio Code

Viewed 8566

I'm using Visual Studio Code to develop some code in Java. I tried a simple code as shown below :

    int no_friends;

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter values");
    no_friends = sc.nextInt();
    System.out.println("Value entered is : " + no_friends);

I placed a breakpoint on the sc.nextInt() line and the debugger successfully stopped this line. However, I did not find a way to give an input to the program while it is in debug mode. Is there a way to pass user input values to the code during debug mode ?

3 Answers

Changing the console property to "integratedTerminal" in launch.json fixed the issue for for me.

"console": "integratedTerminal"

Hope that helps.

You can change variable values in the debugger "variables" panel. Right-click on the variable name (obviously it must be in either local or global scope) and you will see a "Set Value" option to change its value going forward.

See vscode data inspection and modification.

Related