|登录 |注册

查看: 2273|回复: 9
打印 上一主题 下一主题

关于Java Validation的问题...JAVA

[复制链接]
yclaw1015
2011-10-18 01:07 AM
我目前要做一分project, 做完了~大致上都可以运行
我想请教大家的是怎样做validation,我的project是给user enter value(Hex,Oct,Bin) 然后 会convert去 Decimal number.

可是当user 选择 value(Oct) 时候,然后User 按 a, 然后 convert是时候会有run time error, 因为(oct)只是(0-7)嘛...

所以大家有什么方便可以解决呢?
当user选择 Hex, 只可以enter 0-9,a,b,c,d,e,f
当user选择 Oct, 只可以enter 0-7
当user选择 Bin, 只可以enter 0,1
Super-Tomato
2011-10-18 06:57 AM
我目前要做一分project, 做完了~大致上都可以运行
我想请教大家的是怎样做validation,我的project是给use ...
yclaw1015 发表于 2011-10-18 01:07 AM



去看看 Java.util.regex 的 package 提供了你甚麽方法
~Zero
2011-10-18 09:28 AM
不然用 try...catch 也可以。
yclaw1015
2011-10-18 03:16 PM
不然用 try...catch 也可以。
~Zero 发表于 2011-10-18 09:28 AM



    try and catch 不是真真的VAlidation的方法...
~Zero
2011-10-18 03:32 PM
coding 没有分真假,只有对错,好坏,快慢。

try catch 只是其中一个方法。
番茄提示的 regex 也是其中一个方法。
肯定还有其他方法的。
yclaw1015
2011-10-18 04:09 PM
coding 没有分真假,只有对错,好坏,快慢。

try catch 只是其中一个方法。
番茄提示的 regex 也是其中 ...
~Zero 发表于 2011-10-18 03:32 PM



    regex 不太明白,刚才看了几遍,我目前学会 if, else, for loop这些~我想用for loop来read每个char
for(int i=0;jtfValue.getText().length()>i;i++){
                    if(jtfValue.getText()== '0'){
                     
                    }
可是好像行不通
~Zero
2011-10-18 04:18 PM
本帖最后由 ~Zero 于 2011-10-18 04:22 PM 编辑

http://download.oracle.com/javas ... va/lang/String.html
String 里面有一个 matches 的 method 可以用。

http://download.oracle.com/javas ... a/lang/Integer.html
Integer 里面有一个 parseInt(int, radix) 的 method 也可以用。
当然用 parseInt 的话需要配合 try...catch 来用。

你要 loop 的话,很麻烦咯,人家几行 code,你要几十行。
如果你坚持要 loop 的话,你可以看看 String 里面的 charAt() method,或 toCharArray(),
你的 coding 错了。
yclaw1015
2011-10-18 04:42 PM
String 里面有一个 matches 的 method 可以用。


Integer 里面有一个 parseInt(int, radix) 的 metho ...
~Zero 发表于 2011-10-18 04:18 PM



    haha~ 我看我还是研究matches好了 :-D
yclaw1015
2011-10-18 04:45 PM
String 里面有一个 matches 的 method 可以用。


Integer 里面有一个 parseInt(int, radix) 的 metho ...
~Zero 发表于 2011-10-18 04:18 PM



    parseInt(int, radix) 我是拿来算 decimal的...Integer.parseInt(xxx.getText(),16);
winmxbb
2011-11-15 07:52 PM
我目前要做一分project, 做完了~大致上都可以运行
我想请教大家的是怎样做validation,我的project是给use ...
yclaw1015 发表于 2011-10-18 01:07 AM



    你可以
if(x.equals('0')||x.equals('2')....) {
     return true;
}
else {
     return false;
}
您需要登录后才可以回帖 登录 | 注册

JBTALKS.CC |联系我们 |隐私政策 |Share

GMT+8, 2026-4-28 01:42 AM , Processed in 0.094785 second(s), 25 queries .

Powered by Discuz! X2.5 © 2001-2012 Comsenz Inc.

本论坛言论纯属发表者个人意见,与本论坛立场无关
Copyright © 2003-2012 JBTALKS.CC All Rights Reserved

Dedicated Server powered by iCore Technology Sdn. Bhd.

合作联盟网站:
JBTALKS 马来西亚中文论坛 | JBTALKS我的空间 | ICORE TECHNOLOGY SDN. BHD.
回顶部