Sunday, January 27, 2013

Printing a string integer in comma separated format

Input: 12345667800000
Output: 12,34,56,78,00,000

// PROGRAM TO PRINT COMMA SEPARATED INTEGERS

import java.lang.*;

public class Program
{
    public static void main(String args[])
    {
        System.out.println("Hello world! From Compilr.com");
        String str = "1234567800000";
        System.out.println(getStr(str));
     
    }
    public static String getStr(String str1)
    {
      if(str1.length()  <=3)
        {
            return str1;
        }
        else
        {
            System.out.println(str1.length());
            if(str1.length() % 2 == 1)
            {
                return str1.substring(0,2) + "," + getStr(str1.substring(2,str1.length()));
            }
            else
            {
                return str1.substring(0,1) + "," + getStr(str1.substring(1,str1.length()));      
            }
        }
    }
}

Permutation of possible string using a phone keypad, Java

Print all possible combinations of letters that a particular phone number could represent.
<pre class="brush:java">

import java.util.*;

public class permutation {
public static String[] mapping1[] = {{"0"},{"1"},{"A","B","C"},{"D","E","F"},{"G","H","I"},{"J","K","L"},{"M","N","O"},
{"P","Q","R","S"},{"T","U","V"},{"W","X","Y","Z"}};

public List<String> GetPermutation(String phoneNumber)
{
List<String> combo = new ArrayList<String>();
GeneratePermutation(combo, "", phoneNumber);
return combo;
}
public static void main(String[] args)
{
String phonenumber = "3456789";
permutation abc = new permutation();
List<String> comboList = abc.GetPermutation(phonenumber);

for(String permutation: comboList)
{
System.out.println(permutation);
}
}

public void GeneratePermutation(List<String> combo, String prefix, String endPhone){
Integer currentInt = Integer.parseInt(endPhone.substring(0,1));

if(endPhone.length() == 1)
{
// Reached string end add all possible combination
for(int temp = 0; temp < mapping1[currentInt].length; temp++)
{
combo.add(prefix + mapping1[currentInt][temp]);
}
}
else
{
for(int temp = 0; temp < mapping1[currentInt].length; temp++)
{
GeneratePermutation(combo, prefix + mapping1[currentInt][temp], endPhone.substring(1));
}
}
}
}
</pre>

Major intention of this post is to test the new script in blogger which can display code block. But it does not seems to work. Will update it soon.

Credits: http://stackoverflow.com/questions/2344496/how-can-i-print-out-all-possible-letter-combinations-a-given-phone-number-can-re

Multidimensional arrays, Java

You must have used multidimensional arrays in whichever programming languages you use. Here is a brief about different ways of declaring and accessing multi-dimensional arrays in java.

Say you want to represent the following data in 2-D array
0
1
A B C
D E F
G H I
J K L
M N O
P Q R S
T U V
W X Y Z

which in fact represent the keys on your phone.

You can declare such mapping as:

public static String[] mapping1[] =
{  {"0"},
    {"1"},
    {"A","B","C"},
    {"D","E","F"},
    {"G","H","I"},
    {"J","K","L"},
    {"M","N","O"},
    {"P","Q","R","S"},
    {"T","U","V"},
    {"W","X","Y","Z"}
};

public static String mapping2[][] =

{  {"0"},
    {"1"},
    {"A","B","C"},
    {"D","E","F"},
    {"G","H","I"},
    {"J","K","L"},
    {"M","N","O"},
    {"P","Q","R","S"},
    {"T","U","V"},
    {"W","X","Y","Z"}
};

public static String[][] mapping3 =

{  {"0"},
    {"1"},
    {"A","B","C"},
    {"D","E","F"},
    {"G","H","I"},
    {"J","K","L"},
    {"M","N","O"},
    {"P","Q","R","S"},
    {"T","U","V"},
    {"W","X","Y","Z"}
};

Note the difference in declaration but all these represent the same data (or same 2D array).
If you do:

System.out.println(Arrays.deepToString(mapping1));
System.out.println(Arrays.deepToString(mapping2));
System.out.println(Arrays.deepToString(mapping3));

then output will be same for all three wiz:
[[0], [1], [A, B, C], [D, E, F], [G, H, I], [J, K, L], [M, N, O], [P, Q, R, S], [T, U, V], [W, X, Y, Z]]

Also one more point worth noticing here is in order to access data there will be same accessor i.e.
if you want to access element 'B' (whose position in 2 D array is [2][1]) then accessor for three mappings will be same wiz
System.out.println(mapping1[2][1]);
System.out.println(mapping2[2][1]);
System.out.println(mapping3[2][1]);

Output: B

Source: http://stackoverflow.com/questions/1067073/java-multidimensional-array/1067087#1067087

Friday, January 18, 2013

Idea: Phone to TV over Wifi

Wouldn't it be great if you can see your phone screen on big tv just by using a simple app on your phone? I searched for such an app but there is n't any as such. You can browse your phone on your machine, see all its content but what if you want to show the world what you are watching on your phone either on your laptop screen or say projector connected to your laptop.

This is very much possible using an HDMI cable but how about doing this wireless - over wifi. I have seen such stuff in manufacturer's keynote (like on new android release) where they show off cool features of new mobile OS on projector. But I don't have much idea what they use to do that.

As a novice, I searched a bit on google but couldn't figure out a way to connect. Will surely work on building such app as soon as got time =). It will involve cross-platform implementation so first need to look for available alternatives.

Just realized this is my first post of 2013. Happy New Year.