Saturday, April 30, 2016

[Java] What is while(true){} in Java


 Example:
while(true) {
   //do Something
}


What is while(true){} in Java?
It's a way to create a endless loop. When true=true (It's always return true), run the statements within the while loop.

And this code do sometime same if using a flag:
boolean keepGoing = true;
while(keepGoing) {
   //do Something
}

Reference:

http://www.coderanch.com/t/592208/java/java/Meaning-true
http://stackoverflow.com/questions/6850380/are-whiletrue-loops-so-bad

Wednesday, April 27, 2016

[Codeigniter] Call to undefined function base_url()

base_url() in a function in Url helper , please check have you loaded it before you use.
$this->load->helper('url');
for example, load that for specified function :


Or load for all functions in controllers :

Or you can load that to you whole website, load globally. just edit application/config/autoload.php and add 'url' as one of the value of $autoload['helper']:

$autoload['helper'] = array('url');



Reference:
http://stackoverflow.com/questions/11581636/fatal-error-call-to-undefined-function-base-url-in-c-wamp-www-test-ci-applic

Wednesday, April 20, 2016

[NetBeans][Resolved] Cannot uninstall netbean 8.1 on Windows machine


Error Message:
It appears that the following instance of the NetBeans IDE is still running:
C:\Program Files|NetBeans 8.1_1
A lock file exists at C:\Users\xxx\AppData\Roaming\NetBeans\8.1\lock
Please close this NetBeans IDE prior to continuing with uninstallation
Netbean version 8.1
Windows 8



If you want uninstall NetBean but get this message in windos machine, firstly you should check is Netbean running in this machine by press "CTRL"+"ALT"+"DELETE" to call Task Manager and check if the name related to netBean existed there, if yes, right click and select "End Task" and do uninstallation again.

However for my case i can't find netbean is running in the Task Manager.


Solution

Step 1

You can visit the file installation directory which's shown in error message (it's C:\Users\xxx\AppData\Roaming\NetBeans\8.1\ in my case ). 

Step 2

Found out the file named "lock" and then delete it.

Step 3

Uninstall NetBean again.



Of course you can visit the log file shown at the error message to check out what's problem about, but what i found in log file is similar to the error message:



[Java][Answer] CodingBat Array-2 > has77()

Given an array of ints, return true if the array contains two 7's next to each other, or there are two 7's separated by one element, such as with {7, 1, 7}.

has77([1, 7, 7]) → true
has77([1, 7, 1, 7]) → true
has77([1, 7, 1, 1, 7]) → false

Answer 1:

public boolean has77(int[] nums) {
  for(int i=0;i<nums.length-1;i++){
    if(nums[i]==7&&nums[i+1]==7) return true;
    if(i<nums.length-2 && (nums[i]==7&&nums[i+2]==7)) return true;
  }
  return false;
}

Answer 2:

public boolean has77(int[] nums) {
boolean result = false;

  for (int i = 0; i < nums.length-1; i++)
  if ((nums[i] == 7 && nums[i+1] == 7))
  result = true;
 
   for (int i = 0; i < nums.length-2; i++)
  if ((nums[i] == 7 && nums[i+2] == 7))
  result = true;

Reference

http://www.javaproblems.com/2012/12/coding-bat-java-array-2-has77.html

Tuesday, April 19, 2016

[Java][Answer] Array-1 > rotateLeft3

Given an array of ints length 3, return an array with the elements "rotated left" so {1, 2, 3} yields {2, 3, 1}.

rotateLeft3([1, 2, 3]) → [2, 3, 1]
rotateLeft3([5, 11, 9]) → [11, 9, 5]
rotateLeft3([7, 0, 0]) → [0, 0, 7]

Answer 1:
public int[] rotateLeft3(int[] nums) {
  return new int[] {nums[1],nums[2],nums[0]};
}
Answer 2:
public int[] rotateLeft3(int[] nums) {
  int[] tempArr = new int[3];
  tempArr[0] = nums[1];
  tempArr[1] = nums[2];
  tempArr[2] = nums[0];
  return tempArr;
}

Reference
http://www.javaproblems.com/2012/12/coding-bat-java-array-1-rotateleft3.html

[Java][Answer] CodingBat Array-1 > sum3()

Given an array of ints length 3, return the sum of all the elements.

sum3([1, 2, 3]) → 6
sum3([5, 11, 2]) → 18
sum3([7, 0, 0]) → 7

Answer 1:

public int sum3(int[] nums) {
  return nums[0] + nums[1] + nums[2];
}

Answer 2:

public int sum3(int[] nums) {
  int sum = 0;
  for(int i=0;i<nums.length; i++){
    sum += nums[i];
  }
  return sum;
}

Reference

http://www.javaproblems.com/2013/11/java-array-1-sum3-codingbat-solution.html

Monday, April 18, 2016

[Java][Answer] CodingBat Array-1 > maxEnd3()

Given an array of ints length 3, figure out which is larger, the first or last element in the array, and set all the other elements to be that value. Return the changed array.

maxEnd3([1, 2, 3]) → [3, 3, 3]
maxEnd3([11, 5, 9]) → [11, 11, 11]
maxEnd3([2, 11, 3]) → [3, 3, 3]

Answer 1:

public int[] maxEnd3(int[] nums) {
  int max = Math.max(nums[0],nums[2]);
  return new int[]{max,max,max};
}

Answer 2:

public int[] maxEnd3(int[] nums) {
  if (nums[0] >= nums[nums.length-1]) {
  nums[0] = nums[0];
  nums[1] = nums[0];
  nums[2] = nums[0];
  }
  else if (nums[0] <= nums[nums.length-1]) {
  nums[0] = nums[nums.length-1];
  nums[1] = nums[nums.length-1];
  nums[2] = nums[nums.length-1];
  }
  return new int[] { nums[0],nums[1],nums[2]};
}

Reference

http://www.javaproblems.com/2012/12/coding-bat-java-array-1-maxend3.html

Sunday, April 17, 2016

[Java][Answer] Array-1 > makeMiddle

Given an array of ints of even length, return a new array length 2 containing the middle two elements from the original array. The original array will be length 2 or more.

makeMiddle([1, 2, 3, 4]) → [2, 3]
makeMiddle([7, 1, 2, 3, 4, 9]) → [2, 3]
makeMiddle([1, 2]) → [1, 2]

Answer 1:

public int[] makeMiddle(int[] nums) {
  return new int[]{nums[nums.length/2-1],nums[nums.length/2]};
}

[Java][Answer] Array-1 > makeLast()

Given an int array, return a new array with double the length where its last element is the same as the original array, and all the other elements are 0. The original array will be length 1 or more. Note: by default, a new int array contains all 0's.

makeLast([4, 5, 6]) → [0, 0, 0, 0, 0, 6]
makeLast([1, 2]) → [0, 0, 0, 2]
makeLast([3]) → [0, 3]

Answer 1

public int[] makeLast(int[] nums) {
  int[] num = new int[nums.length*2];
  num[nums.length*2 - 1] = nums[nums.length -1];
  return num;
}

Answer 2

public int[] makeLast(int[] nums) {
  int[] tempArr = new int[nums.length*2];
  for(int i=0; i<tempArr.length; i++){
    tempArr[i] = 0;
  }
  tempArr[tempArr.length-1] = nums[nums.length-1];
  return tempArr;
}

To use answer 1, firstly you need to know by default int array element default element is 0.

Reference
http://www.javaproblems.com/2012/12/coding-bat-java-array-1-makelast.html

Friday, April 15, 2016

[Java][Answer] CodingBat Array-2 > matchUp()

Given arrays nums1 and nums2 of the same length, for every element in nums1, consider the corresponding element in nums2 (at the same index). Return the count of the number of times that the two elements differ by 2 or less, but are not equal.

matchUp([1, 2, 3], [2, 3, 10]) → 2
matchUp([1, 2, 3], [2, 3, 5]) → 3
matchUp([1, 2, 3], [2, 3, 3]) → 2

Answer 1:

public int matchUp(int[] nums1, int[] nums2) {
  int count = 0;
 
  for (int i = 0; i < nums1.length; i++) {
    int tmp = Math.abs(nums1[i] - nums2[i]);
    if (tmp <= 2 && tmp > 0)
      count++;
  }
  return count;
}

Answer 2:

public int matchUp(int[] nums1, int[] nums2) {
  int diffCount = 0;
  for(int i=0;i<nums1.length;i++){
    for(int j=0;j<nums2.length;j++){
        if(i==j){
            int diff = Math.abs(nums2[j] - nums1[i]);
            if(diff<=2 && diff!=0) diffCount++;
        }
    }
  }
  return diffCount;
}

Reference

http://www.javaproblems.com/2013/11/java-array-2-matchup-codingbat-solution.html

Wednesday, April 13, 2016

[Java][Answer] Array-1 > fix23()

Given an int array length 3, if there is a 2 in the array immediately followed by a 3, set the 3 element to 0. Return the changed array.

fix23([1, 2, 3]) → [1, 2, 0]
fix23([2, 3, 5]) → [2, 0, 5]
fix23([1, 2, 1]) → [1, 2, 1]

Answer 1

public int[] fix23(int[] nums) {
  if(nums[0]==2 && nums[1]==3) nums[1] = 0;
  if(nums[1]==2 && nums[2]==3) nums[2] = 0;
  return nums;
}

Answer 2

public int[] fix23(int[] nums) {
  if (nums[0] == 2 && nums[1] == 3)
   nums[1] = 0;
   if (nums[1] == 2 && nums[2] == 3)
   nums[2] = 0;
   return new int[] {nums[0],nums[1],nums[2]};
}

Reference

http://www.javaproblems.com/2012/12/coding-bat-java-array-1-fix23.html

Monday, April 11, 2016

How do HTML and XML differ in their purposes?

  1. HTML emphasizes data display and XML emphasizes the data content.
  2. HTML only allow pre-defined tags and XML allow customized tags.
  3. Tags in HTML are not case-sensitive and tags in XML are case-sensitive.
  4. Multiple adjacent white-spaces in an element content are different from a single white-space in XML, but same in HTML
  5. Double quote around attribute value are optional to HTML, but it’s compulsory in XML.
  6. HTML  processed mainly by standard web browsers, and XML processed by tailor-made programs as well as generic XML parsers
Reference:
Java Application Development and Programming Languages U06, p.11, Q1

Sunday, April 10, 2016

[Java][Answer] Array-1 > start1()

Start with 2 int arrays, a and b, of any length. Return how many of the arrays have 1 as their first element.

start1([1, 2, 3], [1, 3]) → 2
start1([7, 2, 3], [1]) → 1
start1([1, 2], []) → 1

Answer 1:

public int start1(int[] a, int[] b) {
  int sum = 0;
  if(a.length>0 && a[0] == 1) sum++;
  if(b.length>0 && b[0] == 1) sum++;
  return sum;
}

Answer 2:

public int start1(int[] a, int[] b) {
int count = 0;
  if (a.length != 0) {
     if (a[0]== 1) count++; }
  if (b.length != 0) {
     if (b[0]== 1) count++; }
    
     return count;
}

Reference

http://www.javaproblems.com/2012/12/coding-bat-java-array-1-start1.html

Wednesday, April 6, 2016

[Windows 8] Make a Software Run at Startup on Windows 8

Step 1 

Press "Ctrl"+"Alt"+"Delete" and then check "Task Manager".

Step 2

and then you will see this windows. Click "File" and then "Run new task":

 Step 3

Input "shell:startup" in textbox and click "OK".


shell:startup

Step4

And then it would pop up the folder shown below,  location : 
C:\Users\username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
Copy and paste the shortcut file of  Software which you want to run when startup the windows 8.


Reference:


http://isvincent.pixnet.net/blog/post/38425585-windows-8-%E8%A8%AD%E5%AE%9A%E9%96%8B%E6%A9%9F%E8%A6%81%E7%AB%8B%E5%8D%B3%E5%9F%B7%E8%A1%8C%E7%9A%84%E7%A8%8B%E5%BC%8F

Sunday, April 3, 2016

[Linux] -bash : zip: command not found

Got this message mean you missed install the zip software,  use the command to install it:

Linux Debina / Ubuntu
apt-get install zip
apt-get install unzip

Redhat, Fedora, CentOS

yum install zip
yum install unzip
After installed the software, use cd command to go the target directory to run zip command and you would found the zipped file.

zip -r techtach.zip /var/www/example.com/public_html


Reference:

http://techtach.com/2015/03/how-to-unzipzip-files-in-linuxunix-using-command-line/