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
Related
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.
I've been trying to use Jansi for the first time on a Java program, but I can't import org.fusesource.jansi.AnsiConsole and get an error saying that this package does not exist.
I downloaded the latest Jansi version under C:\Users\Leonardo\Jansi where the jansi-1.17.1.jar is (I do not know if you have to do anything with this file after that or not). The text editor I am using for the program is TextPad and I know that to use Jansi I have to manually add it to its environment variables. I tried doing with by going into
Configure > Preferences > Environment Variables
and adding a new variable
named: %CLASSPATH%
with the
value: C:\Users\Leonardo\Jansi\jansi-1.17.1.jar;%CLASSPATH%;
If anyone has any experience with TextPad and Jansi I would appreciate any help you could give me. I am also open to switching to a different text editor or ide if someone has used Jansi with another program. Below is the sample code I've been using to see if I set it up correctly, no luck so far.
import org.fusesource.jansi.AnsiConsole;
public class JansiDemo{
public static final String CLS = "\u001b[2J\u001b[1;1H";
public static final String RED = "\u001b[31;1m";
public static final String GREEN = "\u001b[32;1m";
public static final String YELLOW = "\u001b[33;1m";
public static final String BLUEONWHITE = "\u001b[34;47m";
public static void main(String[] args){
AnsiConsole.systemInstall();
System.out.println(RED + "apple " + YELLOW + "banana " + GREEN + "pepper");
System.out.println(BLUEONWHITE + "this is blue on white" + NORMAL);
}
}
This is what the console outputs when I compile it:
E:\game.java:2: error: package org.fusesource.jansi does not exist import org.fusesource.jansi.AnsiConsole;
E:\game.java:16: error: cannot find symbol AnsiConsole.systemInstall();
symbol: variable AnsiConsole
location: class game
2 errors
Tool completed with exit code 1
To set up a classpath in Textpad (I am using version 8.1.2) you have to download the .jar file first and then go to into Configure > Preferences... > Environment Variables > New. Then set name:CLASSPATH and the value to the directory of the .jar file in my case value: C:\Users<User_Name>\Jansi\jansi-1.17.1.jar;%CLASSPATH%; After this hit Ok and Apply and restart any command prompt you have open to save any changes.
I have this Java code:
public class Calc {
public int quotient(int a, int b){
return a/b;
}
}
and TestNG unit test for this method:
#Test ()
public void testingMethod3() {
Assert.assertEquals(0, calc.quotient(5,0));
}
On my work computer I successfully get
java.lang.ArithmeticException: / by zero
message, as expected.
But when my colleague runs this test on home computer, then mentioned exception is not throwing and test passes.
How this magic could occur?
P.S.
Environment
OS: Windows 10
TestNG version: 6.13.1
Java version: 8 (don't know exact build version)
P.P.S.
Deletion of target folder and rebuilding of the project was that very helpful solution. Seem like IDE cashed old project sources, and didn't flush them after changes in the code.
In the past, I experienced something like yours. Because different JDK compile environment and/or JRE runtime environment. And need check the different of version of TestNG.
check by add few line of code to print Java properties.
Properties p = System.getProperties();
Enumeration keys = p.keys();
while (keys.hasMoreElements()) {
String key = (String)keys.nextElement();
String value = (String)p.get(key);
System.out.println(key + ": " + value);
}
then comparing
In the following code:
public void connect()
{
/*
String selectedPort = "COM1";
String selectedPort = (String)window.cboxPorts.getSelectedItem();
selectedPortIdentifier = (CommPortIdentifier)portMap.get(selectedPort);
*/
}
Eclipse complains that selectedPort cannot be resolved. Why is it even looking inside of a comment?
First, you need to refresh the project.
Second, clean and rebuild. This must be happening as the new class file is not generated for you java file and eclipse is still looking at the previous compiled form.
I took this code directly out of Thinking in java 4th edition and it keeps giving me 'cannot find symbol: method isLowerCase(Char). Is the book wrong, do I maybe not have the character class with all my files, or am I doing a really really really stupid mistake?
public class ListCharacters{
public static void main(String[] args) {
for(char c = 0; c < 128; c++)
if(Character.isLowerCase(c))
System.out.println("value:"+(int)c+ " character: " + c);
}
}
Do you have a class called Character in the same folder as your ListCharacters class?
On its own, your ListCharacters class compiled fine for me. However, when I added a class called Character to the same folder, I got a 'cannot find symbol' error for the isLowerCase method.
There's nothing wrong with the name of the method or how it's been used, so I'm afraid it does suggest your Java installation has a problem. What version of the JDK have you installed? As far as I can tell isLowerCase() has always been there so I'm not quite sure what could be wrong. Are you compiling from a plain text file using the command-line Java compiler, or using an IDE such as Eclipse? It might be that if you're using an IDE then you haven't pointed it to the correct version of Java.