Write a java program which reads a string from the user and divides the string into different classes such as Strings, Numbers and AlphanumericStrings

In this article I will solve the problem “Write a program which reads a string from the user and divides the string into different classes such as Strings, Numbers and AlphanumericStrings”.

Functions used in this program is as following with explanation of working of function.

public String[] getAllStrings(String str1) :-The method getAllStrings takes the string str1 as input and returns all the strings which are having only characters as a string array.

public String[] getAllNumerics(String str1) :-The method getAllNumerics takes the string str1 as input and returns all the strings which are having both combination of alphabets and numerals as a string array.

public String[] getAllAlphanumericStrings(String str1) :-The method getAllNumerics takes the string str1 as input and returns all the strings which are having only numerical as a string array.

Coding:

public class StringDivisionProgram
{
public String[] getAllStrings(String str1)
{
char[] str = str1.toCharArray();
char[] ch = new char[20];
char[] arr = new char[50];
int k = 0, count = 0, i;
try {
for (i = 0; i < str.length;i++)
{
while (str[i] == ' ')
{
i++;
}
if (i == str.length)
break;
for (; str[i] != ' '; i++)
 {
ch[k++] = str[i];
if (i+1 == str.length)
break;
}
char[] temp = new char[k];
int p = 0;
for (int x = 0; x < temp.length; x++)
temp[x] = ch[x];
if (checkString(temp, k) == 1)
{
for (; p < temp.length; p++)
{
arr[count++] = temp[p];
}
}
if (i+1 != str.length && p > 0)
{
arr[count] = ',';
 count++;
}
if (i++1 == str.length)
break;
k = 0;
}
}
catch (Throwable e)
{
e.printStackTrace();
}
String fin = "";
String[] res = new String[50];
k = 0;
for (int j = 0; j < count; j++)
{
if (arr[j] == ',' || j   1 == count)
{
if (arr[j] != ',') { fin  = arr[j];
}
res[k++] = fin; fin = "";
}
else fin  = arr[j];
}
String[] res1 = new String[k];
System.arraycopy(res, 0, res1, 0, k);
 return res1;
}
public String[] getAllNumerics(String str1)
{
char[] str = str1.toCharArray();
char[] ch = new char[20];
char[] arr = new char[50];
int k = 0, count = 0, i;
try
{
for (i = 0; i < str.length;)
{
while (str[i] == ' ')
{
i++;
}
if (i == str.length)
break;
for (; str[i] != ' '; i++)
{ ch[k++] = str[i];
if (i+1 == str.length)
break;
}
char[] temp = new char[k];
int p = 0;
for (int x = 0; x < temp.length; x++)
temp[x] = ch[x];
if (checkString(temp, k) == 2)
{ for (; p < temp.length; p++)
{
 arr[count  ] = temp[p];
}
}
if (i   1 != str.length && p > 0)
{
arr[count] = ',';
 count++;
}
if (i   1 == str.length)
break; k = 0;
}
}
catch (Throwable e) { e.printStackTrace();
}
String fin = "";
String[] res = new String[50];
k = 0;
for (int j = 0; j < count; j++)
{
if (arr[j] == ',' || j   1 == count)
{
if (arr[j] != ',')
{
fin  = arr[j];
}
res[k++] = fin;
fin = "";
}
else fin  = arr[j];
}
String[] res1 = new String[k];
//Array.Copy(res,0,res1,0,k);
System.arraycopy(res, 0, res1, 0, k);
return res1;
}
public String[] getAllAlphanumericStrings(String str1)
{
char[] str = str1.toCharArray();
char[] ch = new char[20];
char[] arr = new char[50];
int k = 0, count = 0, i;
try
{
for (i = 0; i < str.length;)
{
while (str[i] == ' ')
{ i++;
}
if (i == str.length) break;
for (; str[i] != ' '; i++)
{
ch[k  ] = str[i];
if (i   1 == str.length)
break;
}
char[] temp = new char[k];
int p = 0;
for (int x = 0; x < temp.length; x++)
temp[x] = ch[x];
if (checkString(temp, k) == 3)
{ for (p = 0; p < temp.length; p++)
{ arr[count  ] = temp[p];
}
}
if (i   1 != str.length && p > 0)
{ arr[count] = ','; count++;
}
if (i   1 == str.length)
break;
k = 0;
}
}
catch (Throwable e)
{
e.printStackTrace();
}
String fin = "";
String[] res = new String[50];
k = 0;
for (int j = 0; j < count; j++)
{
if (arr[j] == ',' || j   1 == count)
{
if (arr[j] != ',') { fin  = arr[j];
}
res[k  ] = fin; fin = "";
}
else fin  = arr[j];
}
String[] res1 = new String[k];
//Array.Copy(res,0,res1,0,k);
System.arraycopy(res, 0, res1, 0, k);
return res1;
}
int checkString(char[] str, int k)
{
int flag1 = 0, flag2 = 0;
for (int i = 0; i < str.length; i++)
{
if (str[i] >= 48 && str[i] <= 57)
flag1 = 1;
else if ((str[i] >= 65 && str[i] <= 90) ||
(str[i] >= 97 && str[i] <= 122)) flag2= 1;
}
if (flag2 == 1 && flag1 == 0)
return 1;
 if (flag2 == 0 && flag1 == 1)
return 2;
if (flag2 == 1 && flag1 == 1)
 return 3;
return 1;
}
public static void main(String[] args)
{
System.out.println(new
StringDivisionProgram().getAllNumerics
("h3llo this 1s 4 input string 001"));
System.out.println(new
StringDivisionProgram().getAllStrings
("h3llo this 1s 4 input string 001"));
System.out.println(new
StringDivisionProgram).getAllAlphanumericStrings
("h3llo this 1s 4 input string 001"));
}
}

Example:
Input String:-
str1 = “h3llo this 1s 4 input string 001”
Output:-
getAllStrings Returns : {“this”, “input”, “string”};
getAllAlphanumericStrings Returns : {“h3llo”, “1s”};
getAllNumerics Returns : {“4”, “001”};