Java 要匹配文本文件中的模式吗
这是我的密码Java 要匹配文本文件中的模式吗,java,regex,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 In
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关于
并希望打印出与模式对应的单词
就像上面的例子一样
那么我将如何做到这一点呢
请帮帮我
通过main从命令行获取输入。
逐行读取文件,并将第二个值与正则表达式匹配的用户输入进行比较。
首先将随机数组转换为字符串,然后在文件中匹配它 例如 公开课演示1{
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");
}
}
}请包括样本数据、所需输出以及您迄今为止所做的尝试。没有人想去看一个没有理由存在的文本文件的屏幕截图,我们甚至不知道有多少/什么类型的空白。我建议你读一读好的谢谢你给我建议我怎么做你能给我举个例子吗?我想你不会明白我的问题。这是我的问题。假设我有一个字符串Ex:-strings=12345;我想把这个字符串值和我已经连接到程序的文本文件匹配起来。文本文件包含12345 about,我想打印对应于12345的字符串,在这里。我将如何做。我希望你或有人能帮助我