Java string declaration IntelliJ IDEA - java

Trying to declare a string in Java inside the main method of a Console application.
String s = "this is some text";
I get a red underline saying, 'class' or 'interface' expected.
If I change the code to read
String s = new String("this is some text");
everything works, or at least the code compiles. Using JDK 1.8 and have recently upgraded the IDE to version 2016.2.4.
This only occurs when declaring a new String, all other type declarations and initializations work without declaring a new instance, i.e.
int i = 0;
Anyone know why the first declaration won't work?
Similar behaviour is exhibited when trying to write to the console,
System.out.println("this is some text");
The word 'text' is red underlined saying 'class' or 'interface' expected.
EDIT: entire class as requested
package Sandbox;
public class Main {
public static void main(String[] args) {
System.out.println("this fails");
}
}
however
package Sandbox;
public class Main {
public static void main(String[] args) {
System.out.println(new String("this works"));
}
}
See screenshot below of actual code in the IDE. Comments welcome.

Looks like an issue with Language Injections in IntelliJ.
https://www.jetbrains.com/help/idea/2016.2/using-language-injections.html
Disable the Language Injections. That should fix your Problem.
An similiar issue with the println method and string is described here and has been solved by unregistering println from string injections: https://intellij-support.jetbrains.com/hc/en-us/community/posts/206836685-System-out-println-hello-analyze-error

Related

Why is my Java static block is not executing? (Extremely simple example. Other answers don't seem to apply.)

I've included my code below. Following some other examples, I even tried to dynamically load the class in order to force it to run the static block, but that doesn't solve my problem. The class is loaded and class.getName() is printed successfully, but still, when it gets to the last line in the main method it throws an error saying the array is null.
All the other answers address things which don't seem to apply here, like how using the "final" keyword can allow the compiler to skip static blocks. Any help is appreciated!
package helper;
public class StaticTest {
public static boolean [] ALL_TRUE;
private static void setArray(){
ALL_TRUE = new boolean[8];
for(int i=0;i<ALL_TRUE.length;i++){
ALL_TRUE[i] = true;
}
}
static {
setArray();
}
public static void main(String [] args){
ClassLoader cLoader = StaticTest.class.getClassLoader();
try{
Class aClass = cLoader.loadClass("helper.StaticTest");
System.out.println("aClass.getName() = " + aClass.getName());
} catch(ClassNotFoundException e){
e.printStackTrace(System.out);
}
System.out.println(StaticTest.ALL_TRUE[0]);
}
}
In case anyone else lands here, the problem was that I had checked the Netbeans option "Compile on Save" (under Build->Compiling). Somehow, compiling files immediately upon saving was preventing the static block from being run.
Again, thanks to everyone who chimed in to verify that the code itself worked as expected.

I get bugs in whatever i write on my editor using inteliJ in Java.(maybe an IDE problem?) [duplicate]

This question already has answers here:
Does Java support inner / local / sub methods?
(5 answers)
Closed 3 years ago.
After some time I tested this simple code and still I had errors.
The errors that I got on this example was the following:
line 4 cannot resolve method a.
Line 6:first: ';' expected,second: Expression expected,third: Variable 'a' is never used.
So I don't get it. Whatever I try I get errors. I even downloaded some code in java from an IntelliJ developer and when I pasted on my editor I still got errors with methods and even the System.out.println. I tried to write in Eclipse but even there I got errors. Please help. I want so much to study but I am getting frustrated with these things.
public class asdf {
public static void main(String[] args) {
a();
static void a() {
System.out.println("asdfff");
}
}
}
Just get your a() method declaration out of the main :
public class asdf {
public static void main(String[] args) {
a();
}
static void a() {
System.out.println("asdfff");
}
}

Java Lexer and Parser

I am writing a new editor for java using Xtext.
I want parser to parse the main class and replace the method call by actual code.
e.g
Class Test {
public static void main(String[] args ){
System.out.println("Virag");
method();
}
public static method(){
System.out.println("Purnam");
}
}
After parsing I want to return a document like mentioned below.
Class Test {
public static void main(String[] args ){
System.out.println("Virag");
System.out.println("Purnam");
}
}
I achieved this in lexer and parser by return of method body instead of method.
But later in editor, text region gets changed and any edit performed in editor goes wrong.
Character positions in documents are going wrong.
How to fix this problem?
You probably want to use the Code Generation concept of Xtext to transform the source code of your DSL into a new artefact where the methods' body are inlined.

IntelliJ IDEA not showing member field

Problem as picture bellow, is there any option to enable showing member abc?
I am using IntelliJ IDEA CE 2017.2.1 with JDK 9+179.
Here is the sample code above:
public class Test {
#FunctionalInterface
interface IF {
String abc = "abc";
void apply();
}
public static void main(String[] args) {
IF theIF = ()->{};
System.out.println(theIF.abc); // I can print `abc` value here, but...
theIF. // DOT here(press ctrl + space) not show member `abc`
}
}
This is accessing static member with instance qualifier, and is better done as IF.abc. Therefore code completion doesn't offer it right away, but if you press Ctrl+Space second time, it should be suggested.

Error: Main method not found in class... why am I getting this?

Before people start flagging this question as a duplicate, know that I took the time too look at similar questions, and found that the answers to other "Error: Main method not found in class..." were not clearly applicable to my situation (according to my limited understanding of java)
I'm trying to utilize a text to speech api. Eclipse isn't complaining about the following code until I try to compile:
package com.textToSpeech;
import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;
public class FreeTTS {
private static final String VOICENAME_kevin = "kevin";
private String text; // string to speech
public FreeTTS(String text) {
this.text = text;
}
public void speak() {
Voice voice;
VoiceManager voiceManager = VoiceManager.getInstance();
voice = voiceManager.getVoice(VOICENAME_kevin);
voice.allocate();
voice.speak(text);
}
public static void main(String[] args) {
String text = "FreeTTS was written by the Sun Microsystems Laboratories "
+ "Speech Team and is based on CMU's Flite engine.";
FreeTTS freeTTS = new FreeTTS(text);
freeTTS.speak();
}
}
The following error shows up in the console:
Error: Main method not found in class com.textToSpeech.FreeTTS, please define the main method as:
public static void main(String[] args)
The code above obviously has a main method, so does anyone know why I am getting this error, and furthermore how I can fix it?
I think it has something to do with the name of the class. If I change the name of the class to something like t2s and then try to compile, I get this error:
Error: Could not find or load main class com.textToSpeech.t2s
Anybody have any thoughts? Any help would be really appreciated.
You may have messed up your project properties. I don't use eclipse, so I cannot say for sure, but try creating a new project and adding the same code to it without fiddling with the properties. The class name and the file name should be the same, check that. Also make sure that the source file is in the same package folder. If nothing works, just create a new project.
Cheers.

Categories