-2

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.

doop_dev
  • 29
  • 7
  • 3
    Show us the entire class / code where you are doing this – TheLostMind Sep 23 '16 at 09:41
  • It might have been caused due to a flaw in the structure of your program. Perhaps you mismatched braces. – progyammer Sep 23 '16 at 09:42
  • Have you imported `com.sun.org.apache.xpath.internal.operations.String`? – Lyubomyr Shaydariv Sep 23 '16 at 09:43
  • @LyubomyrShaydariv No, this is a brand new project opened as a Console application template in IntelliJ. No external packages have been added. – doop_dev Sep 23 '16 at 09:52
  • You're clearly doing something wrong and/or not posting your actual code as the statement you claim is marked as invalid is perfectly valid and IntelliJ knows that quite well. There's a serious problem elsewhere, probably in the way you structured your project. Did you even create it as a Java project? – jwenting Sep 23 '16 at 10:12
  • @jwenting I created it as a new Java project then as a template type of Console application from the main IntelliJ window. No other external libraries were included. I'm not claiming the code is invalid, I *know* the code should be valid but it isn't. I also *know* that something else is wrong somewhere, I just don't know what, that's what I was hoping someone could help with. It's also why I opened a console application as it's an easy way to test code. I'll post a screenshot of the code you seem to think I've doctored or edited so you can see the error message from IntelliJ. – doop_dev Sep 23 '16 at 10:23
  • [link](https://postimg.org/image/75k2q0lv9/) – doop_dev Sep 23 '16 at 10:30
  • @doop_dev Is there really a JDK 1.8 reference in _Project Structure -> Modules -> your_module_name -> Dependencies -> 2nd column_? Also, make sure _Project Structure -> Platform Settings -> SDKs -> Classpath_ targets to a real location. Are there any issues? `Ctrl+B` at the `String` must navigate to the `java.lang.String` class definition. – Lyubomyr Shaydariv Sep 23 '16 at 10:59
  • @LyubomyrShaydariv Yep, there really is. I can provide a screenshot if you wish, but 1.8 is the only version on the machine. Previous projects have been fine, the only thing that has changed is the upgrade to the IDE, but I don't believe for a second that JetBrains has done something. It has to be a local setting but no clue what. I picked the console app as a simple environment to test code. Any ideas, shout up. – doop_dev Sep 23 '16 at 14:46

1 Answers1

2

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

tak3shi
  • 2,305
  • 1
  • 20
  • 33
  • This looks like it might be an actual solution, thank you. This isn't the first project I've had in IDEA though so I'm curious as to what's changed. It's not the IDE upgrade surely and the JDK hasn't changed. Thank you though. – doop_dev Sep 23 '16 at 14:59
  • It was the println function. I de-registered it and it was fine. Accepted as an answer, thank you. Would love to know how that had been changed in between versions but no matter, you fixed it and that's all I care about. – doop_dev Sep 26 '16 at 07:19