August 25, 2018

How to get the list of countries in java

/**
 * How to get the list of countries in java
 */
package com.belazy.stackoverflow;

import java.util.Locale;

/**
 * @author it section kozhikode
 *
 */
public class CountriesList {

/**
* @param cfed
*/
public static void main(String[] cfed) {
// TODO Auto-generated method stub
String[] locales = Locale.getISOCountries();
for(String locale : locales){
System.out.print(locale+ "   ");
Locale locCon = new Locale("", locale);
System.out.println(locCon.getDisplayCountry()+" "+locCon.getDisplayLanguage());
}

}

}



Output


AD   Andorra 
AE   United Arab Emirates 
AF   Afghanistan 
AG   Antigua and Barbuda 
AI   Anguilla 
AL   Albania 
AM   Armenia 
AN   Netherlands Antilles 
AO   Angola 
AQ   Antarctica 
AR   Argentina 
AS   American Samoa 
AT   Austria 
AU   Australia 
AW   Aruba 
AX   Åland Islands 
AZ   Azerbaijan 
BA   Bosnia and Herzegovina 
BB   Barbados 
BD   Bangladesh 
BE   Belgium 
BF   Burkina Faso 
BG   Bulgaria 
BH   Bahrain 
BI   Burundi 
BJ   Benin 
BL   Saint Barthélemy 
BM   Bermuda 
BN   Brunei 
BO   Bolivia 
BQ   Bonaire, Sint Eustatius and Saba 
BR   Brazil 
BS   Bahamas 
BT   Bhutan 
BV   Bouvet Island 
BW   Botswana 
BY   Belarus 
BZ   Belize 
CA   Canada 
CC   Cocos Islands 
CD   The Democratic Republic Of Congo 
CF   Central African Republic 
CG   Congo 
CH   Switzerland 
CI   Côte d'Ivoire 
CK   Cook Islands 
CL   Chile 
CM   Cameroon 
CN   China 
CO   Colombia 
CR   Costa Rica 
CS   Serbia and Montenegro 
CU   Cuba 
CV   Cape Verde 
CW   Curaçao 
CX   Christmas Island 
CY   Cyprus 
CZ   Czech Republic 
DE   Germany 
DJ   Djibouti 
DK   Denmark 
DM   Dominica 
DO   Dominican Republic 
DZ   Algeria 
EC   Ecuador 
EE   Estonia 
EG   Egypt 
EH   Western Sahara 
ER   Eritrea 
ES   Spain 
ET   Ethiopia 
FI   Finland 
FJ   Fiji 
FK   Falkland Islands 
FM   Micronesia 
FO   Faroe Islands 
FR   France 
GA   Gabon 
GB   United Kingdom 
GD   Grenada 
GE   Georgia 
GF   French Guiana 
GG   Guernsey 
GH   Ghana 
GI   Gibraltar 
GL   Greenland 
GM   Gambia 
GN   Guinea 
GP   Guadeloupe 
GQ   Equatorial Guinea 
GR   Greece 
GS   South Georgia And The South Sandwich Islands 
GT   Guatemala 
GU   Guam 
GW   Guinea-Bissau 
GY   Guyana 
HK   Hong Kong 
HM   Heard Island And McDonald Islands 
HN   Honduras 
HR   Croatia 
HT   Haiti 
HU   Hungary 
ID   Indonesia 
IE   Ireland 
IL   Israel 
IM   Isle Of Man 
IN   India 
IO   British Indian Ocean Territory 
IQ   Iraq 
IR   Iran 
IS   Iceland 
IT   Italy 
JE   Jersey 
JM   Jamaica 
JO   Jordan 
JP   Japan 
KE   Kenya 
KG   Kyrgyzstan 
KH   Cambodia 
KI   Kiribati 
KM   Comoros 
KN   Saint Kitts And Nevis 
KP   North Korea 
KR   South Korea 
KW   Kuwait 
KY   Cayman Islands 
KZ   Kazakhstan 
LA   Laos 
LB   Lebanon 
LC   Saint Lucia 
LI   Liechtenstein 
LK   Sri Lanka 
LR   Liberia 
LS   Lesotho 
LT   Lithuania 
LU   Luxembourg 
LV   Latvia 
LY   Libya 
MA   Morocco 
MC   Monaco 
MD   Moldova 
ME   Montenegro 
MF   Saint Martin 
MG   Madagascar 
MH   Marshall Islands 
MK   Macedonia 
ML   Mali 
MM   Myanmar 
MN   Mongolia 
MO   Macao 
MP   Northern Mariana Islands 
MQ   Martinique 
MR   Mauritania 
MS   Montserrat 
MT   Malta 
MU   Mauritius 
MV   Maldives 
MW   Malawi 
MX   Mexico 
MY   Malaysia 
MZ   Mozambique 
NA   Namibia 
NC   New Caledonia 
NE   Niger 
NF   Norfolk Island 
NG   Nigeria 
NI   Nicaragua 
NL   Netherlands 
NO   Norway 
NP   Nepal 
NR   Nauru 
NU   Niue 
NZ   New Zealand 
OM   Oman 
PA   Panama 
PE   Peru 
PF   French Polynesia 
PG   Papua New Guinea 
PH   Philippines 
PK   Pakistan 
PL   Poland 
PM   Saint Pierre And Miquelon 
PN   Pitcairn 
PR   Puerto Rico 
PS   Palestine 
PT   Portugal 
PW   Palau 
PY   Paraguay 
QA   Qatar 
RE   Reunion 
RO   Romania 
RS   Serbia 
RU   Russia 
RW   Rwanda 
SA   Saudi Arabia 
SB   Solomon Islands 
SC   Seychelles 
SD   Sudan 
SE   Sweden 
SG   Singapore 
SH   Saint Helena 
SI   Slovenia 
SJ   Svalbard And Jan Mayen 
SK   Slovakia 
SL   Sierra Leone 
SM   San Marino 
SN   Senegal 
SO   Somalia 
SR   Suriname 
ST   Sao Tome And Principe 
SV   El Salvador 
SX   Sint Maarten (Dutch part) 
SY   Syria 
SZ   Swaziland 
TC   Turks And Caicos Islands 
TD   Chad 
TF   French Southern Territories 
TG   Togo 
TH   Thailand 
TJ   Tajikistan 
TK   Tokelau 
TL   Timor-Leste 
TM   Turkmenistan 
TN   Tunisia 
TO   Tonga 
TR   Turkey 
TT   Trinidad and Tobago 
TV   Tuvalu 
TW   Taiwan 
TZ   Tanzania 
UA   Ukraine 
UG   Uganda 
UM   United States Minor Outlying Islands 
US   United States 
UY   Uruguay 
UZ   Uzbekistan 
VA   Vatican 
VC   Saint Vincent And The Grenadines 
VE   Venezuela 
VG   British Virgin Islands 
VI   U.S. Virgin Islands 
VN   Vietnam 
VU   Vanuatu 
WF   Wallis And Futuna 
WS   Samoa 
YE   Yemen 
YT   Mayotte 
ZA   South Africa 
ZM   Zambia 
ZW   Zimbabwe 


