Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9141599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:39:58+00:00 2026-06-17T09:39:58+00:00

public class Main { public static void main(String[] args) { int ch = 0;

  • 0
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);
  }
}

this is the program that I made to search a user given string from 2 diff text files. Its working fine but only searching the first line.
eg.: If a file has
1000
1001
1002
it wll only search 1000. How do I go to next line and keep on using the .equals() method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:39:59+00:00Added an answer on June 17, 2026 at 9:39 am

    You should use Scanner not BufferedReader as it’s a more recent class
    and I feel does a nicer job with this task. Especially since you have
    already used Scanner elsewhere in your code and thus imported it.
    Below is a scanner that will read all the lines in a file while there
    is a next one to read.

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    public class JavaApplication32 
    {
        public static void main(String[] args) 
        {
            Scanner keyboard = new Scanner(System.in);
            Scanner scanner1 = null;
            Scanner scanner2 = null;
            String partCheck;
            String repeatLoop;
            boolean isInOne;
            boolean isInTwo;
    
            File file1 = new File("data1.txt");
            File file2 = new File("data2.txt");
    
            try
            {
                scanner1 = new Scanner(file1);
                scanner2 = new Scanner(file2);
            }
            catch(FileNotFoundException e)
            {
                e.printStackTrace();
            }
                do
                {
                    isInOne = false;
                    isInTwo = false;
                    System.out.println("Enter the part number");
                    partCheck = keyboard.nextLine();
                    while (scanner1.hasNextLine() && !isInOne)
                    {
                        String line = scanner1.nextLine();
                        if(line.equals(partCheck))
                        {
                            System.out.println("Part Number exists in 1");
                            isInOne = true;
                        }
                    }
                    if(!isInOne)
                    {
                        System.out.println("Part Number does not exist in 1");
                    }
                    while(scanner2.hasNextLine() && !isInOne && !isInTwo)
                    {
                        String line = scanner2.nextLine();
                        if(line.equals(partCheck))
                        {
                            System.out.println("Part Number exists in 2");
                            isInTwo = true;
                        }
                    }
                    if(!isInTwo)
                    {
                        System.out.println("Part Number does not exist in 2");
                    }
                    System.out.println("Do you want to continue? (Y/N)");
                    repeatLoop = keyboard.nextLine();
                } while(repeatLoop.equalsIgnoreCase("y"));
            scanner1.close();
            scanner2.close();
        }
    }
    

    Example Text File data1.txt:

    Test1
    Test2
    Test3
    Test4
    

    Example Test File data2.txt

    Check1
    Check2
    Check3
    Check4
    

    Example stdout when code is run with these datafiles:

    run:
    Enter the part number
    Test1
    Part Number exists in 1
    Part Number does not exist in 2
    Do you want to continue? (Y/N)
    y
    Enter the part number
    Check1
    Part Number does not exist in 1
    Part Number exists in 2
    Do you want to continue? (Y/N)
    n
    BUILD SUCCESSFUL (total time: 19 seconds)
    

    You also shouldn’t put all of your read in information in a loop. By
    putting do at the top you effectively keep creating a new set of
    BufferedReaders and naming them the same thing and telling to do the
    same thing and then telling them to break after the first hit. If you
    did actually get rid of the break you’d have even more problems since
    all of this other stuff is in the loop where it shouldn’t be.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class Foo { public static void main(String[] args) { float f; System.out.println(f); }
public class Sample { public static void main(String[] args) { int year = 2012;
public class I { public static void main(String args[]) { int i=0,j=1,k=2; i=i++ -
public class Main { public static void main(String[] args){ Class2 class2Object = new Class2();
package GC; import java.util.Scanner; public class main { public static void main(String args[]) {
public class Test { public static void main(String[] args) { DemoAbstractClass abstractClass = new
public class Loopname{ public static void main (String [] args){ String files []= new
import java.util.Scanner; public class Main extends Hashmap{ public static void main(String[] args) { Hashmap
import java.util.*; public class SoSanh { public static void main(String[] args) { Scanner input
package hw3; public class Main { public static void main(String[] args) { final int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.