import java.io.*;
import java.util.*;
public class Java_CH10_HW2
{
public static void main(String[] args)
{
// TODO 自動產生的方法 Stub
String filepath = "C:\\Users\\Dragonyue\\Desktop\\第 十章作業參考文件\\washington.txt";
try
{
Scanner scan = new Scanner(System.in);
InputStreamReader sr = new InputStreamReader(
new FileInputStream(filepath));
BufferedReader reader = new BufferedReader(sr);
String str = "";
int count = 0;
int index = -1;
int linenums = 0;
System.out.print("Input character sequence:");
String ch = scan.nextLine();
while ((str = reader.readLine()) != null)
{
linenums++;
index = str.indexOf(ch);
if (index != -1)
{
System.out
.println(String.format("i=%d %d", linenums, index));
count++;
}
}
System.out.println(String.format("字串%s總共次數%d", ch, count));
reader.close();
sr.close();
}
catch (IOException e)
{
// TODO 自動產生的 catch 區塊
e.printStackTrace();
}
}
}
留言
張貼留言