文件写入记事本时出错,它会一直提示

时间:2016-01-16 14:34:48

标签: compiler-errors

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the 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 and instantiate a new ArrayList
        ArrayList<CustomerDetails> custList = new ArrayList<CustomerDetails>();
        // int []  array = int new {
        FileReader fr = null;
        BufferedReader br = null;
        String inFile = "CustomerDetails.txt";
        String line = "";
        // look for how to connect to label
    /**
     * @param args the 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 a line from the file
            data.line = data.br.readLine();

            //While line is not null
            while(data.line != null)
            {
                //Tokenize the 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+" was not found");
        }
        catch(IOException e)
        {
            System.out.println("Reading Error!");
        }
        finally
        {
            if(data.fr!=null)
                // if not empty, will try
            {
                try
                    //error ->  unexpected case
                {
                    data.fr.close();
                }
                catch(IOException e)
                    // 
                {
                    System.out.println("Error closing file!");
                }
            }
        }
        Scanner kb = new Scanner(System.in);
        // declare a scanner to 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 is 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);
         for (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);
         }
}

错误是:

Exception in thread "main" java.util.NoSuchElementException at java.util.StringTokenizer.nextToken(StringTokenizer.java:349) at CustomerDetails.data.main(data.java:61)

1 个答案:

答案 0 :(得分:0)

您收到此异常,因为数据文件包含一些无效数据。它有一个空行或一行不包含所有信息。

在这种情况下,当您呼叫StringTokenizer时,nextToken()没有足够的令牌服务器。

您应该在调用hasMoreTokens()之前使用nextToken()来验证它是否具有可用的下一个元素值,或者在继续为您可以使用countTokens()验证的任何变量分配值之前使用curl -X PUT -T - [request-url]?name=[object-name] < testfile足够的令牌服务。