import java.io.*;
public class Search {
public static void main(String args[]) {
int first = 0, second =0, count = 0;
String text = "So that account you never had at a bank you never";
String s = "";
String subText = text;
System.out.println(text);
System.out.println("请输入单词");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
s = in.readLine();
} catch(IOException e){}
for(int i=1; i<=text.length();i+=second+1) {
second = subText.indexOf(" ");
String temp = subText.substring(first, second);
if(s.equalsIgnoreCase(temp)) count++;
subText = subText.substring(second+1);
}
if(count==0)
System.out.println("单词"+s+"没有出现");
else
System.out.println("单词"+s+"出现了"+count);
}
}
抛出StringIndexOutOfBoundsException
多多指点,小妹谢谢了
public class Search {
public static void main(String args[]) {
int first = 0, second =0, count = 0;
String text = "So that account you never had at a bank you never";
String s = "";
String subText = text;
System.out.println(text);
System.out.println("请输入单词");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
s = in.readLine();
} catch(IOException e){}
for(int i=1; i<=text.length();i+=second+1) {
second = subText.indexOf(" ");
String temp = subText.substring(first, second);
if(s.equalsIgnoreCase(temp)) count++;
subText = subText.substring(second+1);
}
if(count==0)
System.out.println("单词"+s+"没有出现");
else
System.out.println("单词"+s+"出现了"+count);
}
}
抛出StringIndexOutOfBoundsException
多多指点,小妹谢谢了
解决方案 »
- Class类,getDeclaredField(String name) 方法的疑惑。。。
- JAVA中的ArrayList能被继承吗
- sun驱动是不是要输入sqlserver的用户名和密码吗?
- 简单的java程序,多姿多彩的改写,还有没有更多形式的写法,期待你的参与 !
- 乱码问题
- 各位大哥 大姐们怎样找出数组中重复值的个数?????帮个忙啊
- java学习中,谁可以给我些代码看
- 求助:帮忙看看这个编码转换的源代码(源代码都是正确的)
- 兄弟门啊我可算找到提问了啊!! 郁闷我好几天了也没有找到提问!高兴 !不过我不会给分啊
- 我怎样才能把JAVA学好
- 什么是Web Service?
- JAVA读取串口数据编码的问题
可以在前面加个判断,
if(second > 0){
String temp = subText.substring(first, second);
}
import java.io.*;
public class TestBuffer{
public static void main(String args[]) {
int count = 0;
String text = "So that account you never had at a bank you never";
String s = "";
System.out.println(text);
System.out.println("请输入单词");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
s = in.readLine();
} catch(IOException e){}
String[] textarr = text.split(" ");
for(int i=1; i <textarr.length;i++) {
String temp = textarr[i];
if(s.equalsIgnoreCase(temp)) count++;
}
if(count==0)
System.out.println("单词"+s+"没有出现");
else
System.out.println("单词"+s+"出现了"+count);
}
}