Java- does an opposite of Crtrl-D exist?

40 Views Asked by At

I'm using Scanner to read input to a textfile where I post Account info. After this I'm supposed to do the same thing with Transaction info. However after I finish putting in info for the account class, I press Ctrl-D, as one does, to indicate end of file (EOF) and exist the loop This is great except when the program attempts to open the next file, it skips the part where I'm allowed to input data and just ends the program without error.

Is there a way to combat or work around this so I can make multiple textfiles and actually input sequential data in a single program while using Scanner?

Here's my code

  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();
  }
}

and heres my main

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

There are 0 best solutions below