搜索程序在txt文件中的java代码

时间:2016-08-14 11:00:31

标签: java

我想用以下任务开发java程序。

使用输入文件名并输入产品代码。结果显示所有产品详细信息

例如。

文件数据是MLK#milk #lires日期2016年5月15日

输出

此产品名称为含有MLK代码的牛奶,将于2016年5月15日到期。

帮助我谢谢...

我的代码是......

import java.io.*;
import java.util.*;


public class search 
{

    public static void main( String[] args ) throws IOException 
    {


        String word = ""; int val = 0;
        while(!word.matches("quit"))
        {
            System.out.println("Enter the word to be searched for");
            Scanner input = new Scanner(System.in);
            word = input.next();
            Scanner file = new Scanner(new File("stationMaster.txt"));

            while(file.hasNextLine())           
            {
                String line = file.nextLine();
                if(line.indexOf(word) != -1)
                {
                    while(file.hasNextLine())
                    {       

                        String data=file.nextLine();
                        System.out.println(data);
                    }


                    //System.out.println("");
                    val = 1;
                    break;
                }
                else
                {
                    val = 0;
                    continue;
                }
            }
            if(val == 0)
            {
            System.out.println("Station does not exist");
            break;
            }

        }  
    }
}

2 个答案:

答案 0 :(得分:2)

ansible all -m find -a "patterns=\"myFile.txt\" paths=\"/home/user/Documents\"

答案 1 :(得分:1)

import java.io.*;
import java.util.*;


public class search 
{
    public static void main( String[] args ) throws IOException 
    {
        String word = ""; int val = 0;
        while(!word.matches("quit"))
        {
            System.out.println("Enter the word to be searched for");
            Scanner input = new Scanner(System.in);
            word = input.next();
            Scanner file = new Scanner(new File("stationMaster.txt"));

            while(file.hasNextLine())           
            {
                String line = file.nextLine();

                //split the string on # character so that you get code, product name and expiration date separately.

                String arr[] = line.split("#");

                //check whether the string contains the required string or not

                try{
                    if(arr[0].equalsIgnoreCase(word) || arr[1].equalsIgnoreCase(word)){                                   
                        //line break
                        System.out.println();

                        //split the format 'expiration date 15 may 2016' so that we can use date separately without the heading of 'expiration date'
                        String dateStrings[] = arr[2].split(" ");

                        System.out.print("this product name is " + arr[1] + " with " + arr[0] + " code and will expire on ");
                        System.out.println(dateStrings[2] + " " + dateStrings[3] + " " + dateStrings[4]);
                        val = 1;
                        break;
                    }
                    else
                    {
                        val = 0;
                        continue;
                    }
                }
                catch(IndexOutOfBoundsException indexEx){
                    val = 0;
                    continue;
                }
            }

            if(val == 0){
                System.out.println("Station does not exist");
                break;
            }
        }  
    }
}

上面的代码将搜索将从文件中读取的字符串,如果它包含要搜索的单词。它可以是产品代码或产品名称