日期:2014-05-20  浏览次数:20791 次

将只包含字母的单词放到String数组中,将数字放到int数组中。
jerry,98|tom,99|jack,10|anne,100
    将只包含字母的单词放到String数组中,将数字放到int数组中。
public   class   ChaiFen   {
      public   static   void   main(String   args[]){
      String   a= "jerry,98|tom,99|jack,10|anne,100 ";
      Pattern   p=Pattern.compile( "([a-zA-Z]+)\\W{1}([0-9]+)\\W{0,1} ");
      Matcher   ma=p.matcher(a);
      System.out.println(ma.matches());
        ma.reset();
                  while(ma.find()){
      System.out.println(ma.group(1));//不知道怎么把这个放到数组里面???

  }
      ma.reset();
    while(ma.find()){
    System.out.println(ma.group(2));  
}
}

谁能帮我修改一下,把那个放到那个数组里面啊

------解决方案--------------------
Java code

public static void main(String[] args) {
     String a= "jerry,98|tom,99|jack,10|anne,100 ";
     Matcher m = Pattern.compile("[a-zA-Z]+").matcher(a);
     List<String> list = new ArrayList<String>();
     while(m.find()){
         list.add(m.group());
     }
     String[] strs = list.toArray(new String[list.size()]);
     for(String s : strs){
         System.out.println(s);
     }
}

------解决方案--------------------
楼主你的正则后面有个空格注意到没有啊。。给的字符串要匹配的话后面应该也有空格啊:
String a= "jerry,98|tom,99|jack,10|anne,100 ";
->
String a= "jerry,98 |tom,99 |jack,10 |anne,100 ";

//code
public class ChaiFen {
public static void main(String args[]){
String a= "jerry,98 |tom,99 |jack,10 |anne,100 ";
Pattern p=Pattern.compile( "([a-zA-Z]+)\\W{1}([0-9]+)\\W{0,1} ");
Matcher ma=p.matcher(a);
System.out.println(ma.matches());
ma.reset();
String[] arr=new String[4];
int i=0;
while(ma.find()){
//System.out.println(ma.group(1));//不知道怎么把这个放到数组里面???
arr[i]=new String();
arr[i]=ma.group(1);
i++;
}
ma.reset();
String[] num=new String[4];
int j=0;
while(ma.find()){
//System.out.println(ma.group(2));
num[j]=new String();
num[j]=ma.group(2);
j++;
}
System.out.print("The Strings:");
for(String str:arr){
System.out.print(str+" ");
}
System.out.println();
System.out.print("The Nums:");
for(String str:num){
System.out.print(str+" ");
}
}
}


打印结果是:
false
The Strings:jerry tom jack anne
The Nums:98 99 10 100 

------解决方案--------------------
Java code
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public   class   ChaiFen   {
      public   static   void   main(String   args[]){
      String   a= "jerry,98 |tom,99 |jack,10 |anne,100 ";
      Pattern   p=Pattern.compile( "([a-zA-Z]+)\\W{1}([0-9]+)\\W{0,1} ");
      Matcher   ma=p.matcher(a);
      int length=a.split(" ").length;
      System.out.println(length);
      System.out.println(ma.matches());
      ma.reset();
      String[] arr=new String[length];
      int i=0;
      while(ma.find()){
          //System.out.println(ma.group(1));//不知道怎么把这个放到数组里面???
          arr[i]=new String();
          arr[i]=ma.group(1);
          i++;
      }
      ma.reset();
      
      String[] num=new String[length];
      int j=0;
      while(ma.find()){
          //System.out.println(ma.group(2));  
          num[j]=new String();
          num[j]=ma.group(2);
          j++;
}
      System.out.print("The Strings:");
      for(String str:arr){
          System.out.print(str+"  ");
      }
      System.out.println();
      System.out.print("The Nums:");
      for(String str:num){
          System.out.print(str+"  ");
      }
}
}