| 
                
                  | bombme   
         
  
 | 分享:        ▲
                    
                      ▼         
 
   這我之前寫的,用來判斷使用者輸入的字,不能含有鍵盤上的symbol 其實API裡面有更好的方法可以用,只是我摸了很久還是不會用~_~" 不過這例子跟你問的是差不多的東西...希望你看的懂 這是我修改以前寫的東西PO上來的,有沒有錯誤我也不知道= =... 不過你可以試試,其他高手也可以幫忙指正唷~~       複製程式 
/*使用者輸入的字*/
         String st=a.readLine();
         boolean st=true; 
/*鍵盤上的symbol我一個一個列出來,which is 比較笨的方法= =",等等用來做比較要用的*/
         char[] data = {'~','`','!','@','#','$','%','^','&','*','(',')','-','_','+','=',
            '{','}','[',']','|','\\',':','','"','\'',' ','?','<','>',',','.','/'};
/*
這邊我用double for loop 去檢查 使用者輸入的字串,這邊我不太懂怎麼去解釋
第一個for loop不會解釋= =...假設使用者輸入 abcde 這個loop就會跑五次就對啦
第二個for loop是用來比對上面的symbol,他會一個一個去比對..
*/
         for(int ln=0; ln<st.length(); ln++){      
            for(int ck=0; ck<33; ck++){
               char c = data[ck];
               char l=st.charAt(ln);
               if(c==l){
                  st=false;
               } 
            }   
         }
/*
假如使用者輸入的字裡面沒有那些symbol,列出"input correct"
不然就列出ERROR input, only A~Z, a~z or 0~9 char accepted
*/
            if(st){
               System.out.println("Input correct");   
            }
            else{
               System.out.println("ERROR input, only A~Z, a~z or 0~9 char accepted.");
            }
 
 [ 此文章被bombme在2005-04-14 10:51重新編輯 ] 
 |  
                  | 
                
                  | 
 |  
                  |  x0  [1 樓]
                    
                    
                     From:澳大利亞
 |  Posted:2005-04-14 01:26 | |  |  
                
                  | PeterPan   
         
     
  
 | 分享:        ▲
                    
                      ▼         
 
 試試看使用 String 的 indexOf() 這個 method,它能搜尋 String 物件中,字元或字串的位置,會回傳一個 int 數值,與陣列的索引一樣,傳回的範圍為 0~?,沒找到的話會傳回 -1 indexOf()是一個 overloading 的 method: public int indexOf(char ch) 尋找字元 ch 的所在位置String class 的完整官方說明public int indexOf(char ch,int fromIndex) 從 fromIndex 開始尋找字元 ch 的位置
 public int indexOf(String str) 尋找字串 str 的所在位置
 public int indexOf(String str,int fromIndex) 從 fromIndex 開始尋找字串 str 的位置
 範例一:驗證字元﹝盜用 bombme 提供的原始碼   , plz forgive me﹞複製程式 
public class Test{
    public static void main(String[] args){
        String st="tests";
        boolean test=true;
        char[] data = {'~','`','!','@','#','$','%','^','&','*','(',')','-','_','+','=',
                '{','}','[',']','|','\\',':','','"','\'',' ','?','<','>',',','.','/'};
        for(int ck=0;ck<33;ck++){
            char c = data[ck];
            if(st.indexOf(c) > -1){
                test=false;
            }
        }
        if(test){
            System.out.println("Input correct");
        }
        else{
            System.out.println("ERROR input, only A~Z, a~z or 0~9 char accepted.");
        }
    }
} 範例二:驗證字串複製程式 
public class Test{
    public static void main(String[] args){
        String st="ABCDEFG";
        boolean test=false;
        if(st.indexOf("CDE") > -1){
            test=true;
        }
        if(test){
            System.out.println("Input correct, \"CDE\" in the String");
        }
        else{
            System.out.println("ERROR input, \"CDE\" don't in the String");
        }
    }
}
 |  
                  | 
                
                  | 
 神:在多項技術領域,具有不可取代性且擁有高度權威魔:在某項技術領域,具有不可取代性且擁有高度權威
 妖:有高度實戰經驗,且在專業領域上具有domain-Knowledge
 俠:在技術層面上有過人專業能力
 人:可以獨立運作、設計暨開發系統
 蟲:coder only (my level)
 |  
                  |  x0  [2 樓]
                    
                    
                     From:台灣數位聯合
 |  Posted:2005-04-14 11:22 | |  |  
                
                  | ooxx123 
         
  
 | 分享:        ▲
                    
                      ▼         
 
   以下是我改兩位大大的程式碼成為可以輸入的型態~不知是否有誤= =+ 兩個人的一起偷~   複製程式 
import java.io.*;
public class Test
{
    public static void main(String[] args)throws IOException
    {
        BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("請輸入要判定的字元 :");
        String st=a.readLine();
        boolean test=true;
        char[] data = {'~','`','!','@','#','$','%','^','&','*','(',')','-','_','+','=',
                '{','}','[',']','|','\\',':','','"','\'',' ','?','<','>',',','.','/'};
        for(int ck=0;ck<data.length;ck++)
        {
            char c = data[ck];
            if(st.indexOf(c) > -1)
            {
                test=false;
            }
        }
        if(test)
        {
            System.out.println("Input correct");
        }
        else
        {
            System.out.println("ERROR input, only A~Z, a~z or 0~9 char accepted.");
        }
    }
} 執行之後發現,使用者不只可以輸入字元,也可以輸入字串,而在字串之中,只要出現符號(data中的),程式就會判定使用者所輸入錯誤,不知是否是因為該程式的功能,是在判定使用者全部的輸入有不屬於"文字"的部份??  
 
 [ 此文章被ooxx123在2005-04-14 12:51重新編輯 ] 
 |  
                  | 
                
                  | 
 |  
                  |  x0  [3 樓]
                    
                    
                     From:台灣數位聯合
 |  Posted:2005-04-14 12:42 | |  |  |