これがあなたが探しているものかどうかはわかりませんが、これは仕事をするはずです。これはすばやく簡単なソリューションです。
は
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
String filePath = "{YOUR_FILEPATH_TO_TEST.TXT}";
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your username: ");
String username = scanner.nextLine();
System.out.println("Checking to see if username exists...");
BufferedReader bufferedReader;
try {
bufferedReader = new BufferedReader(new FileReader(filePath));
String line;
boolean usernameExists = false;
while((line = bufferedReader.readLine()) != null) {
if (line.equals(username)) {
usernameExists = true;
break;
}
}
if (usernameExists) {
System.out.println("Username exists! Please try again.");
} else {
System.out.println("Username accepted");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
の
可能性のある重複した[Javaは:テキストファイルを読み込む方法】(http://stackoverflow.com/questions/2788080/java-how-to-read-a-text-file) – byxor
を取ってください[ツアー](http://stackoverflow.com/tour)と[ヘルプセンター](http://stackoverflow.com/help)のドキュメントをお読みください。特に、あなたは[良い質問をする方法](http://stackoverflow.com/help/how-to-ask)と[トピックに関する]どのような質問を読むべきですか(http://stackoverflow.com/help/on-topic)はここにあります。 – azurefrog
具体的には、コードの助けを借りて質問している場合は、そのコードを入力、予想される出力と実際の出力、エラーなどと共に質問に含める必要があります。[mcve]を含むのが理想的です。 – azurefrog