August 17, 2018

To check whether a number is prime in java algorithm

/**
 * To check whether a number is prime in java algorithm
 */
package com.belazy.tricks;

import java.math.BigInteger;

/**
 * @author itsection kozhikode
 *
 */
public class Consumerfed {

/**
* @param args
*/
public static void main(String[] args) {
//multiplying by 2
int i = 10;
i= i << 1;
System.out.println(i);

//swapping 2 numbers
int a = 20;
int b = 30;
b^=a;
a^=b;
b^=a;
System.out.println("a="+a +": b="+b);

//counting the digits
double n =10000000;
double N = Math.floor(Math.log10(n )) + 1;
System.out.println("number's count : "+N);

//checking prime number
int p = 17;
System.out.print("checking prime number :");
System.out.println(BigInteger.valueOf(p).isProbablePrime(1));

System.out.println("-- Jerin V George");


}

}

August 16, 2018

Writing JSON to a file in java

package com.belazy.fileops;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;

/**
 *
 * @author consumerfed IT
 * itatcalicut@gmail.com
 *
 */
public class WritingToFile {

public static void main(String[] args) {

WritingToFile s = new WritingToFile();

String requestStr = "{Data science is future, learn artificial intelligence}";
s.saveJSONToFile(requestStr, "consumerfed", "itesction", "kozhikode",
"8281808029");
// s.saveJSONToFile2(requestStr, "consumerfed", "itesction", "kozhikode",
// "8281808029");
}

public static void saveJSONToFile(String requestStr, String tripID,
String reqRes, String supplier, String folderName) {
PrintWriter out1 = null;
try(PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter(
f.getPath() + File.separator + tripID, true)));
out1) {
String path1 = "D://beebeesotware";

File f = new File(path1 + File.separator + folderName
+ File.separator + "request");
f.mkdirs();
out1 = new PrintWriter(new BufferedWriter(new FileWriter(
f.getPath() + File.separator + tripID, true)));
out1.println(requestStr);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != out1)
out1.close();
}

}

public static void saveJSONToFile2(String requestStr, String tripID,
String reqRes, String supplier, String folderName) {
PrintWriter out1 = null;
try {
String path1 = "D://beebeesotware";

File f = new File(path1 + File.separator + folderName
+ File.separator + "request");
f.mkdirs();
out1 = new PrintWriter(new BufferedWriter(new FileWriter(
f.getPath() + File.separator + tripID, true)));
out1.println(requestStr);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != out1)
out1.close();
}

}
}

August 11, 2018

Serializing POJO to JSON - Input parameterization

How to output a json like below using java program



{"id":"0001","name":"Cake","topping":[{"id":"5001","type":"None"},{"id":"5004","type":"Maple"}],"ppu":"0.55","type":"donut","batters":{"batter":[{"id":"1004","type":"Devil's Food"},{"id":"0001","type":"Regular"}]}}

Program

/**
 * 
 */
package com.belazy.pojostructure;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;


/**
 * @author consumerfed IT Section 
 * Kozhikode
 *
 */
public class ManiClass {

public static void main(String[] args) throws JsonProcessingException {
Javabelazy user = new Javabelazy();
user.setId("0001");
    user.setType("donut");
    user.setName("Cake");
    user.setPpu("0.55");
    Batter batter1 = new Batter();
    batter1.setId("0001");
    batter1.setType("Regular");
    Batter batter2 = new Batter();
    batter2.setId("1004");
    batter2.setType("Devil's Food");
    Batters batters = new Batters();
    Batter[] batter = new Batter[2];
    batter[1] = batter1;
    batter[0] = batter2;
batters.setBatter(batter );
    user.setBatters(batters); 
    Topping topping1 = new Topping();
    topping1.setId("5001");
    topping1.setType("None");
    Topping topping2 = new Topping();
    topping2.setId("5004");
    topping2.setType("Maple");
    Topping[] topping = new Topping[2];
    topping[0] = topping1;
    topping[1] = topping2;
user.setTopping(topping );
ObjectMapper objectmapper = new ObjectMapper();
    String json = objectmapper.writeValueAsString(user);
    System.out.println(json);
}

}


User class

package com.belazy.pojostructure;
public class Javabelazy
{
    private String id;

    private String name;

    private Topping[] topping;

    private String ppu;

    private String type;

    private Batters batters;

    public String getId ()
    {
        return id;
    }

    public void setId (String id)
    {
        this.id = id;
    }

    public String getName ()
    {
        return name;
    }

    public void setName (String name)
    {
        this.name = name;
    }

    public Topping[] getTopping ()
    {
        return topping;
    }

    public void setTopping (Topping[] topping)
    {
        this.topping = topping;
    }

    public String getPpu ()
    {
        return ppu;
    }

    public void setPpu (String ppu)
    {
        this.ppu = ppu;
    }

    public String getType ()
    {
        return type;
    }

    public void setType (String type)
    {
        this.type = type;
    }

    public Batters getBatters ()
    {
        return batters;
    }

    public void setBatters (Batters batters)
    {
        this.batters = batters;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [id = "+id+", name = "+name+", topping = "+topping+", ppu = "+ppu+", type = "+type+", batters = "+batters+"]";
    }
}

Topping class

package com.belazy.pojostructure;
public class Topping
{
    private String id;

    private String type;

    public String getId ()
    {
        return id;
    }

    public void setId (String id)
    {
        this.id = id;
    }

    public String getType ()
    {
        return type;
    }

    public void setType (String type)
    {
        this.type = type;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [id = "+id+", type = "+type+"]";
    }
}


Batters class
package com.belazy.pojostructure;
public class Batters
{
    private Batter[] batter;

    public Batter[] getBatter ()
    {
        return batter;
    }

    public void setBatter (Batter[] batter)
    {
        this.batter = batter;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [batter = "+batter+"]";
    }
}

Batter class

package com.belazy.pojostructure;
public class Batter
{
    private String id;

    private String type;

    public String getId ()
    {
        return id;
    }

    public void setId (String id)
    {
        this.id = id;
    }

    public String getType ()
    {
        return type;
    }

    public void setType (String type)
    {
        this.type = type;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [id = "+id+", type = "+type+"]";
    }
}


Output



August 03, 2018

How to use xml x-www-form-urlencoded and logging in okhttp

How to use xml x-www-form-urlencoded and logging in okhttp

/**
* Consumerfed IT Section Kozhikode
*/

private String okhttpPost(String xmlRequest) {

String responseStr = null;
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(Level.BASIC);
OkHttpClient client = new OkHttpClient().newBuilder().addInterceptor(logging).connectTimeout(60, TimeUnit.SECONDS).writeTimeout(60, TimeUnit.SECONDS).readTimeout(60, TimeUnit.SECONDS).build();
RequestBody formBody = new FormBody.Builder().add("XML", xmlRequest).build();
Request request = new Request.Builder().url("https://consumerfed/itsection/findData").post(formBody).addHeader("Content-Type", "application/x-www-form-urlencoded")
.addHeader("Connection", "Keep-Alive").addHeader("Accept-Language", "en-US").addHeader("Accept-Encoding", "application/gzip").build();
try {Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
responseStr = response.body().string();}} catch (IOException exception) {
exception.printStackTrace();}
return responseStr;
}

InterruptedIOException



java.io.InterruptedIOException: thread interrupted
at okio.Timeout.throwIfReached(Timeout.java:145)
at okio.Okio$1.write(Okio.java:75


okhttp will check whether current running is interrupted. so check the main thread, why the main thread is interrupted , may be you have to increase the waiting time of that thread

Facebook comments