• Skip to main content
  • Skip to primary sidebar

CodeBlogMoney

Make Money using Coding and Blogging

Create a JSON File : 3 easy ways

June 21, 2020 by Jimmy

How to create a JSON File?

How to create JSON file. This article helps to create valid JSON document File.
Create a valid JSON file

To create JSON file first we need to understand what is JSON and what it represents. Please check out this link to find more about the JSON What is JSON?

Once we understand JSON, there are 3 ways to create a new JSON.

  1. Using Text Editor
  2. Using Online Tools such as https://jsonformatter.org
  3. Create a file from the JSON URL

1. Using Text Editor

Open a Text editor like Notepad, Visual Studio Code, or Sublime or your favorite one.

Cope and Paste below JSON data in Text Editor or create your own base on the What is JSON article.

{
  "InsuranceCompanies": {
    "Top Insurance Companies": [
      {
        "No": "1",
        "Name": "Berkshire Hathaway ( BRK.A)",
        "Market Capitalization": "$308 billion"
      }
    ],
    "source": "investopedia.com",
    "Time": "Feb 2019"
  }
}

Use this JSON validator tool to validate the JSON

https://codebeautify.org/jsonvalidator

Once file data are validated, save the file with the extension of .json, and now you know to create the Valid JSON document.

2. Using Online Tool

Open a JSON Formatter tool from the link below

https://jsonformatter.org/

Copy and Paste the JSON Data which is mention in Option 1 in the Input tool of the online tool

Jsonformatter.org helps to create , validate and format the JSON Data
Click on Download icon Highlighted in Red from the right textarea

Download this file using the download icon mentioned on the Right input area. It will download the JSON file.

This jsonformatter.org already support the JSON validation. This will create a New JSON file and will allow downloading.

Now if you already have JSON document or file, you can upload it to this tool and modify or delete few objects, array of the JSON and download the updated and valid JSON file

3. create a file from the JSON URL

Developer needs to work with API and nowadays 95% API returns data as JSON.

Here are few samples of the JSON URL. Now click on these JSON samples.

  1. https://gist.githsubusercontent.com/jimmibond/9205480889e19c0de347/raw/sample.json
  2. https://gist.githubusercontent.com/cbmgit/852c2702d4342e7811c95f8ffc2f017f/raw/InsuranceCompanies.json

Once they are open use save as functionality of the browser and save these file.

I hope this article helps you to understand basic ways to create JSON file a Valid and formatter.

Filed Under: JSON

Byte Array to String in Java

June 10, 2020 by Jimmy

This article explain how to convert byte array to String.

Byte Array to String code

public class ByteArrayToString {
	
	public static void main(String[] args)
    { 
		byte[] byteArray= {67,111,100,101,32,
				           66,108,111,103,32,
				           77,111,110,101,121};
		
		//Convert byte[] to String		
	    String convertedToString = new String(byteArray);  
	    
	    System.out.println("Byte Array to String : " + convertedToString);
    }
}

Output

Byte Array to String : Code Blog Money

String to Byte Array

public class StringtoByteArray {
	
	public static void main(String[] args)
    { 
		String sampleString = "CodeBlogMoney";
		
		byte[] byteArray = sampleString.getBytes();
		
		System.out.println("Byte Array data :\n");
		
		for(byte b : byteArray) {
			System.out.print(b + " ");
		}
    }
}

Output

Byte Array data :

67 111 100 101 66 108 111 103 77 111 110 101 121 

Filed Under: Java

Iterate Java List using Loop, forEach, Iterator and more

June 9, 2020 by Jimmy

While programming array is essential to store list of fruits, list of employee, list of states and many more. Array has limitation and slow to travers through when the list of items are getting bigger and dynamically incresing.

AraryList in Java supports to store N numbers of items. After storing those items in the list, it needs iteration to find item which are available in the Array. We need to loop it one by one to fetch the required item.

Here are ways to Iterate or Loop List in Java.

1. For Loop

This is a basic for loop which we learn in Programming 101.

import java.util.ArrayList;
import java.util.List;

public class JavaLoopList {

	public static void main(String[] args) {

		List<String> actorList = new ArrayList<String>();

		actorList.add("Robert Downey, Jr.");
		actorList.add("Tom Hanks");
		actorList.add("Leonardo DiCaprio");
		actorList.add("Will Smith");
		actorList.add("Tom Cruise");
		actorList.add("Dwayne Johnson");

		for ( int i = 0; i < actorList.size(); i++ ) {
			System.out.println( "Actron Name:-> " + actorList.get(i) );
		}
	}
}

Output

--Using Foor Loop--

Actron Name:-> Robert Downey, Jr.
Actron Name:-> Tom Hanks
Actron Name:-> Leonardo DiCaprio
Actron Name:-> Will Smith
Actron Name:-> Tom Cruise
Actron Name:-> Dwayne Johnson

This code uses for loop which uses size() to get the available items in the List and going one by one to get the items from the list and print on the console.

2. Next Generation For Loop

import java.util.ArrayList;
import java.util.List;

public class NextGenerationForLoop {

	public static void main(String[] args) {

		List<String> actorList = new ArrayList<String>();

		actorList.add("Robert Downey, Jr.");
		actorList.add("Tom Hanks");
		actorList.add("Leonardo DiCaprio");
		actorList.add("Will Smith");
		actorList.add("Tom Cruise");
		actorList.add("Dwayne Johnson");

		for ( String actorName : actorList ) {
			
			System.out.println( "Actron Name:-> " + actorName );
			
		}
	}
}

Output

--Using Next Generation Foor Loop--

Actron Name:-> Robert Downey, Jr.
Actron Name:-> Tom Hanks
Actron Name:-> Leonardo DiCaprio
Actron Name:-> Will Smith
Actron Name:-> Tom Cruise
Actron Name:-> Dwayne Johnson

3. While loop: Programming 101

import java.util.ArrayList;
import java.util.List;

public class JavaWhileLoopList {

	public static void main(String[] args) {

		List<String> actorList = new ArrayList<String>();

		actorList.add("Robert Downey, Jr.");
		actorList.add("Tom Hanks");
		actorList.add("Leonardo DiCaprio");
		actorList.add("Will Smith");
		actorList.add("Tom Cruise");
		actorList.add("Dwayne Johnson");

		System.out.println("--Using While Loop--\n");

		int index = 0;
		
		while (index < actorList.size()) {
			
			System.out.println("Name:-> "+actorList.get(index));
			index++;
		}
	}
}

Output

--Using While Loop--

Name:-> Robert Downey, Jr.
Name:-> Tom Hanks
Name:-> Leonardo DiCaprio
Name:-> Will Smith
Name:-> Tom Cruise
Name:-> Dwayne Johnson

4. Iterator with While Loop

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class JavaIteratorWhileLoopList {

	public static void main(String[] args) {

		List<String> actorList = new ArrayList<String>();

		actorList.add("Robert Downey, Jr.");
		actorList.add("Tom Hanks");
		actorList.add("Leonardo DiCaprio");
		actorList.add("Will Smith");
		actorList.add("Tom Cruise");
		actorList.add("Dwayne Johnson");

		System.out.println("--Using Iterator with While Loop--\n");

		Iterator<String> actoreIterator = actorList.iterator();
		
		while (actoreIterator.hasNext()) {
			
			System.out.println("Name:-> "+actoreIterator.next());
		}
	}
}

Output

--Using Iterator with While Loop--

Name:-> Robert Downey, Jr.
Name:-> Tom Hanks
Name:-> Leonardo DiCaprio
Name:-> Will Smith
Name:-> Tom Cruise
Name:-> Dwayne Johnson

5. ListIterator with While loop

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class JavaListIteratorWhileLoopList {

	public static void main(String[] args) {

		List<String> actorList = new ArrayList<String>();

		actorList.add("Robert Downey, Jr.");
		actorList.add("Tom Hanks");
		actorList.add("Leonardo DiCaprio");
		actorList.add("Will Smith");
		actorList.add("Tom Cruise");
		actorList.add("Dwayne Johnson");

		System.out.println("--Using ListIterator with While Loop--\n");

		ListIterator<String> actoreIterator = actorList.listIterator();
		
		while (actoreIterator.hasNext()) {
			
			System.out.println("Name:-> "+actoreIterator.next());
		}
	}
}

Output

--Using ListIterator with While Loop--

Name:-> Robert Downey, Jr.
Name:-> Tom Hanks
Name:-> Leonardo DiCaprio
Name:-> Will Smith
Name:-> Tom Cruise
Name:-> Dwayne Johnson

6 Iterable.forEach()

import java.util.ArrayList;
import java.util.List;

public class JavaListIteratorForEachList {

	public static void main(String[] args) {

		List<String> actorList = new ArrayList<String>();

		actorList.add("Robert Downey, Jr.");
		actorList.add("Tom Hanks");
		actorList.add("Leonardo DiCaprio");
		actorList.add("Will Smith");
		actorList.add("Tom Cruise");
		actorList.add("Dwayne Johnson");

		System.out.println("-- Using Iterable.forEach() --\n");

		actorList.forEach((actorName) -> {
            System.out.println("Actor Name:-> "+actorName);
        });
		
	}
}

Output

-- Using Iterable.forEach() --

Actor Name:-> Robert Downey, Jr.
Actor Name:-> Tom Hanks
Actor Name:-> Leonardo DiCaprio
Actor Name:-> Will Smith
Actor Name:-> Tom Cruise
Actor Name:-> Dwayne Johnson

7 Stream.forEach

import java.util.ArrayList;
import java.util.List;

public class JavaStreamForEachList {

