Friday, 19 February 2016

Arithmetic Operators in Java - Live practical examples : Part 1

In today's post we'll cover the topic of arithmetic operators in Java with some real live examples. In this post, we'll skip the theory and go straightaway for some practical examples. With these examples, you will get to grasp and understand the operator functions much quicker than ever.

Check out the following code in which we have used multiple combinations of arithmetic operators in different positions.

package com.ironcladzone;

public class operator1 {

public static void main(String[] args) {
System.out.println("Example 1 :" + 3 + 4 + 5);
System.out.println("Example 2 : " + 3 + 4 * 5);
System.out.println("Example 3 : " + 3 + 12 * 10);
System.out.println("Example 4 : " + 3 * 4 + 5);
System.out.println("Example 5 : " + 3 * 4 / 5);
System.out.println("Example 6 : " + 5 / 4 * 5);
System.out.println("Example 7 : " + 10 / 4 + 5);
System.out.println("Example 8 : " + 10 + 4 / 5);
System.out.println("Example 9 : " + 10 + 15 / 5);
System.out.println("Example 10 : " + 125 / 10 / 5);
System.out.println("Example 11 : " + 50 / 10 / 5);
System.out.println("Example 12 : " + 23 / 10 / 5);
System.out.println("Example 13 : " + 23 / 10 / 5 + 4);
System.out.println("Example 14 : " + 3 + 4 + 5 / 7);
System.out.println("Example 15 : " + 3 + 4 + 5 * 2);
System.out.println("Example 16 : " + 3 + 5 / 5 * 2);
System.out.println("Example 17 : " + 3 + 22 / 5 * 2);
System.out.println("Example 18 : " + 3 + 7 / 5 * 2);
System.out.println("Example 19 : " + 2 + 8 / 5 * 2);
System.out.println("Example 20 : " + 3 * 9 / 5 + 2.59);

}


}

Output : 

Example 1 :345
Example 2 : 320
Example 3 : 3120
Example 4 : 125
Example 5 : 2
Example 6 : 5
Example 7 : 25
Example 8 : 100
Example 9 : 103
Example 10 : 2
Example 11 : 1
Example 12 : 0
Example 13 : 04
Example 14 : 340
Example 15 : 3410
Example 16 : 32
Example 17 : 38
Example 18 : 32
Example 19 : 22
Example 20 : 52.59

If you observe closely, you may find a few of the combinations are tricky. For instance, note that the + sign we have used, is for string concatenation and not to be confused with the plus sign used for addition.

This was just the first set of live examples. Another exhaustive post on more examples is on its way. Stay tuned folks. Am also quite sure this code will help you cover the basic operator concepts if you're planning to give the Oracle Java Certification 1Z0-803. Good luck guys. Signing out.

Tuesday, 16 February 2016

Add a character at a specific location within strings using Awk

Folks, in previous examples we saw how to add a prefix to strings in a file and how to add a suffix to strings in a file. In today's example, we'll see how to add a character somewhere in the middle of multiple strings in a file at the same time.

We'll use the same data as used in previous examples. Let's use a text file named Phones_unformatted.txt with the following phone numbers data :

2127841212
3458922345
7713398403
6461228847

Now we want to add a - sign after the first 3 digits to separate the area code. Notice that all phone numbers are of 10 digits. We want to add a - sign between the 3rd and 4th character.

For doing this lets use awk again :

awk '$0=substr($0,1,3) "-" substr($0,4,7)' Phones_unformatted.txt

Output :

212-7841212
345-8922345
771-3398403
646-1228847


Notice how we have used substr() function to split the entire 10 digit phone number into 2 parts. The first part is between 1-3rd character and the second part is from 4-10th character.

Similarly, lets say for e.g. we want to add the - sign twice within the string i.e once between 3rd and 4th character and the next - between 6th and 7th character. The awk code for doing this is as follows :

awk '$0=substr($0,1,3) "-" substr($0,4,3) "-" substr($0,7,4)' Phones_unformatted.txt

Output :

212-784-1212
345-892-2345
771-339-8403
646-122-8847

This way, you could append the string with the "-" character or anything like a dot "." or maybe just a whitespace " ".

awk '$0=substr($0,1,3) " " substr($0,4,3) " " substr($0,7,4)' Phones_unformatted.txt

Output :

212 784 1212
345 892 2345
771 339 8403
646 122 8847

List all baselines of a stream in IBM RTC

For listing out all baselines of a stream using the Rational Team Concert command line interface, type the following command.

Usage :

lscm list baselines -r repository-name -w stream-name

Eg :

lscm list baselines -r https://jtsrv.uk.hibm.company:9094/jazz -w htse.mydocs.inc.midata.release

Notice we haven't used any double quotes for any parameter in the syntax for this command.

Sunday, 14 February 2016

Kala Ghoda Festival 2016, Mumbai : PhotoStream

Hello fellas! Presenting an artsy post today featuring the Kala Ghoda arts festival 2016, Mumbai. FYI, Kala Ghoda literally means Black Horse.

Check out the raw unedited PhotoStream here. (Pardon the photo quality since its taken with a cheap phone camera)

The festival which is held every here in the Kala Ghoda area of Fort neighborhood, Mumbai. It features a variety of entertainment programs like :
  • Street art fest featuring unique sculptures and installations.
  • Poetry and book reading sessions
  • Street plays and dramas
  • Music shows
  • Workshops
  • Theatre shows
  • Food and cuisine tasting sessions and much much more...
The official schedule can be found here. Also do check this map for a quick location guide. Make sure you visit it since 14th Feb is the last closing day, in case you missed it.

Print File size using Java

If you ever want to print the file size using Java, use the following piece of code below :

import java.io.*;

public class FileSize {

public static void main(String[] args) {
File f = new File("//Users//ironcladzone//Downloads//image1.jpg");
System.out.println("File size in bytes = " +f.length());
System.out.println("\nFile size in kb = " +f.length()/1024);

}


}

Output :

File size in bytes = 241833

File size in kb = 236

Note that when we use the length() function, it gives us the file size in bytes. For getting output in kilobytes, divide it by 1024. If you want size in megabytes, again divide it by another 1024. So on and so forth...

Add suffix to strings in a file using Awk

Guys, in my previous post yesterday, we discussed how to add a prefix to strings in a file using Awk. Today lets work on the same example to add a suffix to the strings i.e append the strings with characters in the end.

We'll use the same set of phone numbers for formatting :

2127841212
3458922345
7713398403
6461228847

Let's add some characters to the these numbers in the end. How about something like " Extn : 7142". Here's the Awk code to do it below :

awk '{print $0 " Extn : 7142"}' Phones_unformatted.txt

Output :

2127841212 Extn : 7142
3458922345 Extn : 7142
7713398403 Extn : 7142

6461228847 Extn : 7142

Saturday, 13 February 2016

Add a prefix to strings in a file using Awk

Suppose you have a file in Unix / Mac OS X which has phone numbers data. I mean lets say I have a text file named Phones_unformatted.txt which has some phone numbers as follows :

2127841212
3458922345
7713398403
6461228847

Now I want to format the phone numbers to have a prefix maybe like a country code +44- . something like :

+44-2127841212
+44-3458922345
+44-7713398403
+44-6461228847

If you observe I need to only append the phone number with some characters in the beginning. We can achieve the same using awk.

A simple awk code will do the trick as follows :

awk '{print "+44-" $0}' Phones_unformatted.txt

Output :

+44-2127841212
+44-3458922345
+44-7713398403
+44-6461228847

Viola!
Related Posts Plugin for WordPress, Blogger...
eXTReMe Tracker