Java-是否存在Crtrl-D的对立面?

时间:2018-12-01 01:59:48

标签: java text-files java.util.scanner user-input end-of-life

我正在使用扫描仪将输入读取到文本文件,并在其中发布帐户信息。 在此之后,我应该对交易信息执行相同的操作。 但是,在为帐户类输入完信息后,就像按一个一样,按Ctrl-D表示文件结尾(EOF)并存在循环 这很好,除了程序尝试打开下一个文件时,它会跳过允许我输入数据的部分,而只是在没有错误的情况下结束了程序。

有没有一种方法可以解决这个问题或解决此问题,以便我可以制作多个文本文件,并在使用扫描仪时在单个程序中实际输入顺序数据?

这是我的代码

  public class FileMatch
  {
  private Formatter output;
  private Formatter out;


    public void openOldMast()
    {
        try
        {
        output = new Formatter( "oldmast.txt" );
        } // end try
        catch ( SecurityException securityException )
        {
         System.err.println(
             "You do not have write access to this file." );
        System.exit( 1 );
        }
        catch ( FileNotFoundException filesNotFoundException )
        {System.err.println( "Error creating file." );
        System.exit( 1 );
        }
    }


    public void addMast()
    {
    Account record = new Account();

         Scanner input = new Scanner( System.in );



           System.out.printf( "%s\n%s",
              "Enter account number, first name, last name and balance.",
              "? " );

           while ( input.hasNext())
           {
              try
              {
                 record.setAccountNumber( input.nextInt() ); // read account number
                 record.setFirstName( input.next() ); // read first name
                 record.setLastName( input.next() ); // read last name
                 record.setBalance( input.nextDouble() ); // read balance

                 if ( record.getAccountNumber() > 0 )
                 {
                    output.format( "%d %s %s %.2f\n", record.getAccountNumber(),
                       record.getFirstName(), record.getLastName(),
                       record.getBalance() );
                 }
                 else
                 {
                    System.out.println(
                       "Account number must be greater than 0." );
                 }
              }
              catch ( FormatterClosedException formatterClosedException )
              {
                 System.err.println( "Error writing to file." );
                 return;
              }
              catch ( NoSuchElementException elementException )
              {
                 System.err.println( "Invalid input. Please try again." );
                 input.nextLine(); // discard input so user can try again
              }

              System.out.printf("? " );
           }
        input.close();
        }

        public void closeMast()
        {
           if ( output != null )
              output.close();
        }



  public void openTrans()
  {
      try
      {
          out = new Formatter( "trans.txt" );
      }
      catch ( SecurityException securityException )
      {
          System.err.println(
                  "You do not have write access to this file." );
          System.exit( 1 );
      }
      catch ( FileNotFoundException filesNotFoundException )
      {System.err.println( "Error creating file." );
          System.exit( 1 );
      }
  }


  public void addTrans()
  {
      TransactionRecord rec = new TransactionRecord();

      Scanner inn = new Scanner( System.in );



      System.out.printf( "%s\n%s",
              "Enter account number and amount.",
              "? " );

      while ( inn.hasNext() )
      {
          try
          {
              rec.setAccountNumber( inn.nextInt() ); // read account number
              rec.setAmount( inn.nextDouble() ); // read balance

              if ( rec.getAccountNumber() > 0 )
              {
                  out.format( "%d %.2f\n", rec.getAccountNumber(),
                          rec.getAmount() );
              }
              else
              {
                  System.out.println(
                          "Account number must be greater than 0." );
              }
          }
          catch ( FormatterClosedException formatterClosedException )
          {
              System.err.println( "Error writing to file." );
              return;
          }
          catch ( NoSuchElementException elementException )
          {
              System.err.println( "Invalid input. Please try again." );
              inn.nextLine(); // discard input so user can try again
          }

          System.out.printf("? " );
      }
  }

  public void closeTrans()
  {
      if ( out != null )
          out.close();
  }
}

这是我的主要

public static void main( String args[] )
{
    FileMatch app1 = new FileMatch();
    FileMatch app2 = new FileMatch();


    app1.openOldMast();
    app1.addMast();
    app1.closeMast();

    app2.openTrans();
    app2.addTrans();
    app2.closeTrans();



} // end main

0 个答案:

没有答案