Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Scanner help: What on earth is wrong?  RSS feed

 
AjayG Shah
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,
I am new to JAVA and to this forum. I have been trying some online tutorials and came across one that would use Scanner to get user input. Below is the code for the same and the errors that I am receiving (



import java.util.Scanner;

public class learn_to_add {
Scanner user_input = new Scanner( System.in );

String first_name;
System.out.print("Enter your first name: ");
first_name = user_input.next( );

String family_name;
System.out.print("Enter your family name: ");
family_name = user_input.next( );

String full_name;
full_name = first_name + " " + family_name;


System.out.println("You are " + full_name);

}
}

I have highlighted the error in red. Below are the error messages for it:

Multiple markers at this line
- Syntax error on token ";", { expected after this token
- Syntax error on token ""Enter your first name:"", delete
this token
- Syntax error on token(s), misplaced construct(s)
- Syntax error on token(s), misplaced construct(s)
- Syntax error on token ""Enter your first name: "", delete
this token


I am so lost!! One line, so many errors...but my other question would be: i have the same line of coding for the family name, why doesn't it show as a error too? THANKS FOR YOUR HELP!!! Looking forward to be an active member...
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As is often the case, one exception leads to another, what you have to do is fix the first exception and often the others will go away or make more sense.

Your first exception says this:
Syntax error on token ";", { expected after this token


The exception doesn't give us the correct information, but it does give us what is wrong: Syntax! The problem is you are writing executable lines of code directly in the class body. The cell body* class body can only be used for defining and initializing variables. To execute methods like System.out.print("Enter your first name: "); you need a method. You are missing a line somewhere that defines the method which is supposed to contain your code.


*Edit: doh
 
Paul Clapham
Sheriff
Posts: 22487
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure you copied the code correctly from the tutorial? There's an elementary error there -- as a beginner it's perfectly natural that you wouldn't recognize it, but I find it hard to believe that even the worst online tutorial would contain such an error.
 
AjayG Shah
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah thanks to both of you! I had completely skipped the public static void main line. Wow!! The error message could have simplified my life. Instead it confused me. Lol
 
Paul Clapham
Sheriff
Posts: 22487
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, welcome to the Ranch, Ajay!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!