Reputation: 235
I'm getting this error on this piece of code and I can't figure out what's wrong.
public class enc {
//The Look-Up Table with the position of all the available characters
public static final String LUT="*, .abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
Scanner sc=new Scanner(System.in);
System.out.print("Input the sentence you want to encode.");
String s= sc.nextLine();
}
Upvotes: 1
Views: 11246
Reputation: 2453
Your lines:
System.out.print("Input the sentence you want to encode.");
String s = sc.nextLine();
Must be inside a method or a system initialiser block. Actually these are function calls which do not return anything. So cannot be in the class directly.
// In a method
public class enc {
Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.print("Input the sentence you want to encode.");
String s = sc.nextLine();
}
}
OR
// In a system initializer block
public class Test {
Scanner sc = new Scanner(System.in);
{
System.out.println("yoyo");
String s = sc.nextLine();
}
}
Actually those methods can be called directly in a class which actually return something. And must also be initialised to a variable of the respective type.
Upvotes: 1
Reputation: 2865
Try:
public class enc {
//The Look-Up Table with the position of all the available characters
public static final String LUT="*, .abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static void main (String args[]) {
Scanner sc=new Scanner(System.in);
System.out.print("Input the sentence you want to encode.");
String s= sc.nextLine();
}
}
Upvotes: 1
Reputation: 5046
You need to put the code inside a method:
public class enc {
//The Look-Up Table with the position of all the available characters
public static final String LUT="*, .abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Input the sentence you want to encode.");
String s= sc.nextLine();
}
}
Upvotes: 2
Reputation: 159784
The lines
Scanner sc = new Scanner(System.in);
System.out.print("Input the sentence you want to encode.");
String s = sc.nextLine();
should be in a code block such as a method rather than the class block
Upvotes: 2