Home > database >  Serialization of String[]: how to convert String value to String[] one?
Serialization of String[]: how to convert String value to String[] one?

Time:12-19

here is code for main

public static void main(String[] args) {
        Container container= new Container();
        Serializator serializator = new Serializator();
        container.setvalue("1st val");
        serializator.serialization(container);
    }

here is code for container

public class Container implements Serializable {

      
    private static final long serialVersionUID = 1L;
        /**
         * Holds the elements of a container.
         */
        private String[] values;
        
     
       public String[] getvalue() {
        return values;
           
       }
       public void setvalue(String[] values) {
           this.values=values;
       }
    }




here is code for a serializator

public class Serializator {
    public boolean serialization(Container container) {
        boolean flag=false;
        File file= new File("C:/conatiner.data");
        ObjectOutputStream oos = null;
        try {
            FileOutputStream fos= new FileOutputStream(file);
            if(fos != null) {
                oos= new ObjectOutputStream(fos);
                oos.writeObject(container);
                flag=true;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if(oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return flag;
    }
}

Well, the program should be working in a following way: you create a container that has an array of strings,(you can set the values in it) and then the program must serialize it. but the problem is that the tutorial worked with the String value, but not the String[] one. how can i make it understand the String[] value and insert it? The crashlog is the following

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method setvalue(String[]) in the type Container is not applicable for the arguments (String)

    at ua.khpi.oop.taradai06.program6.main(program6.java:7)

"container.setvalue("1st val");"

CodePudding user response:

First, let's focus on the bug: In the container, you promised you will send an array of string in the function setValue, but you are sending a single string. There are two things you could do;

  • keep the container code as it is and send array of strings from main
  • Change the container code and let setValue get a single string value and add it to the values array

And you main question related with serialisation please check that post

CodePudding user response:

If you promised you'll use the String[] parameter, then you should note that. The following version of using setvalue does this work perfectly

container.setvalue(new String[] {"1st val","2nd val","3rd val"});

CodePudding user response:

In order to accept both String and String[] as an input parameter to Container::setvalue method, the varargs should be used, then a single String is accepted as an array consisting of one element.

Also, Java naming conventions for getters/setters of Java Beans specify to capitalize the property names after get/set verb: Getter and setter method names are composed of the word get or set, respectively, plus the property name with the first character of each word capitalized, so the methods should be names as getValues/setValues:

// Container class
public void setValues(String... values) {
    this.values = values;
}

Then this method can be invoked as follows without additional overloading:

container.setValues(); // empty array new String[0]
container.setValues("a string"); // new String[1]{"a string"}
container.setValues("a", "b"); // new String[2]{"a",  "b"}
container.setValues(new String[]{"1", "2", "3"}); // common array
  • Related