史萊姆論壇

史萊姆論壇 (http://forum.slime.com.tw/)
-   程式語言討論區 (http://forum.slime.com.tw/f76.html)
-   -   Java 形態轉換問題 (http://forum.slime.com.tw/thread206316.html)

leowang 2007-05-24 07:49 PM

Java 形態轉換問題
 
語法:

while ((str = fr.readLine()) != null)
          {
                                String[] s = str.split("\\s+");//將字串以空白做區隔 存入陣列
                                System.out.println(s[0]+" ");
                                //for (int i = 3; i < s.length; i++)
                                //System.out.print(s[i]+" ");
                                //System.out.println();
                count++;
                  str1=s[0];
                  str2=s[6];
              String[] b = str1.split(":");
            System.out.println(b[2]+" ");
              String[] c = str2.split(":");
            System.out.println(c[2]+" ");
         
          }

如何把切出來的字串 轉換為Double型態 讓它可以做運算阿
例如 b[2] 和c[2]這兩個陣列 把內容轉為Double型態

saltv2 2007-05-24 09:53 PM

可以試著使用Double這物件來轉...
例如..
String str = "12345";//這是字串型態
double d;//宣告一個double的變數
d = Double.parseDouble(str);//將str這個字串轉換成double型態並assign到d這個變裡
System.out.println(d+1);//印出d這個變裡並加1...測試用的...

注意...使用parsexxx時轉換的字串必需要是數值..否則會throw exception

若有不對的地方還請各位大大指教...
小弟己經一年多沒碰java了...

leowang 2007-05-25 12:52 AM

對喔 還有此方法
來試看看.....感謝大大的解說

snoopy 2007-05-25 01:47 AM

Double 物件裡
parseDouble(String s)
//Returns a new double initialized to the value represented by the specified String, as performed by the valueOf method of class Double.

leowang 2007-05-25 10:06 PM

感謝snoopy大大的解說
看來我的功力還有待加強


所有時間均為台北時間。現在的時間是 05:58 PM

Powered by vBulletin® 版本 3.6.8
版權所有 ©2000 - 2024, Jelsoft Enterprises Ltd.

『服務條款』

* 有問題不知道該怎麼解決嗎?請聯絡本站的系統管理員 *


SEO by vBSEO 3.6.1