Posted in Java on Mar 10th, 2008
最近弄國科會計畫,所以都在碰 java 的東西,都在弄 sparql 跟 rdf 的東西,昨天弄懂一些基本的 sparql,然而利用 sarql 語法取出來的數值不是我想要的,所以利用正規表示,把字串取代了。
我想取代的字串如下:
( ?url = "LAB221"^^xsd:string )
我只想要中間的 LAB221 的部份,然後我上網找了方法,總共可以使用兩種方法
第一種
String result = "( ?url = \"LAB221\"^^xsd:string )";
// compile pattern
Pattern p = Pattern.compile("[^\"]+\"([^\"]+)\"[^\"]+");
// get matcher
Matcher m = p.matcher(result.replaceAll("[<li></li>]",""));
// test if match
if (m.matches()) {
System.out.println(m.group(1));
}
else
{
System.out.println("error");
}
/* 正規比對 */
上面感謝 ptt qrtt1 給我的一點啟示
Read Full Post »
Posted in Java on Mar 5th, 2008
有時候必須知道輸入的字串是否是整數,如果不是的話,就要重新輸入,這有兩種作法
第一種是使用 try ... catch ... finally 的方法,如下
public class test
{
public static void main(String args[])
{
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
try{
System.out.print("請輸入你要的數字:");
int test = Integer.parseInt(buf.readLine());
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e.toString() + "陣列程式發生錯誤");
}
catch(ArithmeticException e)
[...]
Read Full Post »
Posted in Java on Mar 1st, 2008
其實可以在 linux 底下去寫 shell script 然後去執行 java 程式,而並非用 java 去執行 Linux 指令,不過java也是可以做到執行 shell command,底下就是我寫的 java 測試 code,去列出自己所在的目錄底下的檔案 ls 這個指令
import java.io.*;
import java.net.*;
import java.util.*;
public class runstart{
public static void main(String a[]) throws Exception{
Process pl = Runtime.getRuntime().exec("/bin/ls");
String line = [...]
Read Full Post »
Posted in Java on Feb 27th, 2008
今天剛裝好 jdk 新版 jdk1.6.0_04,如要下載請到 這裡 下載,裝好之後當然底下要找編譯檔案,就是要去 bin 這個資料夾,然後找到 javac 跟 java 執行檔就可以了,不過如果你要在任何地方都要使用這個執行檔,就要去修改 path,設定方法如下
我的電腦右鍵->內容
k
Read Full Post »