1

So I want to make a command line calculator that takes several arguments after the word "Calculator ". But with the following codes I cannot achieve it. In both of those methods the gradle waits to read the line before displaying the word "Calculator ".

Using Scanner

System.out.print("Calculator ");
Scanner sc = new Scanner(System.in);
String inputString;
inputString = sc.nextLine();
String[] args = inputString.split( "\\s+" );

Using BufferReader

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Calculator ");
String str= br.readLine();
String[] args = str.split( "\\s+" );

However the System.out.println("Calculator ") displays the word without any problem. But it's not in line. So it doesn't work as I want. Changing the order of the code lines also didnot help.

System.Console().printf() is also not an option as I am using Intellij IDEA. So can someone please help me with this so that I could get an inline input reader. I also tried to synchronize the printing method by implementing it separately in a method. That didn't work as well.

Finally what I want to achieve is the following format

Calculator max 12 23 25 45

  • Here the italicized is the input to be read.
  • Bold part should be printed by the program.

This is the complete code enter image description here

before entering input the console ..

enter image description here

after entering the input

enter image description here

Latest Update

So I tried to do the same with a Maven project. Both of the above methods worked perfectly when I did with a maven project in Intellij.

Therefore it must be something to do with gradle's way of running the project. I want to know whether there is a chance for that to happen ?

t T s
  • 75
  • 1
  • 10

4 Answers4

4

I was able to get the output by modifying gradle setting as: File --> Settings --> Build, Execution, Deployment --> Gradle. There is a dropdown Build and Run using: which has options of Gradle and IntelliJ IDEA. Grade was default. I picked IntelliJ IDEA and my System.outs became alive again. enter image description here

Naeem
  • 171
  • 8
1

This is a bug when using IntelliJ IDEA gradle plugin that received so little attention that IntelliJ team seems not so motivated to solve this.

The only workaround I could figure out is to skip the gradle plugin and call gradle wrapper directly from IntelliJ IDEA. To do this you can either use my IntelliJ IDEA plugin Gradle Run with Arguments (GitHub Link), or manually create a run configuration that invokes gradle wrapper whenever you want to run your program. I'd suggest you try my plugin first and see if it helps.

To manually create a run configuration that invokes gradle wrapper, the easiest way is through a JAR Configuration in IntelliJ IDEA. To make this possible you also need to patch the gradle/wrapper/gradle-wrapper.jar's MANIFEST and append a Main-Class entry. In this way you can launch gradle wrapper from IntelliJ IDEA and enjoy System.out.print(...).

Limitation: This approach won't allow you to use the built-in debugger feature because IntelliJ IDEA tries to debug gradle wrapper itself instead of your actual program.

b1f6c1c4
  • 11
  • 1
-2

Call System.out.flush() before you start processing the input after you have done any output. The IDE is connecting to the processes with a pipe and that is not automatically flushed into on every output.

wilx
  • 17,697
  • 6
  • 59
  • 114
  • I'm sorry but it doesn't seem to work. ```System.out.print("Calculator "); System.out.flush(); Scanner sc = new Scanner(System.in); String inputString; inputString = sc.next(); ``` This is what I did – t T s Oct 16 '19 at 06:13
-2

Instead of sc.nextLine() you should use sc.next().

r0-
  • 2,388
  • 1
  • 24
  • 29
  • But the problem is not that, _Calculator_ word is still not displayed before it waits for the user to enter arguments on the console – t T s Oct 16 '19 at 06:16
  • I am actually testing the above part only in a main method. I am buidling using gradle. – t T s Oct 16 '19 at 06:32
  • I added my entire code and the outputs in the question. – t T s Oct 16 '19 at 07:33