通过Java实现统计输入字符串中以“,”分割的各种单词的出现次数。
  如:“oracle, oracle,sql,mysql,db2,oracle,mysql,db2,oracle”
          则程序输出的结果为如:oralce  x 个   第 x 个位置
                                sql     x 个   第 x 个位置

解决方案 »

  1.   


    import java.util.HashMap;
    import java.util.Map;
    import java.util.Scanner;
    public class Count {
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("请输入待统计的字符串:");
    String str = input.next();
    String[] temp = str.split(",");
    Map<String, Integer> result = new HashMap<String, Integer>();
    int i;

    //计算每个字符串有多少个
    for(String s : temp) {
    if(result.get(s) == null) {
    result.put(s, 1);
    } else {
    i = result.get(s);
    result.remove(s);
    result.put(s, i + 1);
    }
    }

    //打印出每个字符有多少个,及其位置
    i = 0;
    for(String s : temp) {
    i++;
    System.out.println(s + " " + result.get(s) + "个  第" + i + "个位置");
    }
    }
    }
      

  2.   

    split分割后遍历,Map<oracle,List<位置>>然后输出 list.size是个数,内容是位置。
      

  3.   

    结合2楼代码,3楼想法(本来我看的时候3楼还没留言,等我写好了看到了他的留言,晕)
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Scanner;
    import java.util.Map.Entry;public class Count {
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("请输入待统计的字符串:");
    String str = input.next();
    String[] temp = str.split(",");
    Map<String, List<Integer>> result = new HashMap<String, List<Integer>>();
    List<Integer> tempList;
    for (int i = 0; i < temp.length; i++) {
    String s = temp[i];
    if (result.get(s) == null) {
    tempList = new ArrayList<Integer>();
    tempList.add(i+1);
    result.put(s, tempList);
    } else {
    result.get(s).add(i+1);
    }
    tempList = null;
    }
    for (Entry<String, List<Integer>> entry : result.entrySet()) {
    System.out.println(entry.getKey() + ";" + entry.getValue());
    }
    }
    }
      

  4.   


    String str="oracle, oracle,sql,mysql,db2,oracle,mysql,db2,oracle";
    Matcher m1=Pattern.compile("oracle").matcher(str);
    Matcher m2=Pattern.compile("sql").matcher(str);
    int i,j;
    while(m1.find()) {
       System.out.println("oracle第"+m1.start()+"个位置");
        i++;
    }
        System.out.println(i+"个oracle");
    while(m2.find()) {
        System.out.println("sql第"+m2.start()+"个位置");
       j++;
    }
       System.out.println(j+"个sql");
      

  5.   

    要动态的创建n个Matcher 了。嘿嘿。
      

  6.   

    import java.util.HashMap;
    import java.util.Map;
    import java.util.Scanner;public class csdn { public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    System.out.println("请输入一串字符:");
    String st =s.next();
    String []a = st.split(",");
    Map<String,Integer> m = new HashMap<String,Integer>();
    for(int i=0;i<a.length;i++){
    if(!m.containsKey(a[i])){
    m.put(a[i], 1);
    }else{
    int n = m.get(a[i]);
    m.put(a[i], n+1);
    }
    }

    System.out.println(m);
    }}
      

  7.   


    import java.util.HashMap;
    import java.util.Map;
    import java.util.Scanner;
    public class csdn {
    public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    System.out.println("请输入一串字符:");
    String st =s.next();
    String []a = st.split(",");
    Map<String,Integer> m = new HashMap<String,Integer>();
    for(int i=0;i<a.length;i++){
    if(!m.containsKey(a[i])){
    m.put(a[i], 1);
    }else{
    int n = m.get(a[i]);
    m.put(a[i], n+1);
    }
    }
    System.out.println(m);
    }}
      

  8.   


    package com.test.t0916;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Scanner;
    import java.util.Map.Entry;public class Test3 {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            System.out.println("输入字符串:");
            String str = in.next();
            String[] str1 = str.split(",");
            Map<String, List<Integer>> list = new HashMap<String, List<Integer>>();
            List<Integer> tempList;
            for (int i = 0; i < str1.length; i++) {
                String s = str1[i];
                if (list.get(s) == null) {
                    tempList = new ArrayList<Integer>();
                    tempList.add(i+1);
                    list.put(s, tempList);
                } else {
                 list.get(s).add(i+1);
                }
                tempList = null;
            }
            for (Entry<String, List<Integer>> entry : list.entrySet()) {
                System.out.println(entry.getKey() + " "+entry.getValue().size() + "个;第" + entry.getValue()+"的位置");
            }
        }
    }