想要匹配文本文件中的模式

时间:2016-01-16 07:12:25

标签: java regex

这是我的代码

import java.io.*;

public class Random
{  
public static void main(String args[]) throws IOException
{
FourWords ob1=new FourWords();
FiveWords ob2= new FiveWords();
SixWords ob3= new SixWords();



BufferedReader br= new BufferedReader(new InputStreamReader(System.in));


int no;


do
{
    System.out.println("\n1:- Four words Security");
    System.out.println("2:- Five words Security");
    System.out.println("3:- Six words Security");
    System.out.println("4:- Enter 4 to exit");


     no =Integer.parseInt(br.readLine());

    if(no>4)
    {
        System.out.println("Please provide an valid input");
    }


switch(no)
{
    case 1:
    ob1.four();
    break;

    case 2:
    ob2.five();
    break;

    case 3:
    ob3.six();
    break;



}
}
while(no<=3); 


}

}

class FourWords
{
    public void four() throws IOException
    {

        int arr[]= new int[5];
        int randomNum,minimum=1,maximum=4;  
        int i;

        System.out.print("\n");
        for(i=0;i<5;i++)
        {
arr[i]=minimum +(int)(Math.random()*maximum);

System.out.print(arr[i]);
System.out.print("\t");

    }

    }

}

class FiveWords
{
    void five()
    {
int randomNum,minimum=1,maximum=5;
System.out.print("\n");
for(int j=1;j<=5;j++)
{   
randomNum=minimum +(int)(Math.random()*maximum);

System.out.print(randomNum);    
System.out.print("\t");
    }
    }   
}

class SixWords
{
    void six()
    {
        int randomNum,minimum=1,maximum=6;  
System.out.print("\n");
for(int k=1;k<=5;k++)
{
        randomNum=minimum +(int)(Math.random()*maximum);

System.out.print(randomNum);
System.out.print("\t");
}       
}

}

class DiceWare 
{

    public void dice() throws IOException
    {

        FileInputStream fin= new FileInputStream("diceware_wordlist.pdf");
        int ch;
        while((ch=fin.read())!=-1)
        System.out.println((char)ch);

        fin.close();
    }


}

我想将randomNum值与文本文件匹配。

假设randomNum值为11163,我想在文本文件中匹配这些字符串

例如: -

11163关于

并希望打印与模式相对应的单词。

与上例

中的关于类似

所以我将如何做到这一点

请帮帮我

2 个答案:

答案 0 :(得分:0)

  1. 从命令行通过args获取输入到main()。
  2. 逐行读取文件,并将第二个值与正则表达式pattern匹配的用户输入进行比较。

答案 1 :(得分:0)

首先将随机数组转换为字符串,然后将其与文件匹配..

例如......

公共课Demo1 {

    public static void main(String[] args) {

        String arr="12345";
            try{
            FileInputStream fin= new FileInputStream("\\demo\\demo.txt");
            {
                final Scanner scanner = new Scanner(fin);
                while (scanner.hasNextLine()) {
                   final String lineFromFile = scanner.nextLine();
                   if(lineFromFile.contains(arr)) { 
                       // a match!
                       System.out.println("I found " +arr+ " in file ");
                       break;
                   }
                   else
                   {
                       System.out.println("No data found");
                   }
                }   
            }
            fin.close();
            }
            catch(IOException ex)
            {
                System.out.print("Error");
            }
        }

}