According to java -version
this is what my Ubuntu Java environment is like:
java version "1.7.0_51"
OpenJDK Runtime Environment (IcedTea 2.4.4) (7u51-2.4.4-0ubuntu0.12.04.2)
OpenJDK 64-Bit Server VM (build 24.45-b08, mixed mode)
My javac -version
is:
javac 1.6.0_30
How do I change my javac version? Thanks for the tip @SotiriosDelimanolis. ;) (If you are reading this and have the same problem read the comments. I link to a page that describes how to do this on ubuntu).
I am trying to run the following program:
import java.util.*;
class Separate {
public static void main(String[] args) {
String text = "<head>first program</head> <body>hello world</body>";
Set<String> words = new TreeSet<>(); //1 Compiler error
try(Scanner tokenizingScanner = new Scanner(text)) { //2 Compiler Error
tokenizingScanner.useDelimeter("\\W");
while(tokenizingScanner.hasNext()) {
String word = tokenizingScanner.next();
if(!word.trim().equals("")) {
words.add(word);
}
} //end while
} //end try
for(String word: words) {
System.out.print(word + " ");
} //end for
}
I receive these errors upon trying to compile:
Separate.java:8: illegal start of type
Set<String> words = new TreeSet<>();
^
Separate.java:9: '{' expected
try(Scanner tokenizingScanner = new Scanner(text)) {
^
Separate.java:9: ')' expected
try(Scanner tokenizingScanner = new Scanner(text)) {
^
Separate.java:9: ';' expected
try(Scanner tokenizingScanner = new Scanner(text)) {
^
Separate.java:9: 'try' without 'catch' or 'finally'
try(Scanner tokenizingScanner = new Scanner(text)) {
^
Separate.java:24: reached end of file while parsing
}
^
6 errors
These errors seem like they should not be errors. The first error is showing that the diamond notation found in Java 7 is not correct syntax or something when it is correct. This error is shown with 1 above in the comments.
The other errors stemming from the Scanner object creation in the try block is a try with resources that is also a Java 7 feature. This line is marked with 2 above in the source code.
Does anyone know what I am missing?