Error with file write to notepad it keeps prompting -


/*  * change license header, choose license headers in project properties.  * change template file, choose tools | templates  * , open template in editor.  */ package customerdetails; import java.io.*; import java.util.*; import javax.swing.joptionpane;  /**  *  * @author anna germine peck  */ public class data {     string custname;     string custid;     string custnric;     string custgender;     string custdob;     string custnationality;     string custhousingtype;     string custhousingpayment;     string custmonthlyincome;     string custofficeaddress;     string custcontact;     string custemail;     string custaddress;         customerdetails custdetails;         stringtokenizer st;         //delcare , instantiate new arraylist         arraylist<customerdetails> custlist = new arraylist<customerdetails>();         // int []  array = int new {         filereader fr = null;         bufferedreader br = null;         string infile = "customerdetails.txt";         string line = "";         // how connect label     /**      * @param args command line arguments      */     public static void main(string[] args) {         // todo code application logic here         data data = new data();            try {             //instantiate filereader & bufferedreader objects              data.fr = new filereader(data.infile);             data.br = new bufferedreader(data.fr);             //read line file             data.line = data.br.readline();              //while line not null             while(data.line != null)             {                 //tokenize records                 data.st = new stringtokenizer(data.line, "|");                 data.custname = data.st.nexttoken();                 data.custid = data.st.nexttoken();                 data.custnric = data.st.nexttoken();                 data.custgender = data.st.nexttoken();                 data.custdob = data.st.nexttoken();                 data.custnationality = data.st.nexttoken();                 data.custhousingtype = data.st.nexttoken();                 data.custhousingpayment = data.st.nexttoken();                 data.custmonthlyincome = data.st.nexttoken();                 data.custofficeaddress = data.st.nexttoken();                 data.custcontact = data.st.nexttoken();                 data.custemail = data.st.nexttoken();                 data.custaddress = data.st.nexttoken();                 data.custdetails = new customerdetails(                     data.custname,                     data.custid,                     data.custnric,                     data.custgender,                     data.custdob,                      data.custnationality,                     data.custhousingtype,                     data.custhousingpayment,                     data.custmonthlyincome,                     data.custofficeaddress,                     data.custcontact,                     data.custemail,                     data.custaddress);                 data.custlist.add(data.custdetails);                 data.line = data.br.readline();             }             data.br.close();         }         catch(filenotfoundexception e)         {             joptionpane.showmessagedialog(null,"the file "+data.infile+" not found");         }         catch(ioexception e)         {             system.out.println("reading error!");         }                 {             if(data.fr!=null)                 // if not empty, try             {                 try                     //error ->  unexpected case                 {                     data.fr.close();                 }                 catch(ioexception e)                     //                  {                     system.out.println("error closing file!");                 }             }         }         scanner kb = new scanner(system.in);         // declare scanner read user input          /*data.line = "1";         while (!data.line.equals("6"))         {             system.out.println(project.menu_prompt);             project.line = kb.nextline();             if (project.line.equals("1")) {                 project.checkstocks();             } else if (project.line.equals("2")) {                 project.addingredients();             } else if (project.line.equals("3")) {                 project.removeingredients();             } else if (project.line.equals("4")) {                 project.editstocks();             } else if (project.line.equals("5")) {                 project.displayall();             } else if (project.line.equals("6")) {                 project.saveall();                 system.out.println("program exited");             } else {                  system.out.println("invalid input");             }         }         */         }           public void saveall(){          string wfile_failed = "ingredient_records.txt";          try {           filewriter fw= new filewriter("ingredient_records.txt");             bufferedwriter bw = new bufferedwriter(fw);             printwriter file = new printwriter(bw);          (int i=0; i<custlist.size(); i++){              file.println(custlist.get(i).tostring());          }              file.close();          }           catch(exception e){              joptionpane.showmessagedialog(null,"error","error",                      joptionpane.warning_message);          } } 

and error is:

exception in thread "main" java.util.nosuchelementexception @ java.util.stringtokenizer.nexttoken(stringtokenizer.java:349) @ customerdetails.data.main(data.java:61) 

you getting exception because data file have invalid data. either have blank line or line doesn't contain information.

in case stringtokenizer doesn't have enough token server when calling nexttoken().

you should either use hasmoretokens() before calling nexttoken() verify has next element value available or before proceeding assign values variable can first validate using counttokens() has enough tokens serve.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -