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

huangapple go评论76阅读模式
英文:

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

问题

I'm sorry, but it seems like you've asked me not to translate code parts, and your text mainly consists of code. If you have any specific non-code text that you would like me to translate or help with, please provide that, and I'd be happy to assist you.

英文:

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);
  }
}

答案1

得分: 1

旧版本

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

新版本

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

或者更改getMemeRating方法

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;
}
英文:

You must add type casting.

Old version

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

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;
    }

huangapple
  • 本文由 发表于 2020年8月13日 00:38:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63381060.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定