Tuesday, October 6, 2015

Monday, October 5, 2015

[Java] Formatting numeric string output

include java.io.*; 
....
System.out.format("..... %h ......', String word);


Converters and Flags Used in TestFormat.java
ConverterFlagExplanation
d A decimal integer.
f A float.
n A new line character appropriate to the platform running the application. You should always use %n, rather than \n.
tB A date & time conversion—locale-specific full name of month.
td, te A date & time conversion—2-digit day of month. td has leading zeroes as needed, te does not.
ty, tY A date & time conversion—ty = 2-digit year, tY = 4-digit year.
tl A date & time conversion—hour in 12-hour clock.
tM A date & time conversion—minutes in 2 digits, with leading zeroes as necessary.
tp A date & time conversion—locale-specific am/pm (lower case).
tm A date & time conversion—months in 2 digits, with leading zeroes as necessary.
tD A date & time conversion—date as %tm%td%ty
 08Eight characters in width, with leading zeroes as necessary.
 +Includes sign, whether positive or negative.
 ,Includes locale-specific grouping characters.
 -Left-justified..
 .3Three places after decimal point.
 10.3Ten characters in width, right justified, with three places after decimal point.

Source

Regards,
HAJAR.

[Java] Override Error (Java compatibility setting)

I've just learned that there are compatibility settings in Java.

Earlier versions of java doesn't support certain newer features. So whenever you see an error on parts of the code where you really don't see any error, go to project > Properties > Java Compiler > Enable Project specific setting and change the compiler compliance level to a later version, depending on the java features you want to make enabled, such as @Override which only applies to Java 1.5 and up.

Source

Regards,
HAJAR.

[Windows] Best way to make shutdown timer

At least for me LoL

Source

Regards, HAJAR.

Saturday, October 3, 2015

[Java try..finally] Unhandled Exception Type IOException

While trying to program Java Files and I/O on java, I have encountered this error while trying to implement the finally block.

Unhandled Exception Type IOException

This was the code snippet I'd implemented, referred from tutorialspoint's java tutorials:

 
try {
         in = new FileInputStream("input.txt");
         out = new FileOutputStream("output.txt");
         
         int c;
         while ((c = in.read()) != -1) {
            out.write(c);
         }
      }finally {
         if (in != null) {
            in.close();
         }
         if (out != null) {
            out.close();
         }
      }

The trick is to add a catch IOException for the first try and try...catch in the finally block. The fix that works:


try {
 in = new FileInputStream("input.txt");
 out = new FileOutputStream("output.txt");
 while ((c = in.read())!= -1){
 out.write(c);
 }
} catch (IOException e) {
 System.out.println(e);
} finally {
 try {
 if (in != null) in.close();
 if (out != null) out.close(); 
 } catch (IOException ex) {
  System.out.println(ex);
 }
}

That's all for today. Regards, HAJAR.