Author Archives: Wira Sihombing
Problem when installing Toad on 64-bit Windows
I recently trying to install Toad on my 64-bit windows,
Unfortunelately, i can’t connect to oracle server. It said “Cannot load OCI DLL: C:\[your_instant_client_directory]\oci.dll”.
Then i found the solution. Toad can’t use 64-bit instant client, it use 32-bit instant client version.
Just remove your 64-bit instant client and replace with 32-bit version.
Hope that works!
Reading from Console: JAVA Scanner vs BufferedReader
When read an input from console, there are two options exists to achive that. First using Scanner, last using BufferedReader. Both of them have different characteristics. It means differences how to use it.
Scanner treated given input as token. BufferedReader just read line by line given input as string. Scanner it self provide parsing capabilities just like nextInt(), nextFloat().
But, what is others differences between?
- Scanner treated given input as token. BufferedReader as stream line/String
- Scanner tokenized given input using regex. Using BufferedReader must write extra code
- BufferedReader faster than Scanner *point no. 2
- Scanner isn’t synchronized, BufferedReader synchronized
Scanner come with since JDK version 1.5 higher.
When should use Scanner, or Buffered Reader?
Look at the main differences between both of them, one using tokenized, others using stream line. When you need parsing capabilities, use Scanner instead. But, i am more comfortable with BufferedReader. When you need to read from a File, use BufferedReader, because it’s use buffer when read a file. Or you can use BufferedReader as input to Scanner.
Want to know deeper about Scanner or BufferedReader?
Read input from console using JAVA Scanner and BufferedReader Library
As beginner to desktop programming, we usually started learning using console application. Simply learn how to read an input from console and how to write output to console. Java provide two ways to achieve that, one using Scanner class, other used BufferedReader. Both of them have different characteristics. Here is the code:
Scanner
import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * @author CodeLover */ public class Console { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String name; List<String> hobbies = new ArrayList<String>(); int age; String choice; // INPUT System.out.print("Enter your name: "); name = scan.nextLine(); System.out.print("Now, enter your age below:"); age = scan.nextInt();scan.nextLine(); System.out.print("What is your hobbies (separated by comma): "); String temp = scan.nextLine(); // process entered hobby Scanner scanHobby = new Scanner(temp); scanHobby.useDelimiter(","); while (scanHobby.hasNext()) { hobbies.add(scanHobby.next().trim()); } // OUTPUT System.out.print("\n\nYou've just entered '" + name + "' as your name.\n"); System.out.println("Your age is " + age + " years old"); System.out.println("Your hobbies are"); for (String hobby : hobbies) { System.out.println("-" + hobby); } do { System.out.print("Is that correct? (Y/N) : "); choice = scan.nextLine(); if (!choice.equals("Y") && !choice.equals("N")) { System.out.println("Please choose Y or N"); } } while (!choice.equals("Y") && !choice.equals("N")); if (choice.equals("Y")) { System.out.println("\nThanks for submitting!"); } else { System.out.println("\nYou've cancelled submitting"); } } }
If you notice, you will wonder what this line suppose to do:
System.out.print("Now, enter your age below:"); age = scan.nextInt();scan.nextLine();
Scanner will tokenize all input. NextLine() method will consume until line feed found. Others method like nextInt(), nextFloat() or simply next() will consume next token parsed by existing delimiter.
When you enter: 45 35 65 45
first call nextInt() will get ’45’
nextLine() will get remaining line ’35 65 45′ without provide System halt
When nextInt(), nextLine() or thers ‘next’ method was called, i discover it will lookup to it’s buffer. If there no buffer/token, than it do System halt to get input from user.
BufferedReader
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * * @author CodeLover */ public class Console { public static void main(String[] args) { BufferedReader scan = new BufferedReader(new InputStreamReader(System.in)); String name; String hobbies[]; int age; String choice; try { // INPUT System.out.print("Enter your name: "); name = scan.readLine(); System.out.print("Now, enter your age below:"); age = Integer.parseInt(scan.readLine()); System.out.print("What is your hobbies (separated by comma): "); hobbies = scan.readLine().split(","); // OUTPUT System.out.print("\n\nYou've just entered '" + name + "' as your name.\n"); System.out.println("Your age is " + age + " years old"); System.out.println("Your hobbies are"); for (String hobby : hobbies) { System.out.println("-" + hobby.trim()); } do { System.out.print("Is that correct? (Y/N) : "); choice = scan.readLine(); if (!choice.equals("Y") && !choice.equals("N")) { System.out.println("Please choose Y or N"); } } while (!choice.equals("Y") && !choice.equals("N")); if (choice.equals("Y")) { System.out.println("\nThanks for submitting!"); } else { System.out.println("\nYou've cancelled submitting"); } } catch (IOException e) { e.printStackTrace(); } } }
Problem 23 – Projecteuler
A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.
A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.
As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.
Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.
First, we must create a function to sum all proper divisors from a given number. With that function we can iterate over to get all abundant numbers. Then with 6.965 abundant numbers we had, we try all possible combination, it must be 6.965*6.965 possible combination. But, we do not need to evaluate it all. If the sum crossed the limit, just move into next candidate. Here i’m using HashMap instead using ArrayList, the reason is, it is faster to search inside list than ArrayList.
import java.util.*; public class Problem23 { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); int count = 0; for (int i=1; i<28123; i++) { int sum = sumOfDiv(i); if (sum > i) { list.add(i); count++; } } Map<Integer, String> map = new HashMap<>(); for (int one : list) { for (int two : list) { int sum = one + two; if (sum > 28123) { break; } else { if (!map.containsKey(sum)) { map.put(sum, null); } } } } int total = 0; for (int i=1; i<=28123; i++) { if (!map.containsKey(i)) { total += i; } } System.out.println(total); } public static int sumOfDiv(int pValue) { List<Integer> listValue = new ArrayList<>(); int totalValue = 1; for (int i=2; i<=Math.sqrt(pValue) + 1; i++) { if (pValue%i == 0 && pValue != i) { if (!listValue.contains(i)) { listValue.add(i); } if (!listValue.contains(pValue/i)) { listValue.add(pValue/i); } } } Iterator<Integer> iterator = listValue.iterator(); while (iterator.hasNext()) { totalValue += iterator.next(); } return totalValue; } }
Install Oracle Java 6 JDK on Ubuntu 12.04
Previously, i had installed Oracle Java 7 JDK x64 on my Ubuntu 12.04. A few applications didn’t work well on JDK 7 such as Oracle SQL Developer, it is only showing 50 records maximum event there are more exists.
So, i start goole.it. Guess what, as Oracle SQL developer “splash screen” said that compatible version of java is JDK 6 only, the article i’ve found said it either.
And then i downloaded Oracle Java 6 JDK x64 (jdk-6u33-linux-x64-rpm.bin), but i don’t know the correct way to install it. I am new on Ubuntu, i didn’t even know what is the differences between jdk-6u33-linux-r64-rpm.bin and jdk-6u33-linux-x64.bin.
With a help from article, then i found to install Oracle Java 6 JDK x64. Here is:
chmod +x jdk-6u33-linux-x64.bin ./jdk-6u33-linux-x64.bin sudo chown root. -R jdk1.6.0_33/ sudo mv jdk1.6.0_33/ /usr/lib/jvm/ sudo update-alternatives --install "/usr/bin/java" "java" \ "/usr/lib/jvm/jdk1.6.0_33/bin/java" 1 sudo update-alternatives --install "/usr/bin/javac" "javac" \ "/usr/lib/jvm/jdk1.6.0_33/bin/javac" 1 sudo update-alternatives --install "/usr/bin/javaws" "javaws" \ "/usr/lib/jvm/jdk1.6.0_33/bin/javaws" 1 sudo update-alternatives --config java sudo update-alternatives --config javac sudo update-alternatives --config javaws
Download Oracle Java 6 JDK here