• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Scanner help: What on earth is wrong?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 27451
88
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 27451
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And, welcome to the Ranch, Ajay!
 
Put a gun against his head, pulled my trigger, now he's dead, that tiny ad sure bled
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic