0
私は、ユーザーが書籍に関する情報を入力して出力を得ることができるプログラムを作成しようとしていますが、コードは機能していないようです。私はその下にあるエラーを取得しています。私は新しいプログラミングですので、コードに何が間違っているのか分かりません。不正なフォーマット変換エラーが表示され、エラーを特定できませんか?ブックプログラム
Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.String
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4302)
at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2806)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2753)
at java.util.Formatter.format(Formatter.java:2520)
at java.util.Formatter.format(Formatter.java:2455)
at java.lang.String.format(String.java:2940)
at C3519369.main(C3519369.java:47)
これは以下のコードです。
ここimport java.util.Scanner;
public class books{
public static void main (String[] args) {
final int MAX_BOOKS = 1;
Scanner scan = new Scanner (System.in);
String[] title = new String [MAX_BOOKS];
String[] author = new String [MAX_BOOKS];
double[] price = new double [MAX_BOOKS];
String[] publisher = new String [MAX_BOOKS];
String[] isbn = new String [MAX_BOOKS];
for (int i = 0; i < MAX_BOOKS; i ++) {
System.out.print("Enter the Title: ");
title [i] = scan.nextLine();
System.out.print("Enter the Author: ");
author [i] = scan.nextLine();
System.out.print("Enter the Price: ");
price [i] = scan.nextDouble();
System.out.print("Enter the Publisher: ");
publisher [i] = scan.nextLine();
System.out.print("Enter the ISBN: ");
isbn [i] = scan.nextLine();
scan.nextLine();
}
System.out.println();
System.out.println (String.format(("%-20s %-10s %-10s %-10s"), "Title", "Author", "Price", "Publisher"));
System.out.println (String.format(("%-20s %-10s %-10s %-10s"), "=====", "=====", "=====", "========="));
final String PRETTY_PRINT = "%-20s %-10s %-10s %6.2f";
double totalPrice = 0.0;
for (int i =0; i < MAX_BOOKS; i ++) {
totalPrice += price [i];
System.out.println(String.format (PRETTY_PRINT, title [i], author [i], price [i], publisher [i], isbn [i]));
}
System.out.println();
System.out.println ("Total Price: " + String.format ("%6.2f", totalPrice));
System.out.println ("Average Price: " + String.format ("%6.2f", totalPrice/MAX_BOOKS));
scan.close();
}
}
。 'f'はString.formatの浮動小数点数を指しますが、文字列を渡しています。 –