#Java - Code Snippets for '#Coding' - 4 code snippet(s) found |
|
Sample 1. Code Sample / Example / Snippet of org.apache.commons.compress.archivers.zip.ZipEncoding | |
|
private static void assertUnicodeName(final ZipArchiveEntry ze,
final String expectedName,
final String encoding)
throws IOException {
if (!expectedName.equals(ze.getName())) {
final UnicodePathExtraField ucpf = findUniCodePath(ze);
assertNotNull(ucpf);
final ZipEncoding enc = ZipEncodingHelper.getZipEncoding(encoding);
final ByteBuffer ne = enc.encode(ze.getName());
final CRC32 crc = new CRC32();
crc.update(ne.array(), ne.arrayOffset(),
ne.limit() - ne.position());
assertEquals(crc.getValue(), ucpf.getNameCRC32());
assertEquals(expectedName, new String(ucpf.getUnicodeName(),
CharsetNames.UTF_8));
}
}
|
|
Like Feedback org.apache.commons.compress.archivers.zip.ZipEncoding |
|
|
Sample 2. Write a program / method to print first letter of words in a string. | |
|
public void printFirstLetterOfWords(String str){
String[] splittedStringArray = str.split(" ");
for(String word:splittedStringArray){
System.out.println(word.charAt(0));
}
}
|
|
Like Feedback Code Coding String |
|
|
Sample 3. Write a Program to swap 2 variables ( using 3rd variable ) | |
|
int x = 1;
int y = 2;
int z = x;
x = y;
y = z;
System.out.println("x="+x);
System.out.println("y="+y);
|
|
Like Feedback swap 2 variables code coding |
|
|
Sample 4. Write a Program to read a file and then count number of words | |
|
public class CountwordsinFile {
public static void main(String[] args) throws IOException{
String line = null;
File file = new File("C:Hello.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
int countWords = 0;
do {
line = bufferedReader.readLine();
countWords += line.split(" ").length;
} while(line != null);
}
}
|
|
Like Feedback file handling count number of words code coding file handling |
|
|