Saturday, April 6, 2019

NumberFormatException example

public class IntegerparseIntExample {
public static void main(String[] args) {
int ten = Integer.parseInt("Not a number", 2);
System.out.println(ten);
}
}

Answer:
Exception in thread "main" java.lang.NumberFormatException: For input string: "Not a number"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)

at IntegerparseIntExample.main(IntegerparseIntExample.java:5)

StringArrays via Collection Example

import java.util.*;
public class StringArraysVIACollection_Example {
public static void main(String[] args) {
String returnArray[] = method1();
System.out.println(returnArray[0]);
System.out.println(returnArray[1]);
System.out.println(returnArray[2]);
System.out.println(returnArray[3]);
System.out.println(returnArray[4]);
System.out.println(returnArray[5]);
System.out.println(returnArray.length);
}
public static String[] method1() {
List l1 = new ArrayList();
l1.add("10");l1.add("20");l1.add("30");l1.add("40");l1.add("50");
String[] array = new String[6];
int i = 0;
Iterator itr = l1.iterator();
while(itr.hasNext()) {
array[i++] = (String) itr.next();
}
return array;
}
}

Thursday, April 4, 2019

adding method in an enum class

import java.io.Serializable;

public enum EnumValues implements Serializable {
   BALAJI("BALAJI"),
   BANG_AND_JANG_KUTTIES("THEY ARE THE WORLD'S GREATEST KUTTIES");

   private String displayName;
 
   private EnumValues(String displayName) {
      this.displayName = displayName;
   }
   public static EnumValues fromString(String bang_and_jang_kutties) {
       return BANG_AND_JANG_KUTTIES;
   }
   public static EnumValues getvalues() {
       return BANG_AND_JANG_KUTTIES;
   }
   public static void main(String[] args) {
   EnumValues[] array = EnumValues.values();
   System.out.println(array[0] +" "+ array[1]);
   System.out.println(EnumValues.getvalues());
   System.out.println(EnumValues.fromString("The Best"));
   }
}

enum values()

public enum EnumValues implements Serializable {
   BALAJI("BALAJI"),
   BANG_AND_JANG_KUTTIES("THEY ARE THE WORLD'S GREATEST KUTTIES");

   private String displayName;
 
   private EnumValues(String displayName) {
      this.displayName = displayName;
   }
 
   public EnumValues getValues(String input) {
   return BALAJI;
   }
   public static void main(String[] args) {
   System.out.println(EnumValues.values());  
   EnumValues[] array = EnumValues.values();
   System.out.println(array[0] +" "+ array[1]);
   }
}

Wednesday, April 3, 2019

How to add Initial Capacity to ArrayList

We can add initial capacity to arraylist like this

List list = new ArrayList(new String[10].length + 5);

Class Array Example


(a)
public class MainArrayTest {
public static void main(String[] args) {
MyExampleTest[] myexamples = new MyExampleTest[20];
myexamples[0] = new MyExampleTest();
myexamples[0].setQueryKey1(EnumValues.BALAJI);
myexamples[0].setQueryKey2(EnumValues.BANG_AND_JANG_KUTTIES);
System.out.println(myexamples[0].getQueryKey1());
System.out.println(myexamples[0].getQueryKey2());

myexamples[1] = new MyExampleTest();
myexamples[1].setQueryKey1(EnumValues.BALAJI);
myexamples[1].setQueryKey2(EnumValues.BANG_AND_JANG_KUTTIES);
System.out.println(myexamples[1].getQueryKey1());
System.out.println(myexamples[1].getQueryKey2());

myexamples[2] = new MyExampleTest();
myexamples[2].setServiceType3("BALAJI");
myexamples[2].setServiceType4("IS THE GREATEST");
System.out.println(myexamples[2].getServiceType3());
System.out.println(myexamples[2].getServiceType4());

myexamples[3] = new MyExampleTest();
String[] familyArraysKuttiValues = { "Bang","Jang","Chellam","Kutties" };
myexamples[3].setServiceType5(new String[10]);
myexamples[3].setServiceType6(familyArraysKuttiValues);
String kutties = myexamples[3].getServiceType6()[0] + " and " +
myexamples[3].getServiceType6()[1] + " and " +
myexamples[3].getServiceType6()[2] + " and " +
myexamples[3].getServiceType6()[3];
System.out.println(kutties + " are the best in the world");

}
}

(b)
public class MyExampleTest {

private EnumValues serviceType1;
private EnumValues serviceType2;

public void setQueryKey1(EnumValues serviceType1) {
this.serviceType1 = serviceType1;
}
public void setQueryKey2(EnumValues serviceType2) {
this.serviceType2 = serviceType2;
}
public EnumValues getQueryKey1() {
return serviceType1;
}
public EnumValues getQueryKey2() {
return serviceType2;
}

private String serviceType3;
private String serviceType4;

public String getServiceType3() {
return serviceType3;
}
public void setServiceType3(String serviceType3) {
this.serviceType3 = serviceType3;
}
public String getServiceType4() {
return serviceType4;
}
public void setServiceType4(String serviceType4) {
this.serviceType4 = serviceType4;
}

private String serviceType5[];
private String serviceType6[];

public String[] getServiceType5() {
return serviceType5;
}
public void setServiceType5(String[] serviceType5) {
this.serviceType5 = serviceType5;
}
public String[] getServiceType6() {
return serviceType6;
}
public void setServiceType6(String[] serviceType6) {
this.serviceType6 = serviceType6;
}
}

(c)

import java.io.Serializable;

public enum EnumValues implements Serializable {
   BALAJI("BALAJI"),
   BANG_AND_JANG_KUTTIES("THEY ARE THE WORLD'S GREATEST KUTTIES");

   private String displayName;
 
   private EnumValues(String displayName) {
      this.displayName = displayName;
   }
}

BooleanTest Example

package searchOrange;
import java.util.HashMap;
public class BooleanTest {
public static void main(String[] args) {
boolean value1=false;
HashMap hmap1 = new HashMap();
hmap1.put("balaji-1",value1?Boolean.TRUE:Boolean.FALSE);
System.out.println("balaji-1 " + hmap1.get("balaji-1"));
//if the boolean value is false
//its taking in only the second value

hmap1.put("balaji-2",value1?Boolean.FALSE:Boolean.TRUE);
System.out.println("balaji-2 " + hmap1.get("balaji-2"));
//if the boolean value is false
//its taking in only the second value


boolean value2=true;
HashMap hmap2 = new HashMap();
hmap2.put("balaji-3",value2?Boolean.FALSE:Boolean.TRUE);
System.out.println("balaji-3 " + hmap2.get("balaji-3"));
//if the boolean value is true
//its taking in only the first value 

hmap2.put("balaji-4",value2?Boolean.TRUE:Boolean.FALSE);
System.out.println("balaji-4 " + hmap2.get("balaji-4"));
//if the boolean value is true   
//its taking in only the first value
}
}

Answer : 

balaji-1 false
balaji-2 true
balaji-3 false
balaji-4 true

BECAUSE THE VALUE OF BOOLEAN IS CHECKED FIRST AS TRUE AND THEN AS FALSE IN THE BOOLEAN CONDITION ITSELF 

AS IN ANYCASE WITH ANY IF ELSE CONDITION...