2009年4月26日星期日

Java 常用类型转换 总结

Java中常用到的类型转换集中在String Integer int Object之间,下面对具体转换方法进行归纳总结:

一、Integer与int之间

1、 Integer to int
int num=Integer.intValue();
2、 int to Integer
Integer integer=new Integer(i);


二、Integer与String之间
1、 Integer to String
Integer integer;
integer.toString();

2、String to Integer
String s;
Integer integer(s)


三、String 与 int相互转换(实质多半借助Integer)

1、String to int

1) int i = Integer.parseInt([String]);
或 i = Integer.parseInt([String],[int radix]);

2) int i = Integer.valueOf(my_str).intValue();

注: 字串转成 Double, Float, Long 的方法大同小异.


2 int to String

1) String s = String.valueOf(i);

2) String s = Integer.toString(i);

3) String s = "" + i;

注: Double, Float, Long 转成字串的方法大同小异.


总结:int是基本数据类型,String,Integer,Object都是对象类型,转换的时候





参照上面的关系图
按部就班即可。

2009年4月22日星期三

Java 常用正则表达式小结

正则imba,值得研究。

常用的正则表达式:
Java中的17种常用正则表达式归纳:


  
  01、"^\\d+$"  //非负整数(正整数 + 0)
  02、"^[0-9]*[1-9][0-9]*$"  //正整数
  03、"^((-\\d+)(0+))$"  //非正整数(负整数 + 0)
  04、"^-[0-9]*[1-9][0-9]*$"  //负整数
  05、"^-?\\d+$"    //整数

  06、"^\\d+(\\.\\d+)?$"  //非负浮点数(正浮点数 + 0)
  07、"^(([0-9]+\\.[0-9]*[1-9][0-9]*)([0-9]*[1-9][0-9]*\\.[0-9]+)([0-9]*[1-9][0-9]*))$"  //正浮点数
  08、"^((-\\d+(\\.\\d+)?)(0+(\\.0+)?))$"  //非正浮点数(负浮点数 + 0)
  09、"^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)([0-9]*[1-9][0-9]*\\.[0-9]+)([0-9]*[1-9][0-9]*)))$"  //负浮点数
  10、"^(-?\\d+)(\\.\\d+)?$"  //浮点数
  11、"^[A-Za-z]+$"  //由26个英文字母组成的字符串
  12、"^[A-Z]+$"  //由26个英文字母的大写组成的字符串
  13、"^[a-z]+$"  //由26个英文字母的小写组成的字符串
  14、"^[A-Za-z0-9]+$"  //由数字和26个英文字母组成的字符串
  15、"^\\w+$"  //由数字、26个英文字母或者下划线组成的字符串
  16、"^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$"    //email地址
  17、"^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$"  //url



附具体使用程序:

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class SpeStrFilter {


//过滤特殊字符
public static String StringFilter(String str) throws PatternSyntaxException
{
// 只允许字母和数字
// String regEx = "[^a-zA-Z0-9]";
// 清除掉所有特殊字符


String regExSymbol="--―_`〕「」∶※→←%/О<>●·~『』[]〈〉\"《》.~!б㎡@#$%%^&*()+={}':;;',\\[\\].<>/?~!#¥…&*()+ 【】‘:”“’。,、?";//特殊符号
String regExNum="01234567890123456789ⅣⅤⅥⅦⅧⅨⅩⅪⅫ";//数字
String regExletter="a-zA-Z";//字母
String regEmail ="\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*" ;//E-mail
String regURL = "^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$";
String regTelNum = "\\d{3}-\\d{8}\\d{4}-\\d{7}";//国内电话,xxx-xxxxxxx的格式


//String regEx="[-`<>●『』〈〉\"《》.~!@#$%^&*()+={}':;',\\[\\].<>/?~!@#¥%……&*()——+{} 【】‘;:”“’。,、?1234567890]";


Pattern p = Pattern.compile("["+regExSymbol+regExNum+regExletter+"]"); //选用符号组合(这里用特殊符号+数字,注意顺序,不是数字+特殊符号)
Matcher m = p.matcher(str);
return m.replaceAll("").trim();
}

public static void main(String[] args) throws PatternSyntaxException
{
String str = "*adCVs*34_a _09_b5*[/435^*&城池()^$$&*).{}+..)%%*(*.中国}34{45[]12.fd'*&999下面是中文的字符¥……{}【】。,;’“‘”?";
// String str = " <>\"再 美丽~!@#¥%……&*()- 足 部 美容 中心 采用 专业 技术 和 独特 修复 剂 可 彻底 治愈灰指甲 让 您 再现 靓 丽 双 足 生活 无 忧 无效 退款 诚信 为 本 -";
System.out.println(str);
System.out.println(StringFilter(str));
}

}

