使用java将字符串数组打印到文本文件

时间:2013-12-08 09:35:22

标签: java arrays string text printwriter

我正在尝试将用户输入的结果打印到一个简单的文本文件中,但我尝试过的所有内容都失败了。我尝试在开关盒中使用PrintWriter,但结果仍然只打印出null。我对Java很陌生,所以也许我错过了明显的东西? 这是代码:

package parkingsystem;

import java.io.*;
import java.util.Scanner;

public class Registration {

   static String [][] userData;
   static String [][] fileData;

    public static void Registration(){

        Scanner input = new Scanner(System.in);
        String lotNo;
        String First;
        String Last;
        String studentID;
        String phoneNo;
        String email;
        String carNo;
        String dateReg;
        String strcontent;
        String proceed;
        boolean proceed2 = true;
        userData = new String [50][6];
        fileData = new String [50][6];
        int counter = 0;
        int col;
        int row;
        boolean carry_on = true;
        MainMenu choices = new MainMenu();

       while(proceed2=true){
        System.out.println("Press Y/y to add a new user");
        System.out.println("Press N/n to return to menu");
        proceed = input.nextLine();
            switch (proceed) {
                case "Y":
                case "y":
                    System.out.println("Enter your student ID");
                    studentID = input.nextLine();
                    System.out.println("Enter your first name");
                    First = input.nextLine();
                    System.out.println("Enter your last name");
                    Last = input.nextLine();
                    System.out.println("Enter your car number");
                    carNo = input.nextLine();
                    System.out.println("Enter your contact number");
                    phoneNo = input.nextLine();
                    System.out.println("Enter your email address");
                    email = input.nextLine();
                    row = counter ;
                    userData [row][0] = studentID;
                    userData [row][1] = First;
                    userData [row][2] = Last;
                    userData [row][3] = carNo;
                    userData [row][4] = phoneNo;
                    userData [row][5] = email;
                    if (counter == 6){
                        carry_on=false;
                    }  

                    proceed2 = false;
                    break;
                case "N":
                case "n":
                    choices.Menus();
                    break;
            }

       }



}

}

1 个答案:

答案 0 :(得分:1)

这是重新分解代码的第二遍

所以现在在这个重构中我们捕获并存储新创建的CarOwner对象并将它们存储在列表中。

然后我们看看如何浏览CarOwner的List,然后将这些对象写入名为carOwners.dat的文件

通常,在行业中,代码重新分解是在具有一组单元测试的上下文中完成的,您可以确保重构没有破坏代码的所需行为,但我们只是在这里学习所以这工作用来解释你缺少的一些概念,下面的第一遍迭代有一些问题,所以不要把它作为最终产品。

的重构

  1. 我创建了一个CarOwner类。
  2. 我已经重命名了布尔变量canProceed,以便它更自然地读取。
  3. 更新:我已将CarOwner类设为Serializable;这将允许我们将对象写入文件。
  4. 更新:我添加了新CarOwners的代码并将其添加到List中,然后迭代列表以将这些CarOwner对象写入FileStream。

    package parkingsystem;

    import java.io.FileNotFoundException; import java.io.ObjectOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.io.FileOutputStream; import java.util.Scanner;

    公共课程注册{     public static void main(String [] args){         列出carOwners = new ArrayList();

        Scanner input = new Scanner(System.in);
        boolean canProceed = true;
    
        while (canProceed) {
            System.out.println("Press Y/y to add a new user");
            System.out.println("Press N/n to return to menu");
            String optionRequested = input.nextLine();
    
            if (optionRequested.equalsIgnoreCase("Y")) {
                CarOwner owner = new CarOwner();
                System.out.println("Enter your student ID");
                owner.setStudentID(input.nextLine());
                System.out.println("Enter your first name");
                owner.setFirst(input.nextLine());
                System.out.println("Enter your last name");
                owner.setLast(input.nextLine());
                System.out.println("Enter your car number");
                owner.setCarNo(input.nextLine());
                System.out.println("Enter your contact number");
                owner.setContactNumber(input.nextLine());
                System.out.println("Enter your email address");
                owner.setEmail(input.nextLine());
                owner.setDateReg(new Date().toString());
                carOwners.add(owner);
            } else if (optionRequested.equals("N") || optionRequested.equals("n")) {
                canProceed = false;
            }
    
        }
    
        ObjectOutputStream objectWriter = null;
    
        for (CarOwner carOwner : carOwners) {
            try {
                objectWriter = new ObjectOutputStream(new FileOutputStream("carOwners.dat"));
                objectWriter.writeObject(carOwner);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    }

  5. 以下是CarOwner课程现在的样子......

    package parkingsystem;
    
    import java.io.Serializable;
    
    public class CarOwner implements Serializable{
        private String First;
        private String Last;
        private String studentID;
        private String email;
        private String carNo;
        private String dateReg;
        private String contactNumber;
    
        public CarOwner() {
        }
    
        public String  getFirst() {
            return First;
        }
    
        public void setFirst(String first) {
            First = first;
        }
    
        public String getLast() {
            return Last;
        }
    
        public void setLast(String last) {
            Last = last;
        }
    
        public String getStudentID() {
            return studentID;
        }
    
        public void setStudentID(String studentID) {
            this.studentID = studentID;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public String getCarNo() {
            return carNo;
        }
    
        public void setCarNo(String carNo) {
            this.carNo = carNo;
        }
    
        public String getDateReg() {
            return dateReg;
        }
    
        public void setDateReg(String dateReg) {
            this.dateReg = dateReg;
        }
    
        public void setContactNumber(String contactNumber) {
            this.contactNumber = contactNumber;
        }
    
        public String getContactNumber() {
            return contactNumber;
        }
    
        @Override
        public String toString() {
            return "CarOwner{" +
                    "First='" + First + '\'' +
                    ", Last='" + Last + '\'' +
                    ", studentID='" + studentID + '\'' +
                    ", email='" + email + '\'' +
                    ", carNo='" + carNo + '\'' +
                    ", dateReg='" + dateReg + '\'' +
                    ", contactNumber='" + contactNumber + '\'' +
                    '}';
        }
    }
    

    好的,所以创建CarOwner类是为了让这个代码更加面向对象。

    其次,重新分解的代码演示了在Java中正确使用布尔变量。

    正如其他评论员已经指出的那样,赋值运算符=很容易与布尔相等的测试混淆。见Java Operators

    此外,我已将Boolean proceed;重命名为Boolean canProceed;这是一种常见策略。将布尔变量命名为“回答”为的问题,,或 True False < / em>的

    这意味着我们可以编写像while(canProceed)这样非常容易阅读的代码。另请参阅if statement on the Java tutorial

    我希望这会有所帮助。