Intellij Idea scanner.nextLine() returns empty String - java

I've noticed a weird behavior in Intellij Idea Community Edition run window.
Here is my code
Scanner scanner = new Scanner(System.in);
var list = new ArrayList<Integer>();
String[] arr = scanner.nextLine().split(" ");
for (String number : arr) {
list.add(Integer.parseInt(number));
}
// scanner.nextLine();
int n = Integer.parseInt(scanner.nextLine());
When I run it with input
1 2 4 5
3
It will return empty String "" on int n = Integer.parseInt(scanner.nextLine());, unless I uncomment scanner.nextLine() on line 9. Then it works as intended and returns 3.
I have tried to run compiled code in a Terminal window and it works just fine without extra scanner.nextLine().
I am running it on Zorin OS 16.1, IntelliJ IDEA 2022.1.1 (Community Edition) from flathub.
This behavior does not occur when I try it on my Windows 11 machine.
My question is why does it occur and how to have the same behavior on all machines?

As Thomas Kläger pointed out this is a known bug in Intellij Idea 2022.1.1 . Updating to version 2022.1.2 solved the problem.

Related

How to disable IntelliJ IDEA annoying inspections (JAVA)

version: IntelliJ IDEA Community Edition 2020.1.2
There is an error that occurred in Intellij but Eclipse not.
According to the below code, the variable is StringBuilder and it's okay to append an integer or an int by the book.
But why Intellij shows me this error? How to disable the inspections and make the red line disappeaded.
By the way, that code looks fine in eclipse.
my sameaple code
public class test123 {
public static void main(String[] args) {
StringBuilder msg = new StringBuilder();
Double rate = new Double(123);
msg.append(rate == 0 ? (rate == 0 ? 0 : "-" + rate) : String.valueOf(Math.round(rate*100)).replace("0", "") + "折");
System.out.printf("yyuu:"+msg.toString());
}
screenshot
}
The issue seems to be the IntelliJ IDEA bug, javac and eclipse compilers compile the code without errors, though Editor shows the error.
Please follow the issue created for the bug:
https://youtrack.jetbrains.com/issue/IDEA-244854

How to make input ( Scanner ) work on Sublime Text 3 when using JAVA?

I just started learning JAVA and Sublime Text 3 was proposed to me as a great compiler for JAVA code. I downloaded it, started programming and set my build system as JavaC. I wanted to create a quick program adding two numbers given by the user and displaying the result but nothing comes up in the "build" section. Do you have any idea to make that work ?
Here is the code I wanna try:
import java.util.Scanner;
public class Example
{
public static void main(String[] args)
{
int a,b;
Scanner input=new Scanner(System.in);
System.out.println("Enter a number:");
a=input.nextInt();
System.out.println("Enter a number:");
b=input.nextInt();
System.out.println("sum=" + (a+b));
}
}
For future references on how this windows command line or command prompt in windows works.
First check if your computer have the JRE (required to run a Java program) and JDK ( required compile and run Java programs) already install to verify this input the text "java -version" and javac -version into the Command Line. If the Javac is already intalls, however, doesn't show in the command line then you need to follow this sites on how to set up the path depending on your OS.

java assert not throwing exception [duplicate]

This question already has answers here:
How to enable the Java keyword assert in Eclipse program-wise?
(6 answers)
Closed 8 years ago.
I am checking if number is between 1-10 using assert, but if I enter a number beyond 10 it still gives me the result rather than throwing an exception. What am I doing wrong?
import java.util.Scanner;
public class xina {
public static void main(String[] args) throws Exception {
System.out.println("enter any number");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
assert ( num >= 0 && num <= 10 ) : "bad number: " + num;
System.out.println("You entered " + num);
}
}
Assertions are disabled by default in java. You need to manually enable them by adding -ea to your command-line arguments when you invoke the java compiler. I can't tell you how to do this without knowing what compiler/environment you're using.
Edit:
In eclipse, go to the run menu, and click on run configurations. Select the arguments tab and type -ea into the VM arguments.
Are assertions enabled (-ea flag when running program) ?
By default, they are not enabled by the virtual machine.
This article shows you how to do it in eclipse. Could be helpful for you
Go to Run->run configuration
select java application in left nav pan.
right click and select New.
select Arguments tab
Add -ea in VM arguments.
How to enable the Java keyword assert in Eclipse program-wise?

Cannot find the scanner

I have a very bad experienced about the scanner because I am using GUI and JOptionPane. So I am not be able to do the program's interns of scanner because. I am new to it so Please help me, "cannot find the scanner". This is my code so far .
import java.io.*;
import java.util.*;
class MultiplicationTables{
public static void main(String args[]){
int n, c;
System.out.println("Enter an integer to print its multiplication table");
Scanner in = new Scanner(System.in);
n = in.nextInt();
System.out.println("Multiplication table of "+n+" is :-");
for ( c = 1 ; c <= 10 ; c++ )
System.out.println(n+"*"+c+" = "+(n*c));
}
}
If your Java is not version 1.5 or above, Scanner class is not provided.
go to command promt
type "java -version"
check your version.If you have outdated just update. Problem shoud be fixed..
and make sure your IDE or the JDK actually use it.
Code that you have here provided is correct so you can use it after you fix your problem.
First import is redundant.
import java.io.*; // you can remove it
Scanner is a class in java.util package.
Check weather you have properly set your jdk path in your ide, or in your system.
Depends on system that you use:
Windows: Advanced System Settings->Environment Variables->Path check weather there is your path to jdk.
UNIX in console print 'echo $PATCH' and check that if you have there jdk path properly added.
And then you can check your version of java independent to system in console writing
java --version

Console input waiting when it should not

I've just started working with Java, and I've been trying to get the console input to work properly. Here's the code:
System.out.println("Write a word: ");
Scanner keyboard = new Scanner(System.in);
System.out.println("DEBUG 1");
str = keyboard.nextLine();
System.out.println("DEBUG 2");
System.out.println(str);
This should just take input once and the print the input, like this:
Write a word:
DEBUG 1
Hello //My input
DEBUG 2
Hello //Output
But this is what happens:
Write a word:
Hello //My input
DEBUG 1
//Waiting for new input
DEBUG 2
Hello //The first input
So, it seems to me that it's somehow takes the input at the line Scanner keyboard = new Scanner(System.in); and then put it in my variable str. I use gcj to compile with the following commands to compile and run:
javac hello_world.java
java hello_world
EDIT: I've tried running the program on another computer now, using Ubuntu 10.04 64-bit, instead of Ubuntu 10.04 32-bit as before. I ran it the same way and did not recompile, and the program works fine.
Any ideas why it's behaving like this?
There could be a flushing/buffering issue that causes DEBUG 1 to be output later than you expect. I.e., println("DEBUG 1") could be executed before keyboard.nextLine(), but for some reason the output gets held up on the way, and not output until your input is read.
But I take it that you mean that you actually have to enter two newlines, one after Hello and one at the “Waiting for new input” line. If so, this is more difficult to explain. Perhaps a line termination incompatability issue.
Compiling and executing using the standard JDK gives the behavior you expect.
I checked your program in eclipse running on windows using Oracle JDK 6, I got the expected result.
public static void main(String[] args) {
System.out.println("Write a word: ");
Scanner keyboard = new Scanner(System.in);
System.out.println("DEBUG 1");
String str = keyboard.nextLine();
System.out.println("DEBUG 2");
System.out.println(str);
}
Result :
Write a word:
DEBUG 1
hello
DEBUG 2
hello
OpenJDK is much similar to Oracle JDk and it is available for many distros from their package manager. Can you check with openJDK instead of gcj.

Categories