How to store int and string value in bean class? - java

I know this is very basic question yet I am unable to get the perfect answer. I have one bean named Order in which there is one object product_size such as
public Class Order{
private String product_size;
}
Setter and Getter methods are defined respectively in that class. The problem is that product_size may contain string variable such as S or Integer value such as 7.
I am unable to store the Integer value in that bean object.

I have done with only one bean instance and I am check that if value is string it should be added in bean object Simply else Integer.toString() method will convert the int value to String.
Example is given below-
String str; // it is dynamic let there are two values in this 'S' and 8
if (str instancof String){
order.setProduct_Size(str);
}else{
order.setProduct_Size(Integer.toString(str));
}

Related

Intiize the Custom object with empty string in java

I have adresss object with n filed .. while using it i want to intize 1 or 2 filed rest filed i want as - ""
instead of null.
one way is manually adresss.setABC(" ") but do we have way to do it implictily ?
Use the Builder design pattern to create the Objects. Set only the mandatory attributes while creating the Objects. Rest all other fields will be null when not set. Please check https://howtodoinjava.com/design-patterns/creational/builder-pattern-in-java/ on Builder design pattern.
Class variables (or member variables) of each data type has default values. For example,
int number; // number will have default value: 0
float ratio; // default value: 0.0
boolean success; // default value: false
String name; // default: null
Object obj; // default: null
If you want to change this to some other value, you can change the member variable declaration or change the value in your class' constructor.
String name = "";
Declaring the above statement as class variable will ensure that the value is empty instead of null. Another way is to set it in class constructor,
public Address(){
this.name = "";
}

How would you write a constructor that checks that the array passed is the same length as the instance variable in the class?

I need to create a class that initializes the instance variable in it. In the if statement I am getting a Type mismatch error: cannot convert from int to boolean.
public class ProccessForm {
private String UserInfo[];
public ProccessForm(String[] UserInfo) {
UserInfo = new String[6];
if(UserInfo.length){
}
}
// if length of array passed does not equal length of current array
if (UserInfo.length != this.length) {
// do this
}
Right now in your if statement UserInfo.length returns an integer, if statements deal with Boolean logic so you need to use a conditional operator such as (<, >, <=, >=, ==, or !=) when working with primitives.
You are getting a type mismatch error because you're trying to compare an int value to a boolean value. The length field of primitive arrays returns an int data type and if blocks evaluate boolean value so you need to compare it with another object of the same raw type. Learn more about if-then and if-then-else statements as well as Arrays.
The following code should show you how your code should look like but there are a couple of more issues with your code that need to be addressed.
public class ProcessForm {
private String[] userInfo = new String[6];
public ProcessForm(String[] userInfo) {
if (this.userInfo.length == userInfo.length) {
// Do something here...
}
}
}
You should stick to a standard Java naming convention.
You also had a typo in the class name; process is spelled with a single c.
Java does not use C-style array declarations so you should declare your variable like this: String[] userInfo instead of String userInfo[] like you would in C language.
There is no need to initialize the class field inside a constructor when you're not using any parameter values in the initialization process. You can just declare and initialize it on the same line.
Remember to always use this keyword when you want to address class fields that have the same names as local variables otherwise you will be addressing local variables instead.

How to access dynamiclly variables from another Java Class?

My question similar to others but it's little bit more tricky for me
I have a Class DummyData having static defined variables
public static String Survey_1="";
public static String Survey_2="";
public static String Survey_3="";
So, i call them DummyData.Survey_1 and it returns whole string value. Similarly do with DummyData.Survey_2 and DummyData.Survey_3
But the problem is when i call them Dynamically its not return their value.
I have a variable data which value is change dynamically like (data=Survey_1 or data=Survey_2 or data=Survey_3)
I use #Reflection to get its value but failed to get its value
I use methods which I'm mentioning Below help me to sort out this problem.
Field field = DummyData.class.getDeclaredField(data);
String JsonData = field.toString();
and
DummyData.class.getDeclaredField("Survey_1").toString()
but this return package name, class name and string name but not return string value.
What I'm doing can some help me??
Getting the value of a declared field is not as simple as that.
You must first locate the field. Then, you have to get the field from an instance of a class.
Field f = Dummy.class.getDeclaredField(“field”);
Object o = f.get(instanceOfDummy);
String s = (String) o;
Doing the simple toString() of the Field will actually invoke the toString() method of the Field object but won't access the value
You must do something like this:
Field field = SomeClass.class.getDeclaredField("someFieldName");
String someString = (String) field.get(null); // Since the field is static you don't need any instance
Also, beware that using reflection is an expensive and dangerous operation. You should consider redesigning your system

Control return of Type of method in Java

I have a bean with many get but i want to create a generic method that takes this bean and controls the types of the return for its get.
How i can say (boolean method??) that a get method return a primitive data or an Object?
class A { int one; People two; //get method of one and get method of two }
i want a method that say: type of one is primitive; type of two is not primitive
thank you
You can change the return type of the method to a user defined object or a collection and set the value in to the object and return it.
That way you can return any object you want.
In your case you can have a user defined object with private instance variable of all the data types that you might expect and have public getter and setter methods and a method to check what values are not null and return the value.
if primitive it don't have getClass() method else it has.

Losing SuperClass attributes in array - Java

I have a class that extends a superclass. There are some attributes that are in the superclass that I use when constructing my subclass (Candidate). I have a test class for Candidate called CandidateTest.
The short story is, I create an array of candidates with various attributes, then I pass that array into a method. That method then goes through the array and picks certain candidates then creates a new ArrayList with those candidates.
Now, when I'm trying to print some of the values of those candidates, I'm getting a null return on the attributes that are defined in the superclass, but the values from the subclass are showing up fine.
For example, I can do
for(int i=0; i < googleCandidate.size(); i++){
System.out.println(googleCandidate.get(i).getCommunication());
}
and it will print me out the value of all my Candidate's communication value. But if I do this:
System.out.println(googleCandidate.get(i).getFirstName());
it returns 15 lines of null for every Candidate.
When I originally created the Candidate objects, they had 5 values passed into the constructor. However, they seem to have forgotten the values defined in the superclass and only remember the values defined in the subclass.
I had forgotten to initiate the attributes in the superclass when I created an instance of the subclass. I had this code:
public AddressBook(String fn, String ln){
}
And everything worked when I changed it to this:
public AddressBook(String fn, String ln){
firstName = fn;
lastName = ln;
}

Categories