validation - Writing data to sequential text file with java -



validation - Writing data to sequential text file with java -

there text file 'clients.txt' having next contents:

100 james greenish 123.45 200 sue magenta 12345.67

it has 2 records. in first record '100' business relationship no., 'james' firstname, 'green' lastname , '123.45' balance.

the code below 'accountrecord.java' file containing above fields , routine set methods:

public class accountrecord { private int account; private string firstname; private string lastname; private double balance; // no-argument constructor calls other constructor default values public accountrecord() { this(0, "", "", 0.0); // phone call four-argument constructor } // end no-argument accountrecord constructor // initialize record public accountrecord(int acct, string first, string last, double bal) { setaccount(acct); setfirstname(first); setlastname(last); setbalance(bal); } // end four-argument accountrecord constructor // set business relationship number public void setaccount(int acct) { business relationship = acct; } // end method setaccount // business relationship number public int getaccount() homecoming account; } // end method getaccount // set first name public void setfirstname(string first) { firstname = first; } // end method setfirstname // first name public string getfirstname() { homecoming firstname; } // end method getfirstname // set lastly name public void setlastname(string last) { lastname = last; } // end method setlastname // lastly name public string getlastname() { homecoming lastname; } // end method getlastname //set balance public void setbalance(double bal) { balance = bal; } // end method setbalance // balance public double getbalance() { homecoming balance; } // end method getbalance } // end clas

the code below 'createtextfile.java'. has 1 code line: 'output = new formatter( "clients.txt" );', instantiate formatter object 'clients.text' format:

import java.io.filenotfoundexception; import java.lang.securityexception; import java.util.formatter; import java.util.formatterclosedexception; import java.util.nosuchelementexception; import java.util.scanner; import com.deitel.ch17.accountrecord; public class createtextfile { private formatter output; // object used output text file // enable user open file public void openfile() { seek { output = new formatter( "clients.txt" ); // open file } // end seek grab ( securityexception securityexception ) { system.err.println( "you not have write access file." ); system.exit( 1 ); // terminate programme } // end grab grab ( filenotfoundexception filenotfoundexception ) { system.err.println( "error opening or creating file." ); system.exit( 1 ); // terminate programme } // end grab } // end method openfile // add together records file public void addrecords() { // object written file accountrecord record = new accountrecord(); scanner input = new scanner( system.in ); system.out.printf( "%s\n%s\n%s\n%s\n\n", "to terminate input, type end-of-file indicator ", "when prompted come in input.", "on unix/linux/mac os x type <ctrl> d press enter", "on windows type <ctrl> z press enter" ); system.out.printf( "%s\n%s", "enter business relationship number (> 0), first name, lastly name , balance.", "? " ); while ( input.hasnext() ) // loop until end-of-file indicator { seek // output values file { // retrieve info output record.setaccount( input.nextint() ); // read business relationship number record.setfirstname( input.next() ); // read first name record.setlastname( input.next() ); // read lastly name record.setbalance( input.nextdouble() ); // read balance if ( record.getaccount() > 0 ) { // write new record output.format( "%d %s %s %.2f\n", record.getaccount(), record.getfirstname(), record.getlastname(), record.getbalance() ); } // end if else { system.out.println( "account number must greater 0." ); } // end else } // end seek grab ( formatterclosedexception formatterclosedexception ) { system.err.println( "error writing file." ); return; } // end grab grab ( nosuchelementexception elementexception ) { system.err.println( "invalid input. please seek again." ); input.nextline(); // discard input user can seek 1 time again } // end grab system.out.printf( "%s %s\n%s", "enter business relationship number (>0),", "first name, lastly name , balance.", "? " ); } // end while } // end method addrecords // close file public void closefile() { if ( output != null ) output.close(); } // end method closefile }

the lastly portion of code main method as:

public class createtextfiletest { public static void main( string[] args ) { createtextfile application = new createtextfile(); application.openfile(); application.addrecords(); application.closefile(); } // end main }// end class createtextfiletest

when run class , seek come in new record message as:

run: terminate input, type end-of-file indicator when prompted come in input. on unix/linux/mac os x type <ctrl> d press come in on windows type <ctrl> z press come in come in business relationship number (> 0), first name, lastly name , balance. ? 300 har par 112.235 come in business relationship number (>0), first name, lastly name , balance. ? ^z invalid input. please seek again. come in business relationship number (>0), first name, lastly name , balance. ?

being new programming confused why input invalid. need help rectify code or rectify input. whole code has been submitted problem can judged minutely. using netbean ide 8.0 , jdk-8u5-windows-64.

edit : prepare mistake

ctrl+z not bullet proof method under windows. you'd improve (as business relationship number must > 0) : to terminate input, come in 0 , enter. after line

record.setaccount( input.nextint() ); // read business relationship number

add

if ( record.getaccount() == 0 ) { return; }

java validation

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -