Home > Enterprise >  Validity checking in Java
Validity checking in Java

Time:05-01

First off, I am extremely new to coding and I need an answer to this urgently. I am asked to create an array or arraylist of objects using my class that requires a "Validity checking component for the input of each attribute entered. Such checking requires the use of at least one loop and an if-else structure." I have no idea how to make this and I am extremely confused about what it's asking me. My code so far looks like this:

import java.util.ArrayList;

public class AList {

public static void main(String[] args)
{
    Author a1 = new Author("Stephen King", "[email protected]", "Male", "74","It","The Body");
    Author a2 = new Author("Neil Gaiman", "N/A", "Male","61","Coraline","Eternals");
    Author a3 = new Author("Margaret Atwood", "N/A","Female","82","The Handmaid's Tale","Cat's Eye");

    ArrayList<Author> author1 = new ArrayList<>();
    author1.add(a1);
    System.out.println(author1.get(0).name);
    System.out.println(author1.get(0).email);
    System.out.println(author1.get(0).gender);
    System.out.println(author1.get(0).age);
    System.out.println(author1.get(0).favBook);
    System.out.println(author1.get(0).underBook);
    System.out.println("*************************");

    ArrayList<Author> author2 = new ArrayList<>();
    author2.add(a2);
    System.out.println(author2.get(0).name);
    System.out.println(author2.get(0).email);
    System.out.println(author2.get(0).gender);
    System.out.println(author2.get(0).age);
    System.out.println(author2.get(0).favBook);
    System.out.println(author2.get(0).underBook);
    System.out.println("*************************");

    ArrayList<Author> author3 = new ArrayList<>();
    author3.add(a3);
    System.out.println(author3.get(0).name);
    System.out.println(author3.get(0).email);
    System.out.println(author3.get(0).gender);
    System.out.println(author3.get(0).age);
    System.out.println(author3.get(0).favBook);
    System.out.println(author3.get(0).underBook);
    System.out.println("*************************");


}

public static class Author{

    private String name;
    private String email;
    private final String gender;
    private final String age;
    private final String favBook;
    private final String underBook;

    public Author(String name, String email, String gender, String AGE, String FavBook, String UnderBook) {
        this.name = name;
        this.email = email;
        this.gender = gender;
        this.age = AGE;
        this.favBook = FavBook;
        this.underBook = UnderBook;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getName() {
        return this.name;
    }
    public String getEmail() {
        return this.email;
    }
    public String getGender() {
        return this.gender;
    }
    public String getAge() {
        return this.age;
    }
    public String getFavBook() {
        return this.favBook;
    }
    public String getUnderBook() {
        return this.underBook;
    }

}





    }

Also, another component to this code that I have to complete is "Files with being used to load and save array/ArrayList objects from a text file (.txt extension using Tab Separated Values format.)" which I also have no idea how to do. I am so confused and I really need help! How do I go about completing all of this?

CodePudding user response:

Take out the validation logic in separate method

public Map<String,String> validate(Author author) { 
    Map<String,String>errors= new HashMap();
    String name = auth.getName();
    if(name==null || name.isEmpty(){
       errors.put("name","Name is required");
    }
}

For reading files, you can use BufferedReader. Use readLine() method to read a row from file, use String classes split method to split it on tab separator

CodePudding user response:

I'm not really sure what is required for an object to be "valid" in your context. However, I can help with the second part. You can use a PrintWriter to print to a file, and "\t" to print a tab. You can see what Tab Separated Values is from https://en.wikipedia.org/wiki/Tab-separated_values.

PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("file.txt")));

use this statement to print an Author's variables in Tab separated values format:

pw.println(a1.name   "\t"   a1.email   "\t"   a1.gender   "\t"   a1.age   "\t"   a1.favBook   "\t"   a1.underBook);

Make sure to call pw.close() after you are done writing.

To parse all Author objects from a text file, you can use a Bufferedreader:

BufferedReader br = new BufferedReader(new FileReader("file.txt"));
ArrayList<Author> authors = new ArrayList<>();
String input = br.readLine();
while(input != null){
    String[] in = input.split("\t");
    authors.add(new Author(in[0], in[1], in[2], in[3], in[4], in[5]));
    input = br.readLine();
}

the String.split() method will split the string into several Strings. In this case, it will split the String at every location where there is a tab, or "\t".

Using BufferedReader will throw an error, but if you add "throws IOException" right after the method name and right before the parenthesis, it will work.

public static void main(String[] args) throws IOException {...

Another suggestion I have is that when you print the variables of an Author, you don't need to add them to an ArrayList, you can directly print them.

  •  Tags:  
  • java
  • Related