1

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
Sarah Wadley
  • 21
  • 1
  • 5
  • 1
    CTRL+D closes the input stream. There is no undo of that. Don't use CTRL+D to mark end of input (for now). If user is supposed to enter all 4 values on a single line, use e.g. a blank line to mark end of input. You'd need to call `nextLine()` instead of 4 different `nextXxx()` calls, though. – Andreas Dec 01 '18 at 02:04
  • `while ( inn.hasNext() )` is probably not correct. If all you want is two values (account # and amount), then you should read until you have those two values, then exit. Either use `return` to exit the method entirely, or you can use `break` to exit the `while` loop and continue after its final brace. – markspace Dec 01 '18 at 02:10
  • 1
    Note that the mapping of ^D to end-of-file is done either by the OS, or by the console (or whatever) application that is interpreting the key-press events. All before Java even sees the characters. – Stephen C Dec 01 '18 at 02:12

0 Answers0