2016-04-19 20 views
-3

私は、ユーザーがテキストフィールドからの年数での融資額と貸出期間を入力させてください、このプログラムを作成し、それが5パーセントから始まる各金利のための毎月の総支払額が表示されますテキスト領域では8分の1ずつ増加します。これは愚かだが、数値処理以外の値が入力された場合の例外処理を追加するための例外処理の追加方法がわからないことがあります。たとえば、年数を5にする代わりに、ユーザーが5を入力します。 。前もって感謝します。 パッケージローン。のjava - 例外処理

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 
import javafx.scene.control.Label; 
import javafx.scene.control.TextField; 
import javafx.scene.control.TextArea; 
import javafx.scene.control.Button; 
import javafx.scene.control.ScrollPane; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.BorderPane; 
import javafx.geometry.Pos; 

public class loan extends Application { 
    protected TextField tfLoanAmount = new TextField(); 
    protected TextField tfNumberOfYears = new TextField(); 
    protected TextArea textArea = new TextArea(); 

    @Override // Override the start method in the Application class 
    public void start(Stage primaryStage) { 
     tfNumberOfYears.setPrefColumnCount(2); 
     tfLoanAmount.setPrefColumnCount(7); 
     textArea.setPrefColumnCount(30); 

     // Create a button 
     Button btShowTable = new Button("Show Table"); 

     // Create a hbox 
     HBox paneForControls = new HBox(10); 
     paneForControls.setAlignment(Pos.CENTER); 
     paneForControls.getChildren().addAll(new Label("Loan Amount"), tfLoanAmount, 
      new Label("Number of Years"), tfNumberOfYears, btShowTable); 

     // Create a scrollPane 
     ScrollPane scrollPane = new ScrollPane(textArea); 

     // Create a pane 
     BorderPane pane = new BorderPane(); 
     pane.setTop(paneForControls); 
     pane.setCenter(textArea); 

     // Create and register handler 
     btShowTable.setOnAction(e -> { 
      print(); 
     }); 

     // Create a scene and place it in the stage 
     Scene scene = new Scene(pane); 
     primaryStage.setTitle("loans"); // Set the stage title 
     primaryStage.setScene(scene); // Place the scene in the stage 
     primaryStage.show(); // Display the stage 
    } 

    private void print() { 
     // Create a output string 
     String output = ""; 
     double monthlyInterestRate; // Monthly interest rate 
     double monthlyPayment; // Monthly payment 

     // Add table header 
     output += "Interest Rate  Monthly Payment   Total Payment\n"; 

     // Calculate and add table with interest rates to output 
     for (double i = 5.0; i <= 8; i += 0.125) { 
      monthlyInterestRate = i/1200; 
      monthlyPayment = Double.parseDouble(tfLoanAmount.getText()) * 
       monthlyInterestRate/(1 - 1/Math.pow(1 + monthlyInterestRate, 
       Double.parseDouble(tfNumberOfYears.getText()) * 12)); 

      output += String.format("%-24.3f%-34.2f%-8.2f\n", i, 
       monthlyPayment, (monthlyPayment * 12) * 
       Double.parseDouble(tfNumberOfYears.getText())); 
     } 

     textArea.setText(output); 
    } 
     public static void main(String[] args) { 
      launch(args); 
    } 
} 
+1

あなたはすでに前に何を試してみましたか?あなたのコードのどこで例外処理が必要ですか?あなたのコードをすべてダンプしても、多くの回答は得られません。 – eestrada

+0

ああ私。これはあなたが簡単にgoogleに投票できる理由です(他人によって)。彼らは意味がありません、ちょうど混乱を制限しようとしています。もし(!(isNumerc(tfLoanAmount.getText()))){)(新しいIllegalArgumentExceptionをスローし;}パブリック静的ブールISNUMERIC(String str)文字 { 戻りstr.matches( " - \\ D +(\\ dは+)?。 ? "); } – MikeJRamsey56

答えて

0

テキストフィールド(tfNumberOfYears)[テキストフィールド]:https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TextField.html、 このテキストフィールドは、メソッドpublic final String getText()を有し、この方法は、文字列を返します。

あなたは[Double.parseDouble(tfNumberOfYears.getText())]を使用する場合:https://docs.oracle.com/javase/7/docs/api/java/lang/Double.html

public static double parseDouble(String s) throws NumberFormatException

例外:

NullPointerExceptionが - 文字列がnullの場合

NumberFormatException - 文字列が解析可能な二重が含まれていない場合。

は、だから、try/catchブロック内でそのコードを入れて、あなたは、ユーザーが5の代わりに5

などを入力したときに望むものにすることができます。

`private void print() { 
    // Create a output string 
    String output = ""; 
    double monthlyInterestRate; // Monthly interest rate 
    double monthlyPayment; // Monthly payment 

    // Add table header 
    output += "Interest Rate  Monthly Payment   Total Payment\n"; 

    // Calculate and add table with interest rates to output 
    for (double i = 5.0; i <= 8; i += 0.125) { 
     monthlyInterestRate = i/1200; 
     try{ 
      monthlyPayment = Double.parseDouble(tfLoanAmount.getText()) * 
      monthlyInterestRate/(1 - 1/Math.pow(1 + monthlyInterestRate, 
      Double.parseDouble(tfNumberOfYears.getText()) * 12)); 
     } 
     catch(NumberFormatException e){ 
      //Here you write the code to manage this exception 

     } 
     try{ 
      output += String.format("%-24.3f%-34.2f%-8.2f\n", i, 
      monthlyPayment, (monthlyPayment * 12) * 
      Double.parseDouble(tfNumberOfYears.getText())); 
     } 
     catch(NumberFormatException e){ 
      //Here you write the code to manage this exception 

     } 
    } 

    textArea.setText(output); 
} 
    public static void main(String[] args) { 
     launch(args); 
}` 

これはどのようにの一例に過ぎませんその例外を処理します。

0

多分このアプローチにはいくつかありますが、私の頭の上から外れると考えることができるのはif文です。どのような種類のエラーが予想されるか、正当な入力でフィルタリングする正規表現にマッチするか、catch/try例えば

// if the userinput string matches a number 
if(userInputNumber.matches("-?\\d+(\\.\\d+)?")) { 
    // put your code here 
else { 
    System.out.println("Input unrecognized. Please type in a number (e.g. "5") 
} 

または多分

try { 
    double userInputNumber = Double.parseDouble(loan.getText()); 
    // do some code 
catch (Exception ex) { 
    System.out.println("Error, unrecognized input."); 
    System.out.println(ex); 
}