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

これは、2 つの diff テキスト ファイルからユーザー指定の文字列を検索するために作成したプログラムです。正常に動作しますが、最初の行のみを検索します。例: ファイルに 1000 1001 1002 がある場合、1000 のみを検索します。次の行に移動して.equals()メソッドを使い続けるにはどうすればよいですか?

4

2 に答える 2

3

BufferedReader ではなく Scanner を使用する必要があります。これは最近のクラスであり、このタスクでより良い仕事をしていると思います。特に、コードの他の場所ですでに Scanner を使用しているため、それをインポートしたためです。以下は、次に読み取る行がある間にファイル内のすべての行を読み取るスキャナーです。

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();
    }
}

テキスト ファイルの例 data1.txt:

Test1
Test2
Test3
Test4

サンプル テスト ファイル data2.txt

Check1
Check2
Check3
Check4

これらのデータファイルを使用してコードを実行した場合の stdout の例:

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)

また、読み込んだすべての情報をループに入れてはいけません。do を一番上に置くことで、BufferedReaders の新しいセットを効果的に作成し続け、それらに同じ名前を付け、同じことを行うように指示し、最初のヒット後にブレークするように指示します。実際にブレークを取り除いた場合、この他のすべてのものはループであってはならないループにあるため、さらに多くの問題が発生します。

于 2013-01-17T16:47:05.063 に答える
1

ご利用いただいてから

 break;

while ループでは、最初の行をチェックした後にループを終了します。ブレークを削除してみてください。すべての行を読みたい場合。

于 2013-01-17T16:45:59.057 に答える