1

を使用して、メートルのユーザー入力をフィートとインチに変換します。

//     following format  (16ft. 4in.).  Disable the button so that
//     the user is forced to clear the form.

問題は、文字列と int 値の両方を 1 つのテキスト フィールドに入れる方法がわからないことです。 if else ステートメントでそれらを設定する方法よりも

   private void ConversionActionPerformed(ActionEvent e )
   {
         String s =(FourthTextField.getText());
         int val = Integer.parseInt(FifthTextField.getText()); 

         double INCHES = 0.0254001;
         double FEET = 0.3048;
         double meters;

         if(s.equals("in" ) )
         {
             FourthTextField.setText(" " + val*INCHES + "inch");
         }
         else if(s.equals("ft"))
         {
             FourthTextField.setText(" " +val*FEET + "feet");
         }
   }

string と int 値の両方を 1 つに追加することは可能JTextFieldですか?

4

1 に答える 1

2

あなたはできる...

FourthTextField.setText(" " + (val*INCHES) + "inch");

また

FourthTextField.setText(" " + Double.toString(val*INCHES) + "inch");

また

FourthTextField.setText(" " + NumberFormat.getNumberInstance().format(val*INCHES) + "inch");

更新しました

テキストの数値部分を抽出するだけなら、次のようなことができます...

String value = "1.9m";
Pattern pattern = Pattern.compile("\\d+([.]\\d+)?");

Matcher matcher = pattern.matcher(value);
String match = null;

while (matcher.find()) {

    int startIndex = matcher.start();
    int endIndex = matcher.end();

    match = matcher.group();
    break;

}

System.out.println(match);

1.9これにより、 の後のすべてが取り除かれ、が出力されmます。これにより、の数値要素を抽出し、String変換用の数値に変換できます。

これは、整数と小数の両方を処理します。

于 2013-02-20T04:35:37.013 に答える