Da error ya que cerré el escáner en el método principal pero creé un nuevo escáner en el método [duplicado]
Intenté crear un nuevo escáner después de cerrar el primero en el archivo principal. pero da error. Pensé que había creado un nuevo escáner en el método y luego lo cerré para que leyera las nuevas entradas a través de mi método, pero no fue así. ¿Cuál es el problema aquí? chatgpt no pudo iluminarme :(
package gross_calculator;
import java.util.Scanner;
public class GrossPayCalculator {
static void story() {
System.out.println("Which season the day is of?");
Scanner scanner1 = new Scanner(System.in);
String season = scanner1.nextLine();
System.out.println("How is that day?");
String adjective = scanner1.nextLine();
System.out.println("What is the number?");
int number = scanner1.nextInt();
System.out.println("On a "+adjective+" "+season+" day,\nyou read "+number+" pages of book.");
scanner1.close();
}
public static void main(String[] args) {
int hours = 0;
System.out.println("How many hours did you work?: ");
Scanner scanner = new Scanner(System.in);
hours = scanner.nextInt();
double payRate = 0;
System.out.println("What is your pay rate?: ");
payRate = scanner.nextDouble();
scanner.close();
double grossPay = hours*payRate;
System.out.println("Gross pay: "+grossPay);
story();
}
}
mensaje de error:
How many hours did you work?:
23
What is your pay rate?:
12,2
Gross pay: 280.59999999999997
Which season the day is of?
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
at java_essentials/gross_calculator.GrossPayCalculator.story(GrossPayCalculator.java:11)
at java_essentials/gross_calculator.GrossPayCalculator.main(GrossPayCalculator.java:36)
Aceptado
Utilice un solo Scanner
objeto y no lo cierre ya que está leyendoSystem.in
Consulte los comentarios sobre esta respuesta para obtener más aclaraciones.
package gross_calculator;
import java.util.Scanner;
public class GrossPayCalculator {
private static void printStory(Scanner scanner) {
System.out.println("Which season is it?");
String season = scanner.nextLine();
System.out.println("How is the day?");
String adjective = scanner.nextLine();
System.out.println("Enter a number:");
int number = Integer.parseInt(scanner.nextLine());
System.out.println("On a " + adjective + " " + season + " day, you read " + number + " pages of a book.");
}
private static double calculateGrossPay(Scanner scanner) {
System.out.println("How many hours did you work?: ");
int hours = Integer.parseInt(scanner.nextLine());
System.out.println("What is your hourly pay rate?: ");
double payRate = Double.parseDouble(scanner.nextLine());
return hours * payRate;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double grossPay = calculateGrossPay(scanner);
System.out.println("Gross pay: " + grossPay);
printStory(scanner);
}
}
Uso de ejemplo:
How many hours did you work?:
8
What is your hourly pay rate?:
100
Gross pay: 800.0
Which season is it?
Spring
How is the day?
Sunny
Enter a number:
42
On a Sunny Spring day, you read 42 pages of a book.