我正在编写一个代码,用户可以在其中搜索特定字符串的文本文件(不区分大小写),并打印找到字符串的整行。我的代码段在这里:
public static void search() throws FileNotFoundException
{
Scanner input = new Scanner( System.in );
int fieldNumber = 0;
if ( fieldNumber == 0 )
{
System.out.println("Enter field value to search for: ");
String fieldValue = input.next();
if ("orange".equalsIgnoreCase(fieldValue))
{
// Here is where I am stuck.
{
}
答案 0 :(得分:1)
try
{
Scanner file = new Scanner(new File("file.txt"));
String str = str.nextLine();
System.out.print("Enter search ");
Num = input.nextInt();
if ( Num == 0 )
{
System.out.println("Enter search for: ");
String Value = input.next();
while (file.hasNextLine())
{
final String lineFromFile = file.nextLine();
if(lineFromFile.contains(Value))
{
System.out.println("file.txt");
System.out.println(lineFromFile);
break;
}
答案 1 :(得分:0)
当扫描仪读取一行时,它会为您提供一个字符串。
让我们看一下String API。 http://www.google.com/search?q=java+string+api
方法我看一下:
indexOf(String str)