编程语言
首页 > 编程语言> > 判断java中最多的词组

判断java中最多的词组

作者:互联网

其中的难点,是空格,以及如果第一个是空格怎么办,虽然事后看着很简单,但是做的时候却十分的困难!

static void Daunyu()throws IOException
{
    Word word=new Word();                                      //单词的链头
    Word lian,xin;                                            
    String str="";
    FileReader f=new FileReader("E:\\578095023\\FileRecv\\软件工程\\课堂测试\\读入txt\\飘英文版.txt");                //读取英文文件
    char[] c=new char[1];                                 //每次读取一个字母
    int b=0;
    boolean exist=false;                              //判断单词是否存在于  word 链中
    Word word1=new Word();
    Word lian1,xin1;
    String str1="";
    FileReader f1=new FileReader("E:\\578095023\\FileRecv\\软件工程\\课堂测试\\读入txt\\stopwords.txt");  
    char[] c1=new char[1];
    int b1=0;
    boolean exist1=false;
    //stopwords文本
  while((b1=f1.read(c1))!=-1)                              //每次读取一个字母直到最后
     {
         //如果字符为  换行、空格、单引号、双引号、逗号、句号  则为一个单词的结束及另一个单词的开始
         if(String.valueOf(c1).equals("\r")||String.valueOf(c1).equals("\n")||String.valueOf(c1).equals(" ")||String.valueOf(c1).equals(",")||String.valueOf(c1).equals(".")||String.valueOf(c1).equals("\"")||String.valueOf(c1).equals("'"))
         {
          
             lian1=word1;
             while(lian1!=null)           
             {

              if(lian1.value.equalsIgnoreCase(str1))           //如果单词在单词链中存在,则单词个数++
                 {
                     lian1.geshu++;exist1=true;break;
                 }
                 else
                 {
                     lian1=lian1.next;
                 }
                
             }              if(exist1==false)                        //如果不存在,则在单词链中添加
             {
                 xin1=new Word(str1,1);
                 xin1.next=word1.next;
                 word1.next=xin1;
                 str1="";
             }
             else
             {
                 exist1=false;
                 str1="";
             }
         }
         else                                      //单词
         {
             str1+=String.valueOf(c1);
         }
     }                            //判断单词是否存在于  word 链中
     while((b=f.read(c))!=-1)                              //每次读取一个字母直到最后
     {
      
      
      
         //如果字符为  换行、空格、单引号、双引号、逗号、句号  则为一个单词的结束及另一个单词的开始
         if(String.valueOf(c).equals("‘")||String.valueOf(c).equals("’")||String.valueOf(c).equals("”")||String.valueOf(c).equals("“")||String.valueOf(c).equals("\r")||String.valueOf(c).equals("\n")||String.valueOf(c).equals(",")||String.valueOf(c).equals(".")||String.valueOf(c).equals("\"")||String.valueOf(c).equals("'"))
         {
          
             lian=word;
             while(lian!=null)           
             {               if(lian.value.equalsIgnoreCase(str))           //如果单词在单词链中存在,则单词个数++
                 {
                     lian.geshu++;exist=true;break;
                 }
                else
                   {
                    int l1=0,l2=0;
                    while(true)
                 {
                  xin1=new Word("",0);
                        lian1=word1.next;
                        //找到单词链中个数最多的
                        while(lian1!=null)
                        {
                           if(str.equals(lian1.value))
                           {
                            l1=1;
                           }
                            lian1=lian1.next;
                           
                        }      
                  if(lian1==null)
                  {
                   break;
                  }
                 }                      if(str.startsWith(" "))
                     {
                      str=str.substring(1);
 
                     }
                    if(l1==1||!str.contains(" "))
                    {
                     break;
                    }
                       lian=lian.next;
                   }
                
             }              if(exist==false)                        //如果不存在,则在单词链中添加
             {
                 xin=new Word(str,1);
                 xin.next=word.next;
                 word.next=xin;
                 str="";
             }
             else
             {
                 exist=false;
                 str="";
             }
         }
         else                                      //单词
         {
             str+=String.valueOf(c);
         }
     }
    
    
     //   循环10次
     System.out.println("请输入您想查询的前几个出现此处最多的单词");
  Scanner scan=new Scanner(System.in);
  int N=scan.nextInt();
     for(int i=1;i<=N;i++)                  
     {
         xin=new Word("",0);
         lian=word.next;
         //找到单词链中个数最多的
         while(lian!=null)
         {
             if(lian.geshu>xin.geshu)
             {
                 xin=lian;
             }
             lian=lian.next;
         }
         //输出单词链中个数最多的
         System.out.println("第"+i+"个 :"+xin.value+"个数:"+xin.geshu);
         lian=word;
         //删除单词链中单词个数最多的
         while(lian.next!=null)
         {
             if(lian.next.value.equalsIgnoreCase(xin.value))
             {
                 lian.next=lian.next.next;
                 break;
             }
             lian=lian.next;
         }
     }
 }

 

标签:词组,判断,java,String,valueOf,equals,next,单词,lian
来源: https://www.cnblogs.com/smartisn/p/10851483.html