免费注册 查看新帖 |

Chinaunix

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

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

论坛徽章:
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 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
3 [报告]
发表于 2004-09-02 10:57 |显示全部楼层

字符串转换的问题

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

论坛徽章:
0
4 [报告]
发表于 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
5 [报告]
发表于 2004-09-02 15:24 |显示全部楼层

字符串转换的问题

中文问题就是指读中文文本 ,那段代码是别人给的,我也是那一段看不太懂。不知道哪位可以看懂。好像是把中文转换成那样的。不过好像放中文用的是char[][]数组,放英文用的是String[][]数组。但是中英文混合在一起都没法解决,现在要找一个方法搞定这两种情况。英文的符号问题好解决,中文的哪些,。、?我不知道如何解决。

论坛徽章:
0
6 [报告]
发表于 2004-09-02 16:29 |显示全部楼层

字符串转换的问题

我最终的目的是要处理中英文混合的文本。可现在中文还没有搞定:
字符编码我也不太熟,有个哥们发过来了这样一段:

中文字符,你可以用下面的语句获得:
System.out.println(Integer.toHexString((int)('?')));//待输出字符为中文字符

论坛徽章:
0
7 [报告]
发表于 2004-09-03 09:25 |显示全部楼层

字符串转换的问题

是呀,惊叹!!perryhg用到的一些函数我只有一个一个的去查才知道。我要降序排列,可我不知道如何修改Collections.sort(countList, WORD_COUNT_ORDER); 的第二个参数。

论坛徽章:
0
8 [报告]
发表于 2004-09-03 17:44 |显示全部楼层

字符串转换的问题

咋没有人来应一声了呀!

论坛徽章:
0
9 [报告]
发表于 2004-09-06 08:35 |显示全部楼层

字符串转换的问题

向perryhg 和sakulagi再次表示感谢!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP