【问题标题】:How to record customer infromations [duplicate]如何记录客户信息[重复]
【发布时间】:2021-03-11 18:03:40
【问题描述】:

我目前正在开发一个从 txt 文件中删除特定客户条目的基本功能,目前我所拥有的代码仅搜索客户的名称并仅删除名称。如何更改此设置,以便从 txt 文件中删除所有客户信息,而不仅仅是他们的姓名?

txt 文件中的客户数据以这种方式格式化:

Dave Ted
10 Roberts Drive
12345
1000
B&Q £23.72, Úber £13.50, Eleanor Humphries £78.21
Mike Valencia
9 Farrels Way
56789
100
Cineworld £20.00, TGI £39.30, RadioShack £60.19

代码中的方法

public static void removeCus()throws IOException{

    Scanner stdin = new Scanner( System.in);
    String name,address,id,balance,transactions,record;;

    File inputFile = new File("text_files/customers.txt"); 
    File tempFile = new File("text_files/tempFile.txt"); 

    BufferedReader br = new BufferedReader(new FileReader(inputFile)); 
    BufferedWriter bw = new BufferedWriter(new FileWriter(tempFile)); 

    System.out.println("Enter the Customers name: ");
    name =  stdin.nextLine();

    while( ( record = br.readLine() ) != null ) {
        if( record.contains(name) ) 
            continue;

        bw.write(record);
        bw.flush();
        bw.newLine();
    }

    br.close();
    bw.close();
    inputFile.delete();
    tempFile.renameTo(inputFile); 

    boolean successful = tempFile.renameTo(inputFile); 
}

【问题讨论】:

    标签: java


    【解决方案1】:

    为此,您需要忽略接下来的 4 行。像这样的:

    System.out.println("Enter the Customers name: ");
    name =  stdin.nextLine();
    int ignoreLines = 0; // set this value to 4 when customer name is found
    
    while((record = br.readLine()) != null) {
        if(record.contains(name)) {
            ignoreLines = 4;  // ignore next 4 lines
            continue;
        }
    
        if(ignoreLines > 0) {
            ignoreLines--;
            continue;
        }
    
        bw.write(record);
        bw.flush();
        bw.newLine();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-24
      相关资源
      最近更新 更多