2011年1月31日
#
StringTokenizer tokenizer
= new StringTokenizer(number, ",");
boolean bool = true;
while (tokenizer.hasMoreTokens()) {
try {
Double.valueOf(tokenizer.nextToken());
} catch (Exception e) {
bool = false;
}
}
//將字符串轉(zhuǎn)化為數(shù)組的方法
int gv[];
int i = 0;
StringTokenizer tokenizer = new StringTokenizer(goodsVolume, ",, ");
gv = new int[tokenizer.countTokens()];//動態(tài)的決定數(shù)組的長度
while (tokenizer.hasMoreTokens()) {
String d = tokenizer.nextToken();
gv[i] = Integer.valueOf(d);//將字符串轉(zhuǎn)換為整型
i++;
}
//字符串解析
private String[] stringAnalytical(String str, String divisionChar) {
String string[];
int i = 0;
StringTokenizer tokenizer = new StringTokenizer(str, divisionChar);
string = new String[tokenizer.countTokens()];// 動態(tài)的決定數(shù)組的長度
while (tokenizer.hasMoreTokens()) {
string[i] = new String();
string[i] = tokenizer.nextToken();
i++;
}
return string;// 返回字符串?dāng)?shù)組
}
int countTokens()
計(jì)算在生成異常之前可以調(diào)用此 tokenizer 的 nextToken 方法的次數(shù)。
boolean hasMoreElements()
返回與 hasMoreTokens 方法相同的值。
boolean hasMoreTokens()
測試此 tokenizer 的字符串中是否還有更多的可用標(biāo)記。
Object nextElement()
除了其聲明返回值是 Object 而不是 String 之外,它返回與 nextToken 方法相同的值。
String nextToken()
返回此 string tokenizer 的下一個(gè)標(biāo)記。
String nextToken(String delim)
返回此 string tokenizer 的字符串中的下一個(gè)標(biāo)記。
public class StringAnalytical {
// 字符串解析,將字符串轉(zhuǎn)根據(jù)分割符換成字符串?dāng)?shù)組
private String[] stringAnalytical(String string, char c) {
int i = 0;
int count = 0;
if (string.indexOf(c) == -1)
return new String[] { string };// 如果不含分割符則返回字符本身
char[] cs = string.toCharArray();
int length = cs.length;
for (i = 1; i < length - 1; i++) {// 過濾掉第一個(gè)和最后一個(gè)是分隔符的情況
if (cs[i] == c) {
count++;// 得到分隔符的個(gè)數(shù)
}
}
String[] strArray = new String[count + 1];
int k = 0, j = 0;
String str = string;
if ((k = str.indexOf(c)) == 0)// 去掉第一個(gè)字符是分隔符的情況
str = str.substring(k + 1);
if (str.indexOf(c) == -1)// 檢測是否含分隔符,如果不含則返回字符串
return new String[] { str };
while ((k = str.indexOf(c)) != -1) {// 字符串含分割符的時(shí)候
strArray[j++] = str.substring(0, k);
str = str.substring(k + 1);
if ((k = str.indexOf(c)) == -1 && str.length() > 0)
strArray[j++] = str.substring(0);
}
return strArray;
}
public void printString(String[] s) {
System.out.println("*********************************");
for (String str : s)
System.out.println(str);
}
public static void main(String[] args) {
String[] str = null;
StringAnalytical string = new StringAnalytical();
str = string.stringAnalytical("1aaa", '@');
string.printString(str);
str = string.stringAnalytical("2aaa@", '@');
string.printString(str);
str = string.stringAnalytical("@3aaa", '@');
string.printString(str);
str = string.stringAnalytical("4aaa@bbb", '@');
string.printString(str);
str = string.stringAnalytical("@5aaa@bbb", '@');
string.printString(str);
str = string.stringAnalytical("6aaa@bbb@", '@');
string.printString(str);
str = string.stringAnalytical("@7aaa@", '@');
string.printString(str);
str = string.stringAnalytical("@8aaa@bbb@", '@');
string.printString(str);
str = string.stringAnalytical("@9aaa@bbb@ccc", '@');
string.printString(str);
str = string.stringAnalytical("@10aaa@bbb@ccc@eee", '@');
string.printString(str);
}
}
摘要: import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar; import java.util.regex.Pattern;
import o...
閱讀全文
java訪問xml文件
- Java code
-
import java.io.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class xmljava
{
public static void main(String args[])
{
Element element=null;
File f =new File("a.xml");
DocumentBuilder db=null; //documentBuilder為抽象不能直接實(shí)例化(將XML文件轉(zhuǎn)換為DOM文件)
DocumentBuilderFactory dbf=null;
try{
dbf= DocumentBuilderFactory.newInstance(); //返回documentBuilderFactory對象
db =dbf.newDocumentBuilder();//返回db對象用documentBuilderFatory對象獲得返回documentBuildr對象
Document dt= db.parse(f); //得到一個(gè)DOM并返回給document對象
element = dt.getDocumentElement();//得到一個(gè)elment根元素
System.out.println("根元素:"+element.getNodeName()); //獲得根節(jié)點(diǎn)
NodeList childNodes =element.getChildNodes() ; // 獲得根元素下的子節(jié)點(diǎn)
for (int i = 0; i < childNodes.getLength(); i++) // 遍歷這些子節(jié)點(diǎn)
{
Node node1 = childNodes.item(i); // childNodes.item(i); 獲得每個(gè)對應(yīng)位置i的結(jié)點(diǎn)
if ("Account".equals(node1.getNodeName()))
{
// 如果節(jié)點(diǎn)的名稱為"Account",則輸出Account元素屬性type
System.out.println("\r\n找到一篇賬號. 所屬區(qū)域: " + node1.getAttributes().getNamedItem ("type").getNodeValue() + ". ");
NodeList nodeDetail = node1.getChildNodes(); // 獲得<Accounts>下的節(jié)點(diǎn)
for (int j = 0; j < nodeDetail.getLength(); j++)
{ // 遍歷<Accounts>下的節(jié)點(diǎn)
Node detail = nodeDetail.item(j); // 獲得<Accounts>元素每一個(gè)節(jié)點(diǎn)
if ("code".equals(detail.getNodeName())) // 輸出code
System.out.println("卡號: " + detail.getTextContent());
else if ("pass".equals(detail.getNodeName())) // 輸出pass
System.out.println("密碼: " + detail.getTextContent());
else if ("name".equals(detail.getNodeName())) // 輸出name
System.out.println("姓名: " + detail.getTextContent());
else if ("money".equals(detail.getNodeName())) // 輸出money
System.out.println("余額: "+ detail.getTextContent());
}
}
}
}
catch(Exception e){System.out.println(e);}
}
}
- XML code
-
<?xml version="1.0" encoding="gbk"?>
<Accounts>
<Account type="by0003">
<code>100001</code>
<pass>123</pass>
<name>李四</name>
<money>1000000.00</money>
</Account>
<Account type="hz0001">
<code>100002</code>
<pass>123</pass>
<name>張三</name>
<money>1000.00</money>
</Account>
</Accounts>
方法沒有確認(rèn)
import java.util.*;
public class Lottery {
public static void main(String[] args) {
int[] data1 = {3, 5, 6, 8, 9, 15, 18, 24, 27, 30, 32};
Random r = new Random();
int irdm = 0;
for(int i = 0; i < 7; i ++) {
irdm = r.nextInt(11 - i);
System.out.println(data1[irdm]);
for(int j = irdm; j < 11 - i - 1; j ++) {
data1[j] = data1[j + 1];
}
}
}
}
方法沒有確認(rèn)不知道是否可行public class AAAAA {
public static void main(String[] args) {
int i = 0;
int j = 0;
int[] temp = new int[20];
for (j = 0; j < temp.length; j++) {
temp[j] = Math.random() * 100 + 1;
System.out.print(temp[j] + ","
;
}
HashSet hh = new HashSet();
while (hh.size() < 7) {
int aa = (int) (Math.random() * 100 + 1);
hh.add(aa);
}
System.out.println(hh.size());
Iterator ii = hh.iterator();
while (ii.hasNext()) {
System.out.print(ii.next() + ","
;
}
}
}
java從指定數(shù)組中取不重復(fù)的7個(gè)隨機(jī)數(shù)
如何用java從指定數(shù)組中取不重復(fù)的7個(gè)隨機(jī)數(shù),以下是我寫的代碼,但是是有重復(fù)的,哪位大俠賜教一下如何使用Random類的種子,幫我實(shí)現(xiàn)不重復(fù)的隨機(jī)數(shù)。。。
import java.util.*;
public class Lottery {
public static void main(String[] args)
{
int[] data1 = {3,5,6,8,9,15,18,24,27,30,32};
Random r=new Random();
StringBuffer str1=new StringBuffer();
for(int i=0;i<6;i++)
{
str1.append(data1[r.nextInt(11)]);
}
System.out.println(str1);
}
}