参考:http://jiake0504.javaeye.com/blog/182095
http://blog.chinaunix.net/u/21684/showart_435251.html

Java 调用 exe与cmd 小结

有时候,需要在Java中调用现成的.exe文件或是cmd进行操作,下面结合自己的项目实例,做以小结。


现在我们的目标是:用Java调用cmd,执行C:/libsvm/windows/svm-train.exe,svm-train.exe参数为: [options] training_set_file [model_file]
具体是 -c 32 -g 0.0078125 trainset trainset.model
可以将doc窗口的输出显示到JAVA IDE的控制台上。

我们需要用到java.lang.Runtime类
主要成员函数:
getRuntime() 返回与当前 Java 应用程序相关的运行时对象。
Runtime r=Runtime.getRuntime();

exec(String command, String[] envp, File dir) 在有指定环境和工作目录的独立进程中执行指定的字符串命令。 。command为.exe及其参数,envp null即可,dir=new File(FilePath)

java.lang.Process类
主要成员函数:
waitFor() 导致当前线程等待,如果必要,一直要等到由该 Process 对象表示的进程已经终止。
注:Process p = Runtime.getRuntime().exec(arg)

实现代码如下:

import java.io.*;
import java.lang.*;

public class RunExe {

public static void main(String[] arg)
{
Runtime r = Runtime.getRuntime();


try
{
String[] cmd = new String[5];

cmd[0] = "cmd "; //命令行
cmd[1] = "/c "; //运行后关闭,
cmd[2] = "start "; //启动另一个窗口来运行指定的程序或命令(cmd 命令集里的)
cmd[3] = "C:\\libsvm\\windows"; //要运行的.exe程序的目录

cmd[4] = "svm-train -c 32 -g 0.0078125 -v 5 trainset ";//exe程序及其需要的参数
String Naiveexe = "calc.exe";//windows自带计算器
String line;
String space=" ";
//Process p = Runtime.getRuntime().exec("cmd /c svm-train.exe -c 32 -g 0.0078125 -v 5 trainset",null,new File("C:\\libsvm\\windows")); 此时输出到控制台
//Process p = Runtime.getRuntime().exec("cmd /c start svm-train.exe -c 32 -g 0.0078125 -v 5 trainset",null,new File("C:\\libsvm\\windows"));此时弹出dos窗口运行


Process p = Runtime.getRuntime().exec((cmd[0]+cmd[1]+cmd[4]),null,new File(cmd[3]));
//Process p = Runtime.getRuntime().exec("calc.exe"); //直接运行计算器



BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ( (line=br.readLine()) != null)
System.out.println(line);

//p.waitFor();
}


catch(Exception e)
{System.out.println("Error!");

}

OK,enjoy!

2009年4月21日星期二

转义字符小结

记住常用转义字符在某些时候会相当方便。

常用转义字符:

\b:回退:向后退一格

\f:换页

\n:换行,光标到下行行首

\r:回车,光标到本行行首

\t:水平制表

\v:垂直制表

\\:反斜杠

\‘:单引号

\":双引号

\?:问号

\ddd:三位八进制

\40 空格

\xhh:二位十六进制


\0:空字符(NULL),什么都不做。换行只是换一行,不改变光标的横坐标;回车只是回到行首,不改变光标的纵坐标。

2009年4月19日星期日

TFIDF公式

TFIDF是计算向量空间模型中(VSM),词语在文档中所占权值大小的公式
TFIDF=TF xIDF
TF=Term Frequency
IDF=Inverse Document Frequency
具体计算公式如下: 词汇Wi对于文档d的tf为:
tf i(d) = d中Wi出现的总次数/d中所有词汇的总数
idf i = log(总的文档数/包含词汇Wi的文档数+0.1)//0.1 for 数据平滑

为了避免文档长度对tfidf的影响,进行归一化
得到最终公式:
















2009年4月16日星期四

static+

static:
如果一个成员被声明为static,它就能够在它的类的任何对象创建之前被访问,而不必引用任
何对象。你可以将方法和变量都声明为static

Eclipse环境下的Java调试小结

设置断点,debug(Ctrl+F11),跟踪查错
主要快捷键:
F5 本方法内
F6 跳入下一层
F7 跳出该层
F8 调到下个断点


调试经验与技巧