免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4916 | 回复: 19
打印 上一主题 下一主题

字符串转换的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-09-01 11:26 |只看该作者 |倒序浏览
str[n][2]是String二维数组
string[0]是要记录单词 string[1]记录出现次数
现在要增加str[1]记录的次数 我下面的方法有问题,有没有解决的办法?
str[1]=((Integer.parseInt(str[1])) +1).toString()

论坛徽章:
0
2 [报告]
发表于 2004-09-01 12:44 |只看该作者

字符串转换的问题

你遇到了什么问题?
另外你的数据结构好像挺奇怪的。为什么要用数组,而不是自己定义一个类存放这些信息呢?

论坛徽章:
0
3 [报告]
发表于 2004-09-01 17:39 |只看该作者

字符串转换的问题

sakulagi thanks!!  自己定义了一个类 刚学java,不知道有没有搞错。
//Demo1.java
import java.io.*;
import java.util.*;
class StringInt
{
        public String[] sg;
        public int[] it;
        StringInt()
        {
        }
    StringInt(int n)
        {
     sg=new String[n];
     it=new int[n];
    }
};

class Demo3{
public static void main(String args[]){

  try{  
                    
                   File  file=new  File("b.txt");  
           BufferedReader  in=new  BufferedReader(new  FileReader(file));
                   BufferedWriter  out=new BufferedWriter (new FileWriter ("out.txt"));
           String  s=""; //定义的字符串为空,准备存放读取的文本
           StringBuffer  str=new  StringBuffer();  
         

           while((s=in.readLine())!=null){               
                             str.append(s);//读取的文本放到字符串str中去
            }  

      

                   String str1=str.toString();//StringTokenizer中只能够用string不能用StringBuffer
                   StringTokenizer st=new StringTokenizer(str1," ");
                   int n=st.countTokens();//求文本的长度

                   for (int i=0; i<str1.length(); i++)
                {
                        if ((str1.charAt(i)<48) || ((str1.charAt(i)>;57) && (str1.charAt(i)<65)) || ((str1.charAt(i)>;90) && (str1.charAt(i)<97)) || ((str1.charAt(i)>;122) && (str1.charAt(i)<126)))
                        {
                                str1 = str1.replace(str1.charAt(i), ' ');         
                          }
                }

                   StringInt SI=new StringInt(n);//新建一个StringInt对象SI
         
          SI.sg=str1.split(" ");//将读取的文本放入SI中的String数组中

                 for (int i=0;i<n ;i++ )
                 {
                         SI.it=1;   //将每个单词出现的次数赋初值为1
                 }

                  for(int i=0;i<n-1;i++)
                         for (int j=i+1;j<n;j++ )
                         {
                          if((SI.sg.equals(SI.sg[j]))&&(SI.it[j]!=0))//这里不能用==要用equals
                           {
                                 SI.it++; //相同的单词个数合并,只在一个单词处记录,其它的都赋值为0
                             SI.it[j]--;
                                 }
                     }
   

                   //用冒泡法进行排序
                    for(int i=0;i<n;i++)
                        for(int j=n-1;j>;i;j--)
               {
                         if(SI.it[j]>;SI.it[j-1])
                         { int temp=SI.it[j];
                        SI.it[j]=SI.it[j-1];
                        SI.it[j-1]=temp;

                       String Stemp=SI.sg[j];
                        SI.sg[j]=SI.sg[j-1];
                        SI.sg[j-1]=Stemp;
                          }
                   }


            for (int i=0;i<n ;i++)
                  {
                         if(SI.it!=0){

                     System.out.println(SI.sg+":"+SI.it);
                         
                         }
                 }
                in.close();  
                out.close();
                          
      }catch(Exception  e){  
           System.out.println(e.toString());  
       }  
   
    }
   }

论坛徽章:
0
4 [报告]
发表于 2004-09-01 23:04 |只看该作者

字符串转换的问题

我写了一个简单实现:

  1. /*
  2. * Created on 2004-9-1
  3. *  
  4. */
  5. package test.lang;

  6. import java.io.BufferedReader;
  7. import java.io.FileNotFoundException;
  8. import java.io.FileReader;
  9. import java.io.IOException;
  10. import java.util.Arrays;
  11. import java.util.StringTokenizer;
  12. import java.util.Vector;

  13. public class PhraseCount {
  14.         private Vector vWordCount = new Vector();

  15.         public PhraseCount(String fileName) {
  16.                 String buffer = null;
  17.                 try {
  18.                         BufferedReader br = new BufferedReader(new FileReader(fileName));
  19.                         while ((buffer = br.readLine()) != null) {
  20.                                 buffer = formalizeBuffer(buffer);
  21.                                 StringTokenizer st = new StringTokenizer(buffer);
  22.                                 String token = null;
  23.                                 while (st.hasMoreTokens()) {
  24.                                         token = st.nextToken();
  25.                                         increseCount(token);
  26.                                 }
  27.                         }//end while (...)

  28.                 } catch (FileNotFoundException e) {
  29.                         e.printStackTrace();
  30.                 } catch (IOException e) {
  31.                         e.printStackTrace();
  32.                 }
  33.         }

  34.         private String formalizeBuffer(String buffer) {
  35.                 char tempChar;
  36.                 for (int i = 0; i < buffer.length(); i++) {
  37.                         tempChar = buffer.charAt(i);
  38.                         if (!Character.isLetterOrDigit(tempChar)) {
  39.                                 buffer = buffer.replace(tempChar, ' ');
  40.                         }
  41.                 }
  42.                 return buffer;
  43.         }

  44.         public static void main(String[] args) {
  45.                 String fileName = "c:\\test.txt";
  46.                 PhraseCount pc = new PhraseCount(fileName);
  47.                 pc.printSorted();
  48.         }

  49.         public void printSorted() {
  50.                 Object[] arrWordCount = vWordCount.toArray();
  51.                 Arrays.sort(arrWordCount);
  52.                 for (int i = arrWordCount.length - 1; i >; 0; i--) {
  53.                         WordCount aWordCount = (WordCount) arrWordCount[i];
  54.                         System.out.println(aWordCount.toString() + "\t\t\t"
  55.                                         + aWordCount.getCount());
  56.                 }
  57.         }

  58.         private void increseCount(String token) {
  59.                 WordCount wc = new WordCount(token);
  60.                 if (!vWordCount.contains(wc)) {
  61.                         vWordCount.add(wc);
  62.                 } else {
  63.                         ((WordCount) vWordCount.get(vWordCount.indexOf(wc))).increase();
  64.                 }
  65.         }
  66. }

  67. //Class WordCount

  68. class WordCount implements Comparable {
  69.         public boolean equals(Object obj) {
  70.                 return toString().equals(obj.toString());
  71.         }

  72.         public String toString() {
  73.                 return s;
  74.         }

  75.         private String s;

  76.         private Integer count;

  77.         public WordCount(String s) {
  78.                 this.s = s;
  79.                 count = new Integer(1);
  80.         }

  81.         public void increase() {
  82.                 count = new Integer(count.intValue() + 1);
  83.         }

  84.         public int compareTo(Object o) {
  85.                 if (o instanceof WordCount) {
  86.                         return count.compareTo(((WordCount) o).getCount());
  87.                 } else
  88.                         throw new ClassCastException(
  89.                                         "The object is not an instance of class \"WordCount\"");
  90.         }

  91.         public Integer getCount() {
  92.                 return count;
  93.         }

  94. }
复制代码

论坛徽章:
0
5 [报告]
发表于 2004-09-02 02:14 |只看该作者

字符串转换的问题

真麻烦,用map不挺简单吗?

  1. public class CountedSet {

  2.     private Map sortMap;

  3.     /** Creates a new instance of CountedSet */
  4.     public CountedSet() {
  5.         sortMap = new TreeMap();
  6.     }

  7.     public void add(Object o)
  8.     {
  9.         int count;
  10.         if(sortMap.containsKey(o))
  11.         {
  12.             count = ((Integer)(sortMap.get(o))).intValue();
  13.             sortMap.put(o, new Integer(count+1));
  14.         }
  15.         else
  16.         {
  17.             sortMap.put(o, new Integer(1));
  18.         }
  19.     }
  20.     public int get(Object o)
  21.     {
  22.         Integer itg;
  23.         itg = (Integer)sortMap.get(o);
  24.         if(itg!=null)
  25.         {
  26.             return itg.intValue();
  27.         }
  28.         else
  29.         {
  30.             return 0;
  31.         }
  32.     }

  33.     public Map getMap()
  34.     {
  35.         return sortMap;
  36.     }
  37. }
复制代码


使用的时候

CountedSet cs = new CountedSet();

cs.add("a");
cs.add("b");
cs.add("a");

int a = cs.get("a"); // = 2
int b = cs.get("b"); // = 1
int c = cs.get("c"); // = 0

[ 本帖最后由 perryhg 于 2007-1-14 09:37 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2004-09-02 10:39 |只看该作者

字符串转换的问题

SortedMap是按key排序,如果需要按Value排序呢?楼主是要按出现频率来排序吧?

论坛徽章:
0
7 [报告]
发表于 2004-09-02 10:57 |只看该作者

字符串转换的问题

要按出现频率来排序!还要问一个:读取中文文本的问题如何解决?

论坛徽章:
0
8 [报告]
发表于 2004-09-02 12:45 |只看该作者

字符串转换的问题

对中文不是很熟悉。我的那个程序已经是按出现频率排序了。
不过结构上还是不够清晰。有时间的话。我会改写一下。
使用SortedMap是一个好方法。楼主可以再好好组织一下结构,以便程序能更容易被复用。

论坛徽章:
0
9 [报告]
发表于 2004-09-02 14:03 |只看该作者

字符串转换的问题

map还没用过,我再看看!非常谢谢两位了!要解决中文和英文混合在一起的问题,现在中文还没有解决。我这里有个中文的解决方法,不过我对一些东西不是很懂?
  1. import java.io.*;
  2. import java.lang.String;
  3. import java.util.StringTokenizer;
  4. import java.util.Vector;

  5. public class demoSumCn
  6. {
  7.         public static String preSplit(String src)throws IOException
  8.         {
  9.                 int i=0;               
  10.                 for (i=0; i<src.length(); i++)
  11.                 {
  12.                        
  13.                         if (src.charAt(i) == 12290 || ((src.charAt(i)>;8208) && (src.charAt(i)<8223)) || ((src.charAt(i)>;65280) && (src.charAt(i)<65311)))
  14.                         {
  15.                                 src = src.replace(src.charAt(i), '?');
  16.                         }
  17.                 }

  18.              return src;
  19.         }
  20.        
  21.         public static void main(String args[])throws IOException
  22.         {
  23.                 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  24.                 String str = reader.readLine();
  25.               String newstr = preSplit(str);
  26.               char rs[][] = new char[2][100];//
  27.               int i=1, j, k;
  28.               boolean find = false;
  29.               
  30.               char t;
  31.                  
  32.                  for (k=0; k<100; k++)
  33.                  {
  34.                          rs[0][k] = '?';
  35.                          rs[1][k] = '0';
  36.                  }
  37.               rs[0][0] = newstr.charAt(0);
  38.               rs[1][0] = '1';
  39.               
  40.                             
  41.               for (k=1; k<newstr.length(); k++)
  42.               {
  43.                       t = newstr.charAt(k);
  44.                       for(j=0; j<i; j++)
  45.                       {
  46.                               if (rs[0][j] == t)
  47.                               {
  48.                                       rs[1][j] = (char)(rs[1][j]+1);
  49.                                       find = true;
  50.                                       break;
  51.                               }                             
  52.                       }
  53.                       if (!find)
  54.                       {
  55.                               rs[0][j] = t;
  56.                               rs[1][j] = '1';
  57.                               i=i+1;
  58.                       }
  59.                       find = false;
  60.               }
  61.               
  62.               for (j=0; j<100; j++)
  63.                       if (rs[0][j] != '?')System.out.println(rs[0][j]+"("+rs[1][j]+")");
  64.         }
复制代码

论坛徽章:
0
10 [报告]
发表于 2004-09-02 14:50 |只看该作者

字符串转换的问题

你指的中文问题具体是什么问题。

  1. if (src.charAt(i) == 12290 || ((src.charAt(i)>;8208) && (src.charAt(i)<8223)) || ((src.charAt(i)>;65280) && (src.charAt(i)<65311)))
复制代码

这样的代码的可读性差了一点
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP