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?

5
  • Why on earth would you create two variables which differ only in case? It's a readability nightmare. And why are you using an integer for what is effectively a Boolean value? Commented Jan 17, 2013 at 16:43
  • Hey will the read in from the user always be an integer, and will the nextLine() value always be an integer as well? I guess I'm wondering why not use nextInt() and save yourself the trouble of parsing and or using string equality on integer strings? Commented Jan 17, 2013 at 17:08
  • @JonSkeet sorry for the variables name ...but taking boolean or integer to check for a condition ,does that matter? Commented Jan 17, 2013 at 17:10
  • But yeah my main problem with this is the do-while loop that you encapsulate everything in, that's just not a good idea. It's very unlikely you will get the desired effect. Commented Jan 17, 2013 at 17:10
  • @user1896796 I deleted my answer. I just ran your program and its working fine. You only need to adjust your do-while loop Commented Jan 17, 2013 at 17:14

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

@user1896796 Alright I edited by answer, this should do exactly what you wanted. It avoids recreating all of the file and scanner instances by placing the do-while loop where it ought to be. It also does't use break statements. You really should avoid using break in loops, it's a bad practice. If you must do it, do it sparingly.
Now if i have to put an else statement in this.It will be printing not found everytime it changes the line .I cannot use [break] also.
@user1896796 Why would it print out not found every time it changes the line? The print statement for not found is outside of the while loop. It will loop through the entire file first searching for your part number. If it finds it, then it prints found and that's it. If it doesn't find it, it prints not found and then moves on to searching your other file which does the same thing. AND you shouldn't use break in the first place.
@user1896796 You're going to need to clarify what you want the file to do. Do you want it to check file1 and if it finds the value print found in 1 and nothing else? Or do you want it to check file1 if it finds the value print found in 1 and then check to see if it also finds it in file2? Right now i have written it as if it should be exclusively found in file1 or file2, but not both. So if it finds it in file1 it doesn't search file2. And it will search file2 as it is written now, but only if it doesn't find it in file1.
1

Since you have used

 break;

in while loop it will exit from the loop after check first line. Try removing break; if you want to read all lines.

3 Comments

@user1896796 And this is why you are only getting one value out. +1
@sage88 after removing break also only first line is read .I used break because without that all the print statement were getting printed.
@user1896796 The reason only one line is getting read is because you put everything in a do-while loop which creates new instances of all your readers on each loop which starts them off one line one again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.