-1

我想验证一个数字(例如 701234567)是否是我在 java 中的数组的一个元素。为此,我的代码搜索是否以 7 开头且有 9 位数字的号码是我的数组“numbercall.txt”的一个元素,该数组有 5 个元素。这是我的文本文件: numbercall.txt [ 702345678, 714326578, 701234567, 791234567,751234567] 这是我的代码:

        import java.io.BufferedReader;
        import java.io.DataInputStream;
        import java.io.FileInputStream;
        import java.io.IOException;
        import java.io.InputStreamReader;
        import java.util.ArrayList;
        import java.util.List;
        import java.util.regex.Matcher;
        import java.util.regex.Pattern;

        public class TestNumberLt  {

            static String[] arr= null;
            String filename = "fichiers/numbercall.txt";
            static String a = null ;
            static List<String> list = new ArrayList<String>();
            public static void main(String [] args) throws IOException{
                FileInputStream fstream_school = new FileInputStream(filename); 
                DataInputStream data_input = new DataInputStream(fstream_school); 
                BufferedReader buffer = new BufferedReader(new InputStreamReader(data_input)); 
                String str_line; 
                while ((str_line = buffer.readLine()) != null) 
                { 
                    str_line = str_line.trim(); 
                    if ((str_line.length()!=0))  
                    { 
                        list.add(str_line);
                                } 
                }
                int  b = 773214576;
                   //convert the arraylist to a array
                        arr = (String[])list.toArray(new String[list.size()]);
                Pattern p = Pattern.compile("^7[0|6|7][0-9]{7}$");
                Matcher m ; 
                    //a loop for verify if a number exist in this array
                for (int j = 0; j < list.size();)
                {
                    System.out.print(" "+list.get(j)+ "  ");
                    m = p.matcher(list.get(j));
                    /*while(m.find())
                    System.out.println(m.group());*/
                            if(list.get(j).equals(b))
                        { 
                            System.out.println("Trouvé "+list.get(j));
                            break;
                        }
                    else 
                    {
                        System.out.println("ce numéro ("+b+") n'existe pas!");
                        }
                    break;
                }

            }
        }
4

3 回答 3

0

另一个答案,使用番石榴:(在这种情况下,确实没有必要,您可以简单地使用 String 对象的 split() 方法,但就像番石榴的可读性和返回一样)

package stackoverflow;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.google.common.base.Splitter;

public class RegexExample {

String filename = "numbercall.txt";

public boolean isInList(String numberToCheck) throws IOException {
    BufferedReader file = loadFile();
    for (String number : extractNumberListFrom(file)) {
        if (number.trim().equals(numberToCheck)) {
            return true;
        }
    }
    return false;
}

private Iterable<String> extractNumberListFrom(BufferedReader buffer) throws IOException {
    StringBuilder numberList = new StringBuilder();
    String line;
    while ((line = buffer.readLine()) != null) {
        numberList.append(line);
    }
    return Splitter.on(",").split(numberList.toString());
}

private BufferedReader loadFile() {
    InputStream fstream_school = RegexExample.class.getResourceAsStream(filename);
    BufferedReader buffer = new BufferedReader(new InputStreamReader(fstream_school));
    return buffer;
}
}
于 2013-05-15T14:34:36.783 回答
0

像这样简单地做

        String str_line= "702345678,714326578,701234567,791234567,751234567";
        String[] strArray = str_line.split(",");
        String key = "702345678";
        for(String v:strArray) {
            if(v.equals(key)) {
                System.out.println("found");
            }
        }
于 2013-05-15T13:32:10.780 回答
0

我不确定你想要什么,但如果你只需要b数组中的索引,只需执行以下操作:

public static void main(String [] args) throws IOException{
    ...
    int  b = 773214576;
    int tmp = list.indexOf(b+"");
    if(tmp!=-1) {
        System.out.println("Trouvé "+ b + " à l'index " + tmp);
    } else {
        System.out.println("Ce numéro ("+b+") n'existe pas!");
    }
    ...
}
于 2013-05-15T13:35:17.043 回答