添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Exception in thread "JavaFX Application Thread" java.util.IllegalFormatConversionException: f != java.lang.Integer

Ask Question

I'm trying to write a program to rate memes based on view count and year entered by the user. I was hoping to have the interface take the view count (an integer) and the years old (an integer) and calculate the meme rating which is just through a linear equation as shown below. The interface loaded as I expected, everything built and compiled fine. However, when I tried to calculate the meme rating for something, what ended up happening is the following error message:

Exception in thread "JavaFX Application Thread" java.util.IllegalFormatConversionException: f != java.lang.Integer
    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 fross_charles_personal_programming_exercise.Fross_Charles_Personal_Programming_Exercise.calculateMemeRating(Fross_Charles_Personal_Programming_Exercise.java:67)
    at fross_charles_personal_programming_exercise.Fross_Charles_Personal_Programming_Exercise.lambda$start$0(Fross_Charles_Personal_Programming_Exercise.java:48)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8411)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:432)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:410)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
    at java.lang.Thread.run(Thread.java:748)

Does anybody know how to fix this issue? Maybe there's a formatting problem I'm not seeing, here? This is my code:

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.geometry.HPos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Fross_Charles_Personal_Programming_Exercise extends Application {
  private TextField tfViewCount = new TextField();
  private TextField tfYearsOld = new TextField();
  private TextField tfMemeRating = new TextField();
  private Button btCalculate = new Button("Calculate");
  @Override // Override the start method in the Application class
  public void start(Stage primaryStage) {
    // Create UI
    GridPane gridPane = new GridPane();
    gridPane.setHgap(5);
    gridPane.setVgap(5);
    gridPane.add(new Label("View Count:"), 0, 0);
    gridPane.add(tfViewCount, 1, 0);
    gridPane.add(new Label("Years Old:"), 0, 1);
    gridPane.add(tfYearsOld, 1, 1);
    gridPane.add(new Label("Meme Rating:"), 0, 2);
    gridPane.add(tfMemeRating, 1, 2);
    gridPane.add(btCalculate, 1, 5);
    // Set properties for UI
    gridPane.setAlignment(Pos.CENTER);
    tfViewCount.setAlignment(Pos.BOTTOM_RIGHT);
    tfYearsOld.setAlignment(Pos.BOTTOM_RIGHT);
    tfMemeRating.setAlignment(Pos.BOTTOM_RIGHT);
    tfMemeRating.setEditable(false);
    GridPane.setHalignment(btCalculate, HPos.RIGHT);
    // Process events
    btCalculate.setOnAction(e -> calculateMemeRating());
    // Create a scene and place it in the stage
    Scene scene = new Scene(gridPane, 400, 250);
    primaryStage.setTitle("MemeRatingGenerator"); // Set title
    primaryStage.setScene(scene); // Place the scene in the stage
    primaryStage.show(); // Display the stage
  private void calculateMemeRating() {
    // Get values from text fields
    int ViewCount =
      Integer.parseInt(tfViewCount.getText());
    int YearsOld = Integer.parseInt(tfYearsOld.getText());
    // Create the meme rating object
    MemeRating MemeRating = new MemeRating(ViewCount, YearsOld);
    // Display meme rating
    tfMemeRating.setText(String.format("$%.2f",
      MemeRating.getMemeRating()));
  public class MemeRating {
      private int ViewCount;
      private int YearsOld;
      /** Default constructor**/
      public MemeRating() {
          this(5000000, 12);
      /** Construct a Meme Rating with specified View Count, and Years Ago
      public MemeRating(int ViewCount, int YearsOld) {
          this.ViewCount = ViewCount;
          this.YearsOld = YearsOld;
      /** Return ViewCount */
      public int getViewCount() {
          return ViewCount;
      /** Set a new ViewCount */
      public void setViewCount(int ViewCount) {
          this.ViewCount = ViewCount;
      /** Return Years Old */
      public int getYearsOld() {
          return YearsOld;
      /** Set a new Year */
      public void setYear(int YearsOld) {
          this.YearsOld = YearsOld;
      /** Find MemeRating **/
      public int getMemeRating() {
          float memeThreshold = -1 * (196019 / 3);
          float memeConstant = 10 / ((5706288 / 12) - (196019 / 3));
          float memeRatio = ViewCount / YearsOld;
          int memeRating = (int) (memeConstant * memeRatio + memeThreshold);
          return memeRating;
   * The main method is only needed for the IDE with limited
   * JavaFX support. Not needed for running from the command line.
  public static void main(String[] args) {
    launch(args);
                Which line is line 48 in the code you have sahred?  As the error emssage shows: "Fross_Charles_Personal_Programming_Exercise.java:48", the problem is in line 48 of file Fross_Charles_Personal_Programming_Exercise.java. You may be trying to convert a null value or string with special char to int.
– Dexter
                Aug 12, 2020 at 16:45
                Its diificult to guess the exact line number from you code. Still I suspect :-  tfViewCount.getText() or tfYearsOld.getText() returning NULL or text with space or other non parse-able special character hence parseInt(..) is throwing Illegal Format Conversion.
– Dexter
                Aug 12, 2020 at 16:57
                The line throwing the exception, as you can tell from the stack trace, is tfMemeRating.setText(String.format(...)). String.format() does not like treating an int (which it autoboxes as an Integer) as a float; provide an explicit cast or conversion.
– James_D
                Aug 12, 2020 at 17:31

New version

tfMemeRating.setText(String.format("$%.2f",
            (float)MemeRating.getMemeRating()));

Or change the getMemeRating method()

 public float getMemeRating() {
        float memeThreshold = -1 * (196019 / 3);
        float memeConstant = 10 / ((5706288 / 12) - (196019 / 3));
        float memeRatio = ViewCount / YearsOld;
        float memeRating =  (memeConstant * memeRatio + memeThreshold);
        return memeRating;
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.