Reputation: 67
I have this code input getting using Scanner Class. When ever I run this code it stores only first two values. I can't enter the third variable value.
//Input Code
import java.util.Scanner;
public class Input
{
public static void main(String[] args)
{
try{
Scanner s = new Scanner(System.in);
int i;
float f;
String str;
System.out.println("Enter the Integer value"); //getting.
input from user
i=s.nextInt(); //store the use entered value
System.out.println("Enter the Float value");
f=s.nextFloat();
System.out.println("Enter the String value");
str=s.nextLine();
System.out.println("\nInt: "+i+"\nFloat: "+f+"\nString
"+str); //print the final result
}
catch(Exception e)
{
System.out.print(e);
}
}
}
------end------
Output:
Enter the Integer value
4
Enter the Float value
4.4
Enter the String valueInt 4
Float 4.10
String
Upvotes: 1
Views: 227
Reputation: 198
Kindly change s.nextline() to s.next(). For more details, you can refer below complete code
import java.util.Scanner;
public class Input {
public static void main(String[] args) {
try {
Scanner s = new Scanner(System.in);
int i;
float f;
String str;
System.out.println("Enter the Integer value"); // getting.
// input from user
i = s.nextInt(); // store the use entered value
System.out.println("Enter the Float value");
f = s.nextFloat();
System.out.println("Enter the String value");
str = s.next();
System.out.println("\nInt: " + i + "\nFloat: " + f + "\nString " + str); // print the final result
} catch (Exception e) {
System.out.print(e);
}
}
}
Upvotes: 1