java—将名称添加到文本文件并从用户输入中搜索名称

ddarikpa  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(327)

我正在尝试使用file writer向文本文件添加名称,并使用scanner类搜索名称。我的问题是,当我向文本文件中添加多个名称并搜索添加到文本文件中的名字时,显示的是第一个名称,但所有其他名称都不显示。这是密码

  1. public class Test {
  2. public static void main(String[] args) throws IOException {
  3. menu();
  4. }
  5. public static void menu() throws IOException {
  6. Scanner input = new Scanner(System.in);
  7. System.out.println("1. Add name");
  8. System.out.println("2. Done");
  9. int choice = input.nextInt();
  10. switch (choice) {
  11. case 1: {
  12. addName();
  13. break;
  14. }
  15. case 2: {
  16. verify();
  17. break;
  18. }
  19. default: {
  20. System.out.println("Invalid Selection");
  21. break;
  22. }
  23. }
  24. }
  25. public static void addName() {
  26. Scanner scan = new Scanner(System.in);
  27. try {
  28. FileWriter writer = new FileWriter("Names.txt", true);
  29. writer.write("\r\n");
  30. System.out.println("Enter Name: ");
  31. writer.write(scan.nextLine());
  32. writer.close();
  33. System.out.println("name added to file");
  34. menu();
  35. } catch (IOException e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. public static void verify()
  40. throws IOException {
  41. Scanner textfile = new Scanner(new File("Names.txt"));
  42. Scanner VerifyScnr = new Scanner(System.in);
  43. System.out.print("Enter name: ");
  44. String Name = VerifyScnr.next();
  45. while (textfile.hasNext()) {
  46. String search = textfile.next();
  47. if (search.equalsIgnoreCase(Name)) {
  48. System.out.println("This name is in the file");
  49. menu();
  50. } else {
  51. System.out.println("This name is not in the file");
  52. verify();
  53. }
  54. }
  55. }
  56. }
eni9jsuy

eni9jsuy1#

试试这个

  1. public class Test {
  2. public static void main(String[] args) throws IOException {
  3. menu();
  4. }
  5. public static void menu() throws IOException {
  6. Scanner input = new Scanner(System.in);
  7. System.out.println("1. Add name");
  8. System.out.println("2. Search");
  9. System.out.println("3. Done");
  10. int choice = input.nextInt();
  11. switch (choice) {
  12. case 1: {
  13. addName();
  14. break;
  15. }
  16. case 2: {
  17. verify();
  18. break;
  19. }
  20. case 3: {
  21. // terminating execution
  22. System.exit(0);
  23. }
  24. default: {
  25. System.out.println("Invalid Selection");
  26. break;
  27. }
  28. }
  29. }
  30. public static void addName() {
  31. Scanner scan = new Scanner(System.in);
  32. try {
  33. FileWriter writer = new FileWriter("Names.txt", true);
  34. writer.write("\r\n");
  35. System.out.println("Enter Name: ");
  36. writer.write(scan.nextLine());
  37. writer.close();
  38. System.out.println("name added to file");
  39. menu();
  40. } catch (IOException e) {
  41. e.printStackTrace();
  42. }
  43. }
  44. public static void verify() throws IOException {
  45. Scanner textfile = new Scanner(new File("Names.txt"));
  46. Scanner VerifyScnr = new Scanner(System.in);
  47. boolean isFound = false;
  48. System.out.print("Enter name: ");
  49. String Name = VerifyScnr.next();
  50. while (textfile.hasNext()) {
  51. String search = textfile.next();
  52. if (search.equalsIgnoreCase(Name)) {
  53. System.out.println(Name + " is in the file");
  54. isFound = true;
  55. break;
  56. }
  57. }
  58. if (!isFound) {
  59. System.out.println(Name + " is not in the file");
  60. }
  61. menu();
  62. }
  63. }
展开查看全部

相关问题