	public static void main(String[] args) {

		List<String> actorList = new ArrayList<String>();

		actorList.add("Robert Downey, Jr.");
		actorList.add("Tom Hanks");
		actorList.add("Leonardo DiCaprio");
		actorList.add("Will Smith");
		actorList.add("Tom Cruise");
		actorList.add("Dwayne Johnson");

		System.out.println("-- Using Stream.forEach() --\n");

		actorList.stream().forEach(
				(actrorName) -> System.out.println("Name:->" + actrorName ) );
		
	}
}

Output

-- Using Stream.forEach() --

Name:->Robert Downey, Jr.
Name:->Tom Hanks
Name:->Leonardo DiCaprio
Name:->Will Smith
Name:->Tom Cruise
Name:->Dwayne Johnson

Filed Under: Java

HTML Space: 3 easy ways

June 9, 2020 by Jimmy

HTML Space : Create HTML Blank Space using &nbsp

The more space in HTML page, it looks better. Just check this CodeBlogMoney site, more than 70% is space. HTML Space will help to make the document more attractive and readable.

HTML Space makes the site looks better. While learning the first time everyone uses space button to generate more space, and when we run in the browser it still was not reflected.

Do you remember those days? or you are here because it’s not reflected on your page? 🙂 use <pre> tag to use keyboard space. explained in section 3.

HTML Space, 3 ways to make it happen in HTML Page.

1. &nbsp; in HTML

&nbsp; creates extra space after and before any text in HTML.

;nbsp html
<html>
	<head>
		<title>Create Space in HTML</title>
	</header>
		<body>
			How are you &nbsp;&nbsp; doing?
		</body>
</html>	

2. Padding

HTML uses concepts of Margin, border and padding to create more space around the element or object of the HTML.

It can achieve the style attribute of the HTML.

In this image at the right side shows the information about the margin, border and padding of an element of HTML. Like this element has padding at the top, left, right and at the bottom.

<html>
	<head>
		<title>Create Space using Padding</title>
	</header>
		<body>
			<div style="padding-top:50px;padding-left:50px">
				How are you doing?
			<div>
		</body>
</html>	

3. <pre>

<pre> means the Preformatted text. It preserve the space in the HTML document which are under the <pre> tag. easiest way to add space.

<html>
	<head>
		<title>Create Space using Padding</title>
	</header>
		<body>
			<pre>
				How    are   you   doing?
			<pre>
		</body>
</html>	

HTML tool to try HTML code.

https://codebeautify.org/htmlviewer/

More articles to read:

https://codeblogmoney.com/how-to-print-xml/

Filed Under: HTML

Java get Hostname

June 8, 2020 by Jimmy

Java get Hostname

This blog helps you to get the hostname and IP address of where the program is running. It uses the InetAddress class of Java which uses the Internet Protocol address.

While development and testing most developer uses own computer or laptop to run the program. so the program will provide the hostname of the system where this code is executed.

This code uses java.net library of java to get the Hostname.

Here is the code which get hostname using Java.

package com.cbm.hostnamejava;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class JavaGetHostName {
	
public static void main(String[] args) throws UnknownHostException{
		 
       InetAddress inetAddress = InetAddress.getLocalHost();
  
       String hostname = inetAddress.getHostName();
          
       System.out.println("Hostname of current host: " + hostname);
            
       System.out.println("IP address of current host: " + inetAddress);
 
    }

}

Output:

Hostname of current host: DESKTOP-TTSBKDU
IP address of current host: DESKTOP-TTSBKDU/192.168.0.110

InetAddress getLocalHost()

getLocalHost() of InetAddress return the local host It get the name from the system and assigned to InetAddress.

This method throws UnknownHostException incase if system is not able to retrieve the hostname or not able to associate with IP address.

Code with Exception Handling

import java.net.InetAddress;
import java.net.UnknownHostException;

public class JavaGetHostName {

	public static void main(String[] args) {

		try {

			InetAddress inetAddress = InetAddress.getLocalHost();

			String hostname = inetAddress.getHostName();

			System.out.println("Hostname of current host: " + hostname);

			System.out.println("IP address of current host: " + inetAddress);
			
		} catch (UnknownHostException exception) {

			System.out.println("Exception:->" + exception.getMessage());
		}

	}

}

More article to read:

String to Char Array Java

Useful tool to https://codebeautify.org/hostname-to-ip

Filed Under: Java

  • Go to page 1
  • Go to page 2
  • Go to page 3
  • Interim pages omitted …
  • Go to page 6
  • Go to Next Page »

Primary Sidebar

Categories

  • Blogging
  • HTML
  • Java
  • JavaScript
  • jQuery
  • JSON
  • MySql
  • Performance
  • PHP
  • Problem Solving
  • Python
  • Testing
  • XML

Copyright © 2021 · Metro Pro on Genesis Framework · WordPress · Log in