public class Main {
public static void main(String[] args)
{
int ch = 0;
do
{
Scanner in = new Scanner(System.in);
String s;
System.out.println("Enter the part number");
s=in.nextLine();
try{
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Ankit\\Documents\\NetBeansProjects\\tcs_1\\number.txt"));
BufferedReader Br = new BufferedReader(new FileReader("C:\\Users\\Ankit\\Documents\\NetBeansProjects\\tcs_1\\number1.txt"));
String strLine;
int flag=0;
while ((strLine = br.readLine()) != null)
{
if(strLine.equals(s))
{
flag=1;
System.out.println ("Part Number exists in 1");
break;
}
else
{
flag=0;
System.out.println ("Part Number doesnot exist in 1");
break;
}
}
if(flag==0)
{
while ((strLine = Br.readLine()) != null)
{
if(strLine.equals(s))
{
System.out.println ("Part Number exists in 2");
break;
}
else
{
System.out.println("File does not exist in 2");
break;
}
}
}
System.out.println ("Do you want to continue-Press1 for yes and 2 for no");
ch= in.nextInt();
br.close();
Br.close();
}
catch (Exception e)
{
System.err.println("Error: " + e.getMessage());
}
}
while(ch==1);
}
}
这是我从 2 个差异文本文件中搜索用户给定字符串的程序。它工作正常,但只搜索第一行。例如:如果一个文件有 1000 1001 1002 它只会搜索 1000。我如何转到下一行并继续使用该.equals()
方法?