简体   繁体   English

java:所有输入流都以单个ctrl + z终止

[英]java: all input streams terminate with a single ctrl+z

I'm working on a java exercise which needs to get customer data from input and transactions consecutively and saves them to two separate text files. 我正在进行一项java练习,它需要连续从输入和事务中获取客户数据,并将它们保存到两个单独的文本文件中。 while running it needs to get first file's data (oldMaster) and then gets the other one's (trans) from user input. 在运行时需要获取第一个文件的数据(oldMaster),然后从用户输入获取另一个(trans)。 when I use ctrl+z to terminate first input stream the next input doesn't start to work. 当我使用ctrl + z终止第一个输入流时,下一个输入不会开始工作。 here is my code: 这是我的代码:

import java.io.*;
import java.util.*;
public class SampleMaker {
    Formatter output1;
    Formatter output2;
    public void makeSampleOldMaster(){
        String firstName;
        String lastName;
        int accountNumber;
        double balance;

        Scanner input1=new Scanner(System.in);

        try{
         output1=new Formatter("oldmaster.txt");

         System.out.println("Enter account number,FirstName,LastName and balance");
         while(input1.hasNext()){
                accountNumber=input1.nextInt();
                firstName=input1.next();
                lastName=input1.next();
                balance=input1.nextDouble();
                output1.format("%-10d%-10s%-10s%-10.2f",accountNumber,firstName,lastName,balance);
                System.out.println("Enter account number,FirstName,LastName and balance");
            }

        }

        catch(IOException e){
            System.err.println("IO exception");
            System.exit(1);
        }

        catch(NoSuchElementException e){
            System.err.println("Invalid input");
            input1.nextLine();
        }
    }

    public void makeSampleTrans(){
        Scanner input2=new Scanner(System.in);
        double transAmount;
        int accountNumber;
        try{
        output2=new Formatter("trans.txt");
        System.out.println("Enter account number,and Transaction Amount:");
        while(input2.hasNext()){
            accountNumber=input2.nextInt();
            transAmount=input2.nextDouble();
            output2.format("%-10d%-10.2f",accountNumber,transAmount);
            System.out.println("Enter account number,and Transaction Amount:");
        }
        }
        catch(IOException e){
            System.err.println("IO exception");
            System.exit(1);
        }

        catch(NoSuchElementException e){
            System.err.println("Invalid input");
            input2.nextLine();
        }
    }

    public void closeOldMasterFile(){

        if(output1 != null){
            output1.close();
        }   
    }
    public void closeTransFile(){
        if(output2 != null){
            output2.close();
        }
    }
}

when I run this code: 当我运行此代码时:

public class Test {
    public static void main(String[] args){
        SampleMaker s=new SampleMaker();
        s.makeSampleOldMaster();
        s.closeOldMasterFile();
        s.makeSampleTrans();
        s.closeTransFile();

    }
}

after using ctrl+z to end the first input and start the second one, it stops working. 在使用ctrl + z结束第一个输入并启动第二个输入后,它将停止工作。

End of stream is end of stream. 流结束是流的结束。 Once System.in has encountered end of stream, it will deliver that to whatever you may wrap around it as many times as you do so. 一旦System.in遇到了流的结束,它就会将它传递给你可以多次缠绕它的任何东西。 Your design is incorrect. 你的设计不正确。 You'll have to use something else to delimit the first set of input. 您将不得不使用其他东西来分隔第一组输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM