• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting user inputs in Java  RSS feed

 
Gihan Madushanka
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The program that I have mentioned below was written to ask the user's name. But when it is compiled it shows some errors. Errors have been indicated too. Can anybody explain me why these errors have taken place and possible modifications to the code to make it compile successfully.




Errors shown by the cmd

Parent.java:6: error: <identifier> expected
System.out.println("Enter your name:");
^
Parent.java:6: error: illegal start of type
System.out.println("Enter your name:");
^
Parent.java:7: error: <identifier> expected
st_name=input.nextLine();
^
3 errors

Thanks !!!




 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch. Where is your main() method?
 
Gihan Madushanka
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote:Welcome to the ranch. Where is your main() method?

Thanks. Yep I have not added a main method. It must be the error. My bad. Anyway thanks for the help !!!
 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No worries. I always insist that the fundamentals must be clear first. Many a times one gets ahead of himself and in the process makes a mistake that is very basic. Build it step by step, gradually. Ask yourself as many questions as to why things are the way they are. Come back with doubts. Always there to guide.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may not need a main method in that class. But you definitely need methods; those statements cannot be outside methods.
 
Gihan Madushanka
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You may not need a main method in that class. But you definitely need methods; those statements cannot be outside methods.

Thanks I got it.
 
Gihan Madushanka
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote:No worries. I always insist that the fundamentals must be clear first. Many a times one gets ahead of himself and in the process makes a mistake that is very basic. Build it step by step, gradually. Ask yourself as many questions as to why things are the way they are. Come back with doubts. Always there to guide.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!