Glam Prestige Journal

Bright entertainment trends with youth appeal.

When I use Java in VSCode, I get an extra % in the output, like this:

output ends with %

but I didn't input %

My env:

  • java version "1.8.0_201"
  • javac 1.8.0_201
  • zsh

My code:

 class Shuffle1 { public static void main(String [] args) { int x = 3; if (x > 2) { System.out.print("a"); } while (x > 0) { System.out.print("-"); x = x - 1; if (x == 2) { System.out.print("b c"); } if (x == 1) { System.out.print("d"); x = x - 1; } } }
}

Why does this happen, and how can I avoid it?

2

1 Answer

Running the code in the question in Visual Studio Code with the Code Runner extension gives the correct console output:

a-b c-d

To run Java code with Code Runner, save Shuffle1.java by selecting File -> Save, and click the ▶ icon in the upper right corner of the code pane. When you hover the mouse over the ▶ icon a tooltip pops up which says Run Code (Ctrl+Alt+N). The output will be shown in the console pane which is located immediately below the code pane.

code pane in Visual Studio Code
code pane and console in Visual Studio Code

Running Java code with Code Runner in Visual Studio Code is more convenient than running the same code in the terminal because I can debug the Java code from Visual Studio code using the Debugger for Java extension which I also have installed. I can also run the Java code by itself by clicking the ▶ icon without having to create a project for it first. For example when I'm trying a code block from Stack Overflow I usually don't want to create a whole project for it, because I want to try multiple code blocks and throw most of them away after trying them once. I don't even want to create a project for the best code block. Instead I want to save that code in a text file. This Java programming workflow does not create a separate project unless I need one.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy