分割字符串时的数组索引输出绑定

时间:2017-10-11 07:14:37

标签: java split

我的txt文件中有一个数据列表,如下所示:

"abc@gmail.com","10.9.2.34"
"abc@gmail.com","10.9.2.34"
"abc@gmail.com","10.9.2.34"
"abc@gmail.com","10.9.2.34"
"abc@gmail.com","10.9.2.34"
"abc@gmail.com","10.9.2.34"

我将读取txt文件中的所有输入并存储到DailyFileData。之后,我想从数据中提取IP addressgmail.com,但我无法实现。mail1变量将存储来自txt文件的"abc@gmail.com","10.9.2.34"我已经搜索了多个解决方案,以便在stackoverflow和其他网站中指定两个分隔符。但是我得到ArrayIndexOutOfboundException。错误发生在以下代码行

String IP1 = Emaildata1[1];



这是我的源代码

 for(int c = DailyFileData.size()-1;c>=0;c--)
       {
        String mail1 = DailyFileData.get(c);
        MailCount1 = Collections.frequency(DailyFileData,mail1);
        System.out.println("Count of mail is :"+MailCount1);
        String filename1 = f.getCanonicalPath();
        Pattern pattern = Pattern.compile("\\d{4}-\\d{2}-\\d{2}");
        Matcher match = pattern.matcher( (CharSequence) filename1);
         while (match.find()) 
        {
           date1 = match.group(0);
        }
         String [] Emaildata1 = mail1.split(CSVSplitBy);
         System.out.println(Emaildata1);
         String domain1 = Emaildata1 [0];
         String IP1 = Emaildata1[1];
         String [] domainArray1 = domain1.split(emailSplitBy);
         String domain2 = domainArray1[0];

         System.out.println("The extracted domain is :"+domain2);
         System.out.println("The extracted date is :"+date1);
         System.out.println("The extracted IP is :"+IP1);
         domainAndCounting1.add(domain1);
         domainAndCounting1.add(String.valueOf(MailCount1));
             Iterator<String> it = DailyFileData.iterator();
                     while(it.hasNext())
                          {
                            String mail2 = it.next();
                            if(mail2.equals(mail1))
                               {
                                 it.remove();
                                 c--;
                               }
                          }
            }

1 个答案:

答案 0 :(得分:1)

我不明白你在代码中想要实现的目标。但是,如果您String "abc@gmail.com","10.9.2.34"并且您尝试仅提取IP address和域名gmail.com,则可以尝试以下方法而不会出现任何模式匹配的复杂情况:

String a = "\"abc@gmail.com\",\"10.9.2.34\"";
        String arr[] = a.split(",");
        System.out.println(Arrays.toString(arr));

        for (String aa : arr) {
            if (aa.contains("@")) {
                System.out.println(aa.substring(aa.indexOf("@") + 1, aa.length() - 1));
            } else {
                System.out.println(aa.substring(1, aa.length() - 1));
            }
        }

N.B。:这不是最好的解决方案。这只是解决你的小问题。希望这会有所帮助。