Good morning, I solved the beginning of the following Java code. When I enter a
ID: 3698450 • Letter: G
Question
Good morning, I solved the beginning of the following Java code. When I enter a float, it does not run. The sample output is below, thank you.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
//using scanner for getting input from user
Scanner in = new Scanner(System.in);
String s = in.nextLine();
System.out.println("You entered string: "+s);
int a = in.nextInt();
System.out.println("You entered string: "+a);
float b = in.nextFloat();
System.out.println("You entered float: "+b);
}
}
/*
INPUT:
GeeksforGeeks
12
3.4
OUTPUT:
You entered integer: 12
Enter a float
You entered float: 3.4
*/
Explanation / Answer
Main.java
import java.util.Scanner;
class Main {
public static void main(String[] args) {
//using scanner for getting input from user
Scanner in = new Scanner(System.in);
String s = in.nextLine();
System.out.println("You entered string: "+s);
int a = in.nextInt();
System.out.println("You entered integer: "+a);
float b = in.nextFloat();
System.out.println("You entered float: "+b);
}
}
Output:
GeeksforGeeks
You entered string: GeeksforGeeks
12
You entered integer: 12
3.4
You entered float: 3.4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.