Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading System.in for Intellij using Console

I need to open up the console and type in inputs for my an assignment using Intellij. Eclipse has a way of doing this using the Scanner class and reading System.in but running the same code in IntelliJ does not work as I can't type anything into the console.

Is there any way to do this?
My code is as follows:

    public class BasicAssertions {
       @Test
       public void testAssertions(){
           System.out.println("Enter: ");
           Scanner reader = new Scanner(System.in);
           int first = reader.nextInt();
           int second = reader.nextInt();
           String s = reader.next();
           String s2 = reader.next();
           assertTrue(first<=second);
           assertFalse(first+second >100);
           assertNotEquals(s,s2);
           assertNotNull(s2);    
       }    
   }
like image 779
daidaidai Avatar asked Sep 29 '14 04:09

daidaidai


People also ask

How do I use console in IntelliJ?

Create a query consoleClick a data source and select File | New | Query Console. Right-click a data source and select New | Query Console. Click a data source, press Alt+Insert , and select Query Console. Click a data source, press Ctrl+Shift+F10 , and select New Query Console.

How do I get console output in IntelliJ?

In the run/debug configuration that will be used for launching the app, click the Logs tab. The Edit Log Files Aliases table displays the list of log files. Select Save console output to file Specify the path to the file.

How do I get full console log in IntelliJ?

Navigate to Editor > General > Console. Check the Override console cycle buffer size (1024 KB) option, if it's not already checked. Increase the default value of 1024 KB to your desired size. You could set it to 4096 KB, for example, which is four times the default size.

How do I set system properties in IntelliJ?

Configure platform properties:From the main menu, select Help | Edit Custom Properties. If you do not have any project open, on the Welcome screen, click Configure and then select Edit Custom Properties. If you cannot start IntelliJ IDEA, manually create an empty idea.


2 Answers

Just click on the console window and type, it works for me on IntelliJ 13 CE. See the image below, I clicked in the console and wrote the text (it appears in green then, I typed enter and it shows up):

enter image description here

like image 120
lc2817 Avatar answered Oct 25 '22 00:10

lc2817


Resolved. A public static void main() method is required for the correct console to appear, otherwise running using the default JUnit Test configuration will only result in a console that doesn't receive inputs.

like image 42
daidaidai Avatar answered Oct 25 '22 01:10

daidaidai