A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 樊占江 中级黑马   /  2012-8-14 00:21  /  1224 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.io.*;
public class wordtext
{
public static void main(String[] args)
{
String s=" ";
String w=" ";
    System.out.print("input:\n");
try{
BufferedReader in=new BufferedReader(
new InputStreamReader(System.in));
s=in.readLine();}
catch(IOException e){}
System.out.print("input text word!");
try{
BufferedReader in=new BufferedReader(
new InputStreamReader(System.in));
w=in.readLine();}
catch(IOException e){}
int first=0,second=0,end=0;
for(int i=1;i<=s.length();i+=second+1){
second=s.indexOf(" ");
String t=s.substring(first,second);
s=s.substring(second+1);
if(w.equalsIgnoreCase(t))end++;
}
if(end==0)System.out.println("there no "+w);
else System.out.println("there"+end+w);
}
}

输入文章,再输入要查询的单词,统计查询单词出现几次!!

评分

参与人数 1技术分 +1 收起 理由
张_涛 + 1 赞一个!

查看全部评分

6 个回复

倒序浏览
  1. package com.heima.test;

  2. import java.io.*;

  3. public class wordtext {
  4.         public static void main(String[] args) {
  5.                 String s = " ";
  6.                 String w = " ";
  7.                 System.out.print("input:\n");
  8.                 try {
  9.                         BufferedReader in = new BufferedReader(new InputStreamReader(
  10.                                         System.in));
  11.                         s = in.readLine();
  12.                         System.out.println(s);
  13.                 } catch (IOException e) {
  14.                 }
  15.                 System.out.print("input text word!");
  16.                 try {
  17.                         BufferedReader in = new BufferedReader(new InputStreamReader(
  18.                                         System.in));
  19.                         w = in.readLine();
  20.                         System.out.println(w);
  21.                 } catch (IOException e) {
  22.                 }
  23.                

  24.                 int wLen = w.length();
  25.                 int sLen = s.length();       
  26.                 int beginIndex = 0;
  27.                 int endIndex = wLen;
  28.                 int count = 0;
  29.                 //sLen - wLen就是走过的距离
  30.                 for(int i = 0; i < sLen - wLen; i++){
  31.                         String word = s.substring(beginIndex, endIndex);
  32.                         if(word.equalsIgnoreCase(w)){
  33.                                 count++;
  34.                         }
  35.                         beginIndex++;
  36.                         endIndex++;
  37.                 }       
  38.                 if (count == 0)
  39.                         System.out.println("there no " + w);
  40.                 else
  41.                         System.out.println("there" + count + w);
  42.                
  43.                
  44. //                int first = 0, second = 0, end = 0;
  45. //                for (int i = 1; i <= s.length(); i += second + 1) {
  46. //                        second = s.indexOf(" ");
  47. //                        String t = s.substring(first, second);
  48. //                        s = s.substring(second + 1);
  49. //                        if (w.equalsIgnoreCase(t))
  50. //                                end++;
  51. //                }
  52. //                if (end == 0)
  53. //                        System.out.println("there no " + w);
  54. //                else
  55. //                        System.out.println("there" + end + w);
  56.         }
  57. }

复制代码
我把你的代码中的fo修改了,你的问题是出现在indexOf这里你想表达什么不清楚,这个值取到的是一个负数,你在数组中使用这个的话,当然是数组越界了
而且你的思路感觉不清醒或者有些麻烦

比如:s = aaabcddddbceeeebc
查询w = bc出现的次数

用s的长度- w的长度,就得到你需要比较的次数了
然后用substring(beginIndex,statrIndex)依次比较就好了
比完依次++就OK了

评分

参与人数 1技术分 +1 收起 理由
张_涛 + 1 赞一个!

查看全部评分

回复 使用道具 举报
本帖最后由 周世阳 于 2012-8-14 02:20 编辑

thanks to 黎健东同学的提醒修改一下,
indexOf(“ ”)在不存在“ ”自负串的时候会返回-1,String t=s.substring(0,-1);就会产生数组下表越界
可以将
second=s.indexOf(" ");
String t=s.substring(first,second);
加一个if判定来处理:修改为--->
second=s.indexOf(" ");
if(second!=-1)
{String t=s.substring(first,second); }
else{t=s;}




建议用spit方法直接将语句分解成单词的数组,这样简洁点也不会产生越界问题

import java.io.*;
public class wordtext
{
        public static void main(String[] args)
        {         
                int times=0;
                String s=" ";
                String w=" ";
                System.out.print("input:\n");
                try{
                        BufferedReader in=new BufferedReader(
                        new InputStreamReader(System.in));
                        s=in.readLine();
                } catch(IOException e){}
                System.out.print("input text word!");
                try{
                        BufferedReader in=new BufferedReader(
                        new InputStreamReader(System.in));
                        w=in.readLine();
                }
                catch(IOException e){}
               
                String [] words=s.split(" ");
                for(String word:words){
                        if(word.equalsIgnoreCase(w))times++;
                }//与你得代码不同之处


                if(times==0)System.out.println("there no "+w);
                else System.out.println("there"+times+w);
        }
}

评分

参与人数 1技术分 +1 收起 理由
张_涛 + 1 赞一个!

查看全部评分

回复 使用道具 举报
周世阳 发表于 2012-8-14 01:00
代码运行无错,不过可以改进一下用spit方法直接将语句分解成单词的数组,这样简洁点

import java.io.*;

你多试几次,数组越界了...............
回复 使用道具 举报
import java.io.*;
public class wordtext
{
public static void main(String[] args)
{
String s=" ";
String w=" ";
    System.out.print("input:\n");
try{
BufferedReader in=new BufferedReader(
new InputStreamReader(System.in));
s=in.readLine();}
catch(IOException e){}
System.out.print("input text word!");
try{
BufferedReader in=new BufferedReader(
new InputStreamReader(System.in));
w=in.readLine();}
catch(IOException e){}
int first=0,second=0,end=0;
for(int i=1;i<=s.length();i+=second+1){
//这个s值会随着你这个字串长度的长度不断减小,而你这个i会一直增大,导致你到后面字串的长度小于i值时,i<=s.length()条件已经不满足,虽然你的字串中还有你要查找的字符串,那for循环中的语句也不执行了,导致你的统计结果有时会比实际的偏小
second=s.indexOf(" ");
String t=s.substring(first,second);
s=s.substring(second+1);
if(w.equalsIgnoreCase(t))end++;
}
if(end==0)System.out.println("there no "+w);
else System.out.println("there"+end+w);
}
}
你可以这样:
import java.io.*;
public class wordtext
{
public static void main(String[] args)
{
String s=" ";
String w=" ";
    System.out.print("input:\n");
try{
BufferedReader in=new BufferedReader(
new InputStreamReader(System.in));
s=in.readLine();}
catch(IOException e){}
System.out.print("input text word!");
try{
BufferedReader in=new BufferedReader(
new InputStreamReader(System.in));
w=in.readLine();}
catch(IOException e){}
int first=0,second=0,end=0;
int strLen = s.length();//先把初始字符串的长度保存下来,这样就好了
for(int i=1;i<=strLen;i+=second+1){
second=s.indexOf(" ");
String t=s.substring(first,second);
s=s.substring(second+1);
if(w.equalsIgnoreCase(t))end++;
}
if(end==0)System.out.println("there no "+w);
else System.out.println("there"+end+w);
}
}

评分

参与人数 1技术分 +1 收起 理由
张_涛 + 1 赞一个!

查看全部评分

回复 使用道具 举报
import java.io.*;
public class wordtext
{
public static void main(String[] args)
{
String s=" ";
String w=" ";
    System.out.print("input:\n");
try{
BufferedReader in=new BufferedReader(
new InputStreamReader(System.in));
s=in.readLine();}
catch(IOException e){}
System.out.print("input text word!");
try{
BufferedReader in=new BufferedReader(
new InputStreamReader(System.in));
w=in.readLine();}
catch(IOException e){}
int first=0,second=0,end=0;
int strLen = s.length();//先把初始字符串的长度保存下来,这样就好了
for(int i=1;i<=strLen;i+=second+1){
second=s.indexOf(" ");

if(second==-1)
{
    break;  //加上这段代码,会完善一下,不然当你字符串s的最后面不加空格的话,会报错
}

String t=s.substring(first,second);
s=s.substring(second+1);
if(w.equalsIgnoreCase(t))end++;
}
if(end==0)System.out.println("there no "+w);
else System.out.println("there"+end+w);
}
}
回复 使用道具 举报
黎健东 发表于 2012-8-14 01:02
你多试几次,数组越界了...............

看到了。。。
indexOf(" ")会产生-1,thanks


回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马