2011-03-16 91 views
1


我用Java寫了一個程序。
在程序的一部分中,它從網頁中提取一個字符串數字並將其轉換爲Integer。 這在Linux中工作沒有問題。 在Windows中,它運行正常,當我從Netbeans的運行/調試選項運行程序;
但是,當我從Windows和JRE運行它不起作用! (它不正確地提取這個數字)。
這是代碼部分:問題,當在Windows上運行JRE

subcatPagesCount=0; 
subcatPagesCount=Integer.parseInt(cutstring(CM.getResult(),"(صفحه 1 از ",")").trim()); 
lblPagesCount.setText(""+subcatPagesCount); 

和 「cutstring」 的方法是:

private String cutstring(String in,String from,String to){ 
    int st=in.indexOf(from)+from.length(); 
    return in.substring(st,in.indexOf(to,st)); 
} 

它拋出:

java.lang.NumberFormatException: For input string: "tml PUBLIC "-//W3C//DTD XHTM 
L 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd 
"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Typ 
e" content="text/html;charset=UTF-8" /><title>کامپیوتر :: کامپیوت 
ر و قطعات - istgah.com </title><meta name="keywords" content=",istgah,ا 
یستگاه,نیازمندیها , آگهی , تبلیغات , ایران" /><m 
eta name="description" content="┌⌐╪º┘à┘╛█î┘ê╪¬╪▒ :: ┌⌐╪º┘à┘╛█î┘ê╪¬╪▒ ┘ê ┘é╪╖╪╣╪ 
º╪¬ - istgah.com" /><base href="http://www.istgah.com/" /><link rel="stylesheet" 
type="text/css" href="layout/templates/istblue/css/firev4.css" /><link rel="sty 
lesheet" type="text/css" href="layout/templates/istblue/css/style2.css" /></head 
><body> <div style="width:980px; float:right; " dir="rtl" > <div style="widt 
h:213px; height:89px; float:right; " dir="rtl" >   <a href="http://www.is 
tgah.com/" >  <img border="0" alt="istgah.com" width="213" height="39" ali 
gn="top" src="layout/blue/hr_top_right.gif" />  <img border="0" alt="istgah. 
com" width="187" height="50" align="top" src="layout/blue/hr_top_right2.gif" /> 
    </a> <div style="display:none;">  <img border="0" alt="" width="0" hei 
ght="0" align="top" src="layout/blue/hr_top_right_back.gif" /> </div> 
</div> <div align="right" style="width:767px; height:39px; float:left; backgr 
ound-image:url(layout/blue/hr_top_right_back.gif); " dir="rtl" > 
<div style="width:360px; float:right; padding-top:17px; " >    <a hre 
f="http://www.istgah.com">╪╡┘?╪¡┘ç ╪º╪╡┘ä┘è</a> |     <a href="http: 
//www.istgah.com/province/">استانها</a> |     <a href="http: 
//www.istgah.com/advertise/">نسخه قديم</a> |    <a href="http: 
//www.istgah.com/user/">ثبت‌نام</a> |    <a href="http://www.is 
tgah.com/contactus/">تماس باما</a>    </div>   <div ali 
gn="left" style="width:90px; float:right; padding-top:17px; " >   <font 
color="blue" > </font>   </div>   <div align="left" style="width:3 
00px; float:right; padding-top:17px; " >      ╪º┘à╪▒┘ê╪▓: ΓǬ┌ 
å┘ç╪º╪▒╪┤┘å╪¿┘ç╪î █▓█╡ ╪º╪│┘?┘å╪» █▒█│█╕█╣ΓǼ   </div> </div> <div a 
lign="right" style="width:767px; height:50px; float:left; " dir="rtl" > 
<div style="width:260px; float:right; padding-top:20px; " >    <form 
action="http://www.istgah.com/search/ " method="get">     جستج 
و:      <input name="stext" class="inputbox" size="25" /> 
       <input type="image" src="layout/go.gif" align="top" /> 
    </form>    </div>   <div style="width:500px; float:right; " 
>     <table border="0" width="500px" cellspacing="0" class="mc" dir 
="rtl" > <tr> <td> <a href="http://www.istgah.com/fireview/computer/"> 

..... 

http://www.istgah.com/firekeys/key_3551/" > samsung</a> - <a href="http://www.is 
tgah.com/firekeys/key_136241/" > هارد اکسترنال</a> - <a href="http:/ 
/www.istgah.com/firekeys/key_3066/" > ┘?╪▒┘ê╪┤ ┌⌐╪º┘à┘╛█î┘ê╪¬╪▒</a> - <a href="h 
ttp://www.istgah.com/firekeys/key_1535/" > acer</a> - <a href="http://www.istgah 
.com/firekeys/key_13206/" > رایتر</a> - <a href="http://www.istgah.com/fire 
keys/key_3100/" > ساعت</a> - <a href="http://www.istgah.com/firekeys/key_625 
08/" > مودم adsl</a> - <a href="http://www.istgah.com/firekeys/key_3123/" > 
ضبط</a> - <a href="http://www.istgah.com/firekeys/key_203/" > موبایل</a 
> - <a href="http://www.istgah.com/firekeys/key_1276/" > laptop</a> - <a href="h 
ttp://www.istgah.com/firekeys/key_7199/" > msi</a> - <a href="http://www.istgah. 
com/firekeys/key_2270/" > monitor</a> - <a href="http://www.istgah.com/firekeys/ 
key_321385/" > 5200</a> - <a href="http://www.istgah.com/firekeys/key_807/" > cd 
</a> - <a href="http://www.istgah.com/firekeys/key_1870/" > ups</a> - <a href="h 
ttp://www.istgah.com/firekeys/key_927/" > تلویزیون</a> - <a href="http:/ 
/www.istgah.com/firekeys/key_2079/" > مادربرد</a> - <a href="http://www.i 
stgah.com/firekeys/key_1723/" > sony</a> - <a href="http://www.istgah.com/fireke 
ys/key_532/" > بازی</a> - <a href="http://www.istgah.com/firekeys/key_846/" 
> cool disk</a> - <a href="http://www.istgah.com/firekeys/key_51650/" > ╪º┘ä ╪│█ 
î ╪»█î</a> - <a href="http://www.istgah.com/firekeys/key_51/" > ╪»┘ê╪▒╪¿█î┘å ╪»█ 
î╪¼█î╪¬╪º┘ä</a> - <a href="http://www.istgah.com/firekeys/key_13/" > ┌⌐╪º┘à┘╛█î┘ 
ê╪¬╪▒</a> - <a href="http://www.istgah.com/firekeys/key_1766/" > ╪¿╪º╪¬╪▒█î</a> 
- <a href="http://www.istgah.com/firekeys/key_3454/" > dell</a> - <a href="http: 
//www.istgah.com/firekeys/key_12084/" > mp4</a> - <a href="http://www.istgah.com 
/firekeys/key_3637/" > ram</a> - <a href="http://www.istgah.com/firekeys/key_381 
0/" > کارت کپچر</a> -   <br />  </td></tr>   <tr><t 
d align="center" >   <br />   <form action="http://www.istgah.com/s 
earch/ " method="get">   <b>جستجوگر هوشمند آگهی‌های 
ایستگاه: </b>   <input name="stext" class="inputbox" size="30"/
>    <input type="image" src="layout/go.gif" align="top" />  </for 
m>   <br />   </td></tr> </table>   <div>   <table w 
idth="100%" cellpadding="0" cellspacing="0" class="nls" >    <tr><td 
class="ghd" colspan="3" >تازه‌های کامپیوتر :: کامپیوتر 
┘ê ┘é╪╖╪╣╪º╪¬ (╪╡┘?╪¡┘ç 1 ╪º╪▓ 84" 
     at java.lang.NumberFormatException.forInputString(Unknown Source) 
     at java.lang.Integer.parseInt(Unknown Source) 
     at java.lang.Integer.parseInt(Unknown Source) 
     at istgahrobot.doscan$Scan.run(doscan.java:536) 
     at java.lang.Thread.run(Unknown Source) 

什麼問題?
感謝

+1

什麼是運行它的結果呢? – justkt 2011-03-16 18:02:23

+0

它拋出異常「java.lang.NumberFormatException」這是怎麼一回事,因爲比其他一些html頁面的「cutstring」中提取一部分,因此「parseInt函數」無法解析吧! – RYN 2011-03-16 18:07:11

+0

@Snigger - 你的'from'和'to'參數是什麼?而你的'進'是否有好的措施? – justkt 2011-03-16 18:07:56

回答

3

你可能不使用Unicode運行JRE接通。嘗試用file.encoding選項運行代碼:

-Dfile.encoding=UTF8 

或者設置JAVA_TOOL_OPTIONS environment variable-Dfile.encoding=UTF8

更多見Setting the default Java character encoding?

+0

非常感謝。解決了這個問題。 – RYN 2011-03-16 18:15:32

+1

@Snigger - 設置'file.encoding'選項改變你的代碼運行方式的事實意味着其他錯誤。檢查您的代碼,以瞭解「InputStreamReader」的用法,您不明確指定字符編碼。不支持更改'file.encoding',並可能引入其他問題:http://bugs.sun.com/view_bug.do?bug_id=4163515 – McDowell 2011-03-20 21:37:03