+ 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(); } }
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);
}
}
+ 3
Remove the first line (package Stringvar;).
+ 1
even after removing 1st line. It
is not giving expected outputđ
+ 1
yes remove package
+ 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
+ 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);
}
}
0
first_name=user_input.nextLine();
i guess u skipped nextLine();