A very good link:
http://www.connectionstrings.com/
Tuesday, 31 July 2012
Friday, 27 July 2012
Converting char to String in Java
public class ConvertCharToString { public static void main(String[] args) {
String name = "Sardar";
//get the first character from the string
char firstChar = name[0];
// convert the character to string again
String temp = Character.toString(firstChar);
// check if the char is converted correctly
if ( "S".equals(temp) )
{
System.out.println("String Matched");
}
}
}
Friday, 13 July 2012
Count and Display non empty lines in a file Java
Count and Display nun empty lines in a file Java
public class FileLineCounter
{
public static int countAndDisplayNonEmptyLines(String path) throws IOException
{
int lineCount = 0;
InputStream is = null;
try
{
is = new FileInputStream(path);
InputStreamReader in = new InputStreamReader(is);
BufferedReader _input = new BufferedReader(in);
String line = _input.readLine();
while ( line != null )
{
if ( !line.trim().isEmpty() )
{
System.out.println(lineCount + ". " + line);
lineCount++;
}
line = _input.readLine();
}
}
finally
{
if ( is != null )
is.close();
}
return lineCount;
}
public static void main(String args[])
{
try
{
countAndDisplayNonEmptyLines("C:\\Users\\Desktop\\help.log");
}
catch ( IOException e )
{
e.printStackTrace();
}
}
}
public class FileLineCounter
{
public static int countAndDisplayNonEmptyLines(String path) throws IOException
{
int lineCount = 0;
InputStream is = null;
try
{
is = new FileInputStream(path);
InputStreamReader in = new InputStreamReader(is);
BufferedReader _input = new BufferedReader(in);
String line = _input.readLine();
while ( line != null )
{
if ( !line.trim().isEmpty() )
{
System.out.println(lineCount + ". " + line);
lineCount++;
}
line = _input.readLine();
}
}
finally
{
if ( is != null )
is.close();
}
return lineCount;
}
public static void main(String args[])
{
try
{
countAndDisplayNonEmptyLines("C:\\Users\\Desktop\\help.log");
}
catch ( IOException e )
{
e.printStackTrace();
}
}
}
Get words or characters from a string Java
Get all the words in a string in a simple java program.
public List getWords(String line)
{
char delimiter = ' ';
StringTokenizer tokenizer = new StringTokenizer(line, delimiter );
if ( tokenizer == null )
return null;
List result = new ArrayList();
while ( tokenizer.hasMoreTokens() )
result.add(tokenizer.nextToken());
return result;
}
Count number of Characters in a word
public static int countCharacters(String str)
{
int charCount = 0;
if ( str == null || str.trim().isEmpty() )
return charCount;
int length = str.length();
for ( int i = 0; i < length; i++ )
{
if ( str.charAt(i) != ' ' )
charCount++;
}
return charCount;
}
public static void main(String[] args)
{
String word = "LI FE";
System.out.println("Number of characters in '" + word + "' = " + countCharacters(word));
}
public List
{
char delimiter = ' ';
StringTokenizer tokenizer = new StringTokenizer(line, delimiter );
if ( tokenizer == null )
return null;
List
while ( tokenizer.hasMoreTokens() )
result.add(tokenizer.nextToken());
return result;
}
{
int charCount = 0;
if ( str == null || str.trim().isEmpty() )
return charCount;
int length = str.length();
for ( int i = 0; i < length; i++ )
{
if ( str.charAt(i) != ' ' )
charCount++;
}
return charCount;
}
System.out.println("Number of characters in '" + word + "' = " + countCharacters(word));
Counting number of spaces at the beginning of the string JAVA
Count the number of spaces at the beginning of a string in a Java program
private int countSpaces(String s)
{
int spaceCount = 0;
if ( s == null || s.trim().isEmpty() )
return spaceCount;
int length = s.length();
for ( int i = 0; i < length; i++ )
{
if ( s.charAt(i) == ' ' )
spaceCount++;
else
break;
}
return spaceCount;
}
private int countSpaces(String s)
{
int spaceCount = 0;
if ( s == null || s.trim().isEmpty() )
return spaceCount;
int length = s.length();
for ( int i = 0; i < length; i++ )
{
if ( s.charAt(i) == ' ' )
spaceCount++;
else
break;
}
return spaceCount;
}
Thursday, 5 July 2012
Sorting List or Lists in Java
Sorting a List
Example Program:
List
Collections.sort(persons, new Comparator
- > using Collections.sort(List, Comparator)
Example Program:
List
- > persons;
Collections.sort(persons, new Comparator
- >()
{
@Override
public int compare(List
{
if ( o1 == null || o2 == null || o1.size() == 0 || o2.size() == 0 )
return 0;
Person item1 = o1.get(0);
Person item2 = o2.get(0);
if ( item1 == null )
{
int size = o1.size();
for ( int i = 1; i < size; i++ )
{
item1 = o1.get(i);
if ( item1 != null )
break;
}
}
if ( item2 == null )
{
int size = o2.size();
for ( int i = 1; i < size; i++ )
{
item2 = o2.get(i);
if ( item2 != null )
break;
}
}
Date d1 = null;
Date d2 = null;
if ( item1 != null )
d1 = new Date(item1.getTime());
if ( item2 != null )
d2 = new Date(item2.getTime());
int result = 0;
if ( d1 != null && d2 != null )
{
if ( d1.before(d2) )
result = 1;
else if ( d1.after(d2) )
result = -1;
}
return result;
}
});
Labels:
Java Sorting List of Lists
Subscribe to:
Posts (Atom)