why i am getting error while executing this code??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

why i am getting error while executing this code???

package Stringvar; import java.util.Scanner; class StringVariables { public static void main(String[ ] args) { Scanner user_input= new Scanner(System.in); String first_name; System.out.println("enter your first name: "); first_name=user_input.next(); } }

23rd Sep 2016, 1:13 PM
Priya
Priya - avatar
7 Answers
+ 5
correct ans is import java.util.Scanner; class GetInputFromUser { public static void main(String args[]) { String s; Scanner in = new Scanner(System.in); System.out.println("Enter a name"); s = in.nextLine(); System.out.println("You entered name is " + s); } }
23rd Sep 2016, 3:20 PM
Priya
Priya - avatar
+ 3
Remove the first line (package Stringvar;).
23rd Sep 2016, 2:09 PM
Zen
Zen - avatar
+ 1
even after removing 1st line. It is not giving expected output😑
23rd Sep 2016, 2:50 PM
Priya
Priya - avatar
+ 1
yes remove package
23rd Sep 2016, 2:53 PM
bayram akdemir
bayram akdemir - avatar
+ 1
try these changes to your code and you'll get the output you want import java.util.Scanner; class StringVariables { public static void main(String[ ] args) { String first_name; System.out.println("enter your name: "); Scanner user_input= new Scanner(System.in); first_name=user_input.next(); System.out.println("your name is : "); System.out.println(first_name); } } output: enter your name: Ravindra your name is : Ravindra
8th Oct 2016, 5:29 PM
Ravindra
Ravindra - avatar
+ 1
//This could help. import java.util.Scanner; class StringVariables { public static void main(String[ ] args) { System.out.println("Enter your first name: "); Scanner user_input= new Scanner(System.in); String first_name; first_name=user_input.next(); System.out.println(first_name); } }
27th Dec 2016, 3:26 PM
Deep chand
Deep chand - avatar
0
first_name=user_input.nextLine(); i guess u skipped nextLine();
3rd Nov 2016, 6:06 PM
Vivek Thomas
Vivek Thomas - avatar