in some of my code, i am having the error: The preview feature Implicitly Declared Classes and Instance Main Methods is only available with source level 22 and above, which is strange as i only installed java about a week ago and its on about version 17, im using vscode which has supported java for me so far, but after some point of coding one of my classess it has just malfunctioned, my code is here but im not sure if it will be of much use to anyone:
import java.util.ArrayList;
public class An {
String whichA(String text) {
ArrayList<String> anLetters = new ArrayList<>();
anLetters.add("a");
anLetters.add("e");
anLetters.add("i");
anLetters.add("o");
anLetters.add("u");
Character c0 = text.charAt(0);
Character c1 = text.charAt(1);
if ((anLetters.contains(c0.toString().toLowerCase())) || (c0.toString() + c1.toString() == "11")){
return "an";
}
else{
return "a";
}
}
String whichA(Integer number) {
ArrayList<String> anLetters = new ArrayList<>();
anLetters.add("a");
anLetters.add("e");
anLetters.add("i");
anLetters.add("o");
anLetters.add("u");
String text = number.toString();
Character c0 = text.charAt(0);
Character c1 = text.charAt(1);
if ((anLetters.contains(c0.toString().toLowerCase())) || (c0.toString() + c1.toString() == "11")){
return "an";
}
else{
return "a";
}
}
String whichA(Long number) {
ArrayList<String> anLetters = new ArrayList<>();
anLetters.add("a");
anLetters.add("e");
anLetters.add("i");
anLetters.add("o");
anLetters.add("u");
String text = number.toString();
Character c0 = text.charAt(0);
Character c1 = text.charAt(1);
if ((anLetters.contains(c0.toString().toLowerCase())) || (c0.toString() + c1.toString() == "11")){
return "an";
}
else{
return "a";
}
}
String whichA(Double number) {
ArrayList<String> anLetters = new ArrayList<>();
anLetters.add("a");
anLetters.add("e");
anLetters.add("i");
anLetters.add("o");
anLetters.add("u");
String text = number.toString();
Character c0 = text.charAt(0);
Character c1 = text.charAt(1);
if ((anLetters.contains(c0.toString().toLowerCase())) || (c0.toString() + c1.toString() == "11")){
return "an";
}
else{
return "a";
}
}
String whichA(Float number) {
ArrayList<String> anLetters = new ArrayList<>();
anLetters.add("a");
anLetters.add("e");
anLetters.add("i");
anLetters.add("o");
anLetters.add("u");
String text = number.toString();
Character c0 = text.charAt(0);
Character c1 = text.charAt(1);
if ((anLetters.contains(c0.toString().toLowerCase())) || (c0.toString() + c1.toString() == "11")){
return "an";
}
else{
return "a";
}
}
String whichTh(int num) {
String suffix;
num = num % 10;
switch (num) {
case 1: suffix = "st";
break;
case 2: suffix = "nd";
break;
case 3: suffix = "rd";
break;
default: suffix = "th";
break;
}
return suffix;
}
String whichTh(long number) {
String suffix;
int num = (int) (number % 10);
switch (num) {
case 1: suffix = "st";
break;
case 2: suffix = "nd";
break;
case 3: suffix = "rd";
break;
default: suffix = "th";
break;
}
return suffix;
}
String whichTh(float number) {
String suffix;
int num = (int) (number % 10);
switch (num) {
case 1: suffix = "st";
break;
case 2: suffix = "nd";
break;
case 3: suffix = "rd";
break;
default: suffix = "th";
break;
}
return suffix;
}
String whichTh(double number) {
String suffix;
int num = (int) (number % 10);
switch (num) {
case 1: suffix = "st";
break;
case 2: suffix = "nd";
break;
case 3: suffix = "rd";
break;
default: suffix = "th";
break;
}
return suffix;
}
String whichTh(String number) {
String suffix;
int num = Integer.parseInt(number) % 10;
switch (num) {
case 1: suffix = "st";
break;
case 2: suffix = "nd";
break;
case 3: suffix = "rd";
break;
default: suffix = "th";
break;
}
return suffix;
}
}
String convertString(Integer variable){
return variable.toString();
}
String convertString(Long variable){
return variable.toString();
}
String convertString(Double variable){
return variable.toString();
}
String convertString(Float variable){
return variable.toString();
}
int convertInt(String variable) {
return Integer.parseInt(variable);
}
int convertInt(long variable) {
return (int) variable;
}
int convertInt(float variable) {
return (int) variable;
}
int convertInt(double variable) {
return (int) variable;
}
long convertLong(String variable){
int number = Integer.parseInt(variable);
return (long) number;
}
long convertLong(int variable){
return (long) variable;
}
long convertLong(float variable){
return (long) variable;
}
long convertLong(double variable){
return (long) variable;
}
float convertFloat(String variable){
int number = Integer.parseInt(variable);
return (float) number;
}
float convertFloat(int variable) {
return (float) variable;
}
float convertFloat(long variable) {
return (float) variable;
}
float convertFloat(double variable) {
return (float) variable;
}
double convertDouble(String variable) {
int number = Integer.parseInt(variable);
return (double) number;
}
double convertDouble(int variable) {
return (double) variable;
}
double convertDouble(long variable) {
return (double) variable;
}
double convertDouble(float variable) {
return (double) variable;
}
I was just doing some OOP as I'm new to java and coming from python so its been quite an adjustment, but all this object is meant to do is help with some stuf like knowing whether to but a or an before a word, whether to ad st, nd, rd, or th after a number, and converting data types. On VScode the error appears at the very start, and after some testing/debugging i found that it was the data type conversions atthe bottom of my code.
Does anyone know how i could update java (i used the VScode JavaCodingPack-0.4.1.exe for the java installation), any other solutions or whether I should just use something like eclipse (ideally i would like to stick using VScode as i use multiple other programing languages/markup languages/stylers (python, html, css, javascript, and i hope to learn some others like C# in the future).
VSCode really is operating on java22. However, you have a typo: The closing brace of your class An {} definition is halfway through the file and not at the end: In between whichTh and convertString.
That means convertString and all other methods that follow it exist in the void of space: They aren't part of any class definition.
Until java22, that is straight up, flat out, totally not java. You can't do that: The 'top level' can contain a package declaration, import statements, comments, and type definitions. Period. Methods, Constructors, and Fields must be defined inside a type definition.
In java22, that's still the case, but, toss --preview in there and now 'just method declarations out in the void of space' are allowed, and imply that they are part of an implicit unnamed class. The point of all this is solely to make java easy for that very first hour of learning java. The intent is that you can open a text editor, write e.g.:
> nano whatever.java
void main() {
print("Hi there!");
}
> java whatever.java
Hi there!
which is really how it works, at least, with preview enabled. Or soon will be (this also requires the 'all content of the base module imported by default' JEP and a few other JEPs to come together, but, that's the plan).
That explains the funky error message.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With