Answer the question
In order to leave comments, you need to log in
Java console input. Why can't I enter a string?
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int y=in.nextInt();
float x=in.nextFloat();
in.next();
String h=in.nextLine();
System.out.printf("x = %.3f, y =%d, h=%s \n", x,y, h);
in.close();
}
}
Answer the question
In order to leave comments, you need to log in
After entering the numbers, the program closes, and h remains unspecified. Please write the correct version of the program and why the above version does not work?
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int y=in.nextInt();
float x=in.nextFloat();
in.nextLine();
String h = in.nextLine();
System.out.printf("x = %.3f, y =%d, h=%s \n", x,y, h);
in.close();
}
}
why do you need in.close() in which cases to add in which it is not necessary?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question