1

I am trying to print out the results of the users inputs to a simple text file but everything I have tried has rendered unsuccessful. I tried using a PrintWriter within the switch case but the results still just printed out null. I am quite new to Java so maybe I am missing obvious? Here is the code:

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;
            }

       }



}

}
5
  • 3
    Whenever you write if(boolean = true) a Java programmer dies... This is an assignment operation and always returns true. You need if(boolean == true) or, in fact, if(boolean). Commented Dec 8, 2013 at 9:40
  • 1
    This is the offending line that Boris is pointing out: while(proceed2=true). You should be re-writing it while(proceed2). Check the Java Tutorial if you need help writing to file. Commented Dec 8, 2013 at 9:42
  • beside what boris said, what kind of error you get? Commented Dec 8, 2013 at 9:43
  • There are multiple layers of issues with this code. This code needs need to be re-factored. en.wikipedia.org/wiki/Code_refactoring. I will make a stab at doing some simple refactoring that will help explain some of the Java language issues that you currently learning then I will make further recommendation. Commented Dec 8, 2013 at 9:55
  • Didn't mean to offend anyone but thanks for the tip. I realised the codes are pretty messy. As for printing it to a file, any ideas? Commented Dec 8, 2013 at 10:18

1 Answer 1

1

Here's a second pass at re-factoring your code.

So now in this refactoring we capture and store the newly created CarOwner objects and store them in a list.

Then we see how to go through that List of CarOwner's and then write those objects to a file called carOwners.dat

Ordinarily, in industry, code re-factoring is done in the context of having a set of unit tests against which you can ensure that the refactoring hasn't broken the required behaviour of the code but we are just learning here so this work serves to explain some of the concepts that you are missing and this first pass iteration below has some issues of its own so don't take this as the final product.

Refactorings

  1. I have created a CarOwner class.
  2. I have renamed the Boolean variable canProceed so that it reads more naturally.
  3. Update : I have made the CarOwner class Serializable; this will allow us to write the Object to a File.
  4. Update : I have added code that up the new CarOwners and adds it to a List and then I iterate over the list to write those CarOwner objects to a 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 class Registration { public static void main(String[] args) { List 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();
            }
        }
    }
    

    }

Here's what the CarOwner class now looks like ...

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 + '\'' +
                '}';
    }
}

Ok so creating the CarOwner class is done to make a start at making this code more object oriented.

Secondly the re-factored code demonstrates correct use of a Boolean variable in Java.

As the other commentators have already pointed out the assignment operator = is easily confused with the test for Boolean equality. See Java Operators

Also I have renamed Boolean proceed; to be Boolean canProceed; This is a common strategy. Naming a Boolean variable to read as a question to which the "answer" is, yes or no, or True or False.

This then means we can write code like while(canProceed) which reads very easily. See also if statement on the Java tutorial

I hope this helps.

Sign up to request clarification or add additional context in comments.

4 Comments

This definitely helped in making me understand better. However I am confused now if I am suppose to print the file through the car owner class or during the input process it self.
OK re-read your question. I have an understanding of what you are trying to achieve. I'll update my answer.
String has an equalsIgnoreCase method. It also has the toLowerCase and toUpperCase methods. Comparing both possible cases is one of the seven deadly sins*. (*There are many more than seven.)
Fair comment Boris, thank you! it was a automatic code refactoring of the switch statement offered by IntelliJ.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.