Compilation error: ID expected

import java.io.*; public class details { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("What is your name?"); String name = in.readLine(); ; System.out.println("Hello " + name); } 

my problem with this code is that I get an "id" when compiling. can anyone help me what to do? or better, can someone provide a code that BufferedReader will use to request a name, address and age, and then finally display the output.

thanks!!

+6
source share
4 answers

You have not defined a method around your code.

 import java.io.*; public class details { public static void main( String[] args ) { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("What is your name?"); String name = in.readLine(); ; System.out.println("Hello " + name); } } 

In this case, I assumed that you want your code to be executed in the main method of this class. Of course, it is possible that this code goes in any other way.

+16
source

You need to wrap your next code in a block (either method or static).

 BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("What is your name?"); String name = in.readLine(); ; System.out.println("Hello " + name); 

Without a block, you can only declare variables and more than assign them a value in a single expression.

For the main () method, there will now be a better choice:

 public class details { public static void main(String[] args){ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("What is your name?"); String name = in.readLine(); ; System.out.println("Hello " + name); } } 

or If you want to use a static block, then ...

 public class details { static { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("What is your name?"); String name = in.readLine(); ; System.out.println("Hello " + name); } } 

or if you want to create another method, then ..

 public class details { public static void main(String[] args){ myMethod(); } private static void myMethod(){ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("What is your name?"); String name = in.readLine(); ; System.out.println("Hello " + name); } } 

Also worry about exception due to BufferedReader.

+7
source

You will also have to catch or throw an IOException. See below. Not always the best way, but it will give you the result:

 public class details { public static void main( String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("What is your name?"); String name = in.readLine(); ; System.out.println("Hello " + name); } } 
+1
source

only the variable / object declaration statement is written outside the method

 public class details{ public static void main(String arg[]){ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("What is your name?"); String name = in.readLine(); ; System.out.println("Hello " + name); } } 

here is an example, try to study a java book and look at the syntax, then try to develop a program

0
source

Source: https://habr.com/ru/post/903842/


All Articles