黑马程序员技术交流社区
标题: 代码题!帮我找一下错误 [打印本页]
作者: 樊占江 时间: 2012-8-14 00:21
标题: 代码题!帮我找一下错误
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);
}
}
输入文章,再输入要查询的单词,统计查询单词出现几次!!
作者: 黎健东 时间: 2012-8-14 01:00
- package com.heima.test;
- 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();
- System.out.println(s);
- } catch (IOException e) {
- }
- System.out.print("input text word!");
- try {
- BufferedReader in = new BufferedReader(new InputStreamReader(
- System.in));
- w = in.readLine();
- System.out.println(w);
- } catch (IOException e) {
- }
-
- int wLen = w.length();
- int sLen = s.length();
- int beginIndex = 0;
- int endIndex = wLen;
- int count = 0;
- //sLen - wLen就是走过的距离
- for(int i = 0; i < sLen - wLen; i++){
- String word = s.substring(beginIndex, endIndex);
- if(word.equalsIgnoreCase(w)){
- count++;
- }
- beginIndex++;
- endIndex++;
- }
- if (count == 0)
- System.out.println("there no " + w);
- else
- System.out.println("there" + count + w);
-
-
- // 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);
- }
- }
复制代码 我把你的代码中的fo修改了,你的问题是出现在indexOf这里你想表达什么不清楚,这个值取到的是一个负数,你在数组中使用这个的话,当然是数组越界了
而且你的思路感觉不清醒或者有些麻烦
比如:s = aaabcddddbceeeebc
查询w = bc出现的次数
用s的长度- w的长度,就得到你需要比较的次数了
然后用substring(beginIndex,statrIndex)依次比较就好了
比完依次++就OK了
作者: 周世阳 时间: 2012-8-14 01:00
本帖最后由 周世阳 于 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);
}
}
作者: 黎健东 时间: 2012-8-14 01:02
周世阳 发表于 2012-8-14 01:00 
代码运行无错,不过可以改进一下用spit方法直接将语句分解成单词的数组,这样简洁点
import java.io.*;
你多试几次,数组越界了...............
作者: 黑马-郑鹏 时间: 2012-8-14 01:34
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);
}
}
作者: 黑马-郑鹏 时间: 2012-8-14 01:51
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 02:15
黎健东 发表于 2012-8-14 01:02 
你多试几次,数组越界了...............
看到了。。。
indexOf(" ")会产生-1,thanks
| 欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) |
黑马程序员IT技术论坛 X3.2 |