Topic: java中如何将字符串的每一位用数组存放

  Print this page

1.java中如何将字符串的每一位用数组存放 Copy to clipboard
Posted by: useforever
Posted on: 2007-09-27 11:04

我现在想做一个qq登录的界面,但qq号码只能是数字不能为英文或者其它字符,所以我想利用数组来存放用户输入的每一位字符,然后来判断它是否属于数字0~9之间,如果不是则不能登录,反之则登录成***.如果有其它好的方法也请在此提出,谢谢!

2.Re:java中如何将字符串的每一位用数组存放 [Re: useforever] Copy to clipboard
Posted by: Cappuccino
Posted on: 2007-09-27 13:05


import java.io.*;
class Test
{
public static void main(String[] args)
throws IOException
{
System.out.println("Please input the test string:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
while(line!=null && !line.equals("q"))
{
System.out.println(line.matches("\\d+")); // 正则判断一下就是了~
line = br.readLine();
}
br.close();
}
}


3.Re:java中如何将字符串的每一位用数组存放 [Re: useforever] Copy to clipboard
Posted by: long_5281
Posted on: 2007-11-08 15:22

public static void main(String[] args){
String num = "2343342" ;
byte[] bnum = num.getBytes();
boolean isNum = true ; //true 输入的是全数字、false 输入的不是全数字
for(byte b:bnum){
if(b>=48&&b<=57){
isNum&=isNum ;
}else{
isNum = false ;
}
}
System.out.println(isNum);
}

4.Re:java中如何将字符串的每一位用数组存放 [Re: useforever] Copy to clipboard
Posted by: useforever
Posted on: 2007-11-11 15:44

请问:for(byte b:bnum)是什么意思?我自从学java还为看到过这种for形式的语句,为什么没有初始化\判断条件和修改条件那三个参数呢?请指教!

5.Re:java中如何将字符串的每一位用数组存放 [Re: useforever] Copy to clipboard
Posted by: javafriend
Posted on: 2007-11-12 17:49

为什么不用JAVA自带的正则表达式去判断呢?只要一个语句就OK了。具体怎么用去看看JAVA-API就行了。

6.Re:java中如何将字符串的每一位用数组存放 [Re: javafriend] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2007-11-16 17:56

javafriend wrote:
为什么不用JAVA自带的正则表达式去判断呢?只要一个语句就OK了。具体怎么用去看看JAVA-API就行了。

Yes, I would agree that using Regular Expression is a good item, but for some simple example, my opinion is that Regular Expression might be a little overkilled. And just one thing bear in mind, Regular Expression does not implements Serilizable, hence they cannot be transmitted across the network. I don't like that to happen, that's why I seldom using them.

useforever wrote:
请问:for(byte b:bnum)是什么意思?我自从学java还为看到过这种for形式的语句,为什么没有初始化\判断条件和修改条件那三个参数呢?请指教!

This is a *new* for look introduced by JDK1.5, earlier JDK does not support it.

I would recommend using Jakarta Commons Validator to do this type of validation. If you only have one class to validate, all simple validation is fine and sufficient, however image you have 10000 beans to validate, then you will find out the merits of this Validation Framework. I have written a topic on Jakarta Commons Validator on this forum before, do a search and you shall find it.

Regards,
Jiafan


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923