Wednesday, August 10, 2016

Update Ubuntu 14.04 to 16.04. Unity / OS menu is not loading.

If you are an Ubuntu fan and have recently updated from 14.04 to 16.04, you may run into a situation once you are done. "WHERE ARE MY MENUS?". If you don't see the menus of the OS (i.e. icons on left side aka unity), then don't panic! Do the following:

1- Right click
2- Open Terminal
3- Type the following:
dconf reset -f /org/compiz/
setsid unity


And you are done!! Life can go back to normal now...ish

Source (Thank you O master of askubuntu.com under the following link):
http://askubuntu.com/questions/761035/ubuntu-16-04-no-menu-bar-or-launcher-help

Monday, November 16, 2015

Deploy Servlets on Eclipse on Ubuntu - links to help.

How to deploy Servlets via Eclipse on Ubuntu -- start here
http://www.codejava.net/ides/eclipse/how-to-create-deploy-and-run-java-servlet-in-eclipse

If you run into problems then go here -- (I was using tomcat v7.0)
http://stackoverflow.com/questions/13423593/eclipse-4-2-juno-cannot-create-a-server-using-the-selected-type-in-tomcat-7

Last, if you have permissions preventing you to run the web application, close the eclipse and run it as admin. it will by-pass some of the restrictions with deployments. In the eclipse application directory, type:  sudo ./eclipse and it will run as admin.

Tomecat v7.0 Server, Eclipse, and Ubuntu. Server Name is blank when defining a new server

Problem: 
Tomecat v7.0 Server, Eclipse, and Ubuntu. When you Define a new Server and choose tomcat v7.9, the Server host name is populated however the server name is blank. It is expected to be something like "Tomecat v7.0 Server at localhost"

Solution:
  1. Close Eclipse
  2. In {workspace-directory}/.metadata/.plugins/org.eclipse.core.runtime/.settings. delete the following:
    • org.eclipse.wst.server.core.prefs
    • org.eclipse.jst.server.tomcat.core.prefs
  3. Restart Eclipse
Source (thanks whoever wrote the solution):
http://crunchify.com/eclipse-how-to-fix-installing-apache-tomcat-server-issue-blank-server-name-field/

Sunday, November 8, 2015

Very simple Java String methods: "Split String" and "Capitalize the First Letter "

// Some common simple Java String methods: "Split String" and "Capitalize the First Letter " 

// Split Strings, just like the String split method, but it removes all white spaces.
// I expect a million other implementation of this, but this does the job as well.
public static String[] splitString(String text, String splitChar){
String [] list = text.split(splitChar);

for (int i=0; i<list.length; i++){
list[i] = list[i].trim();
}
return list;
}

// Capitalize the First Letter of the passed in String s
public static String capitalizeFirstLetter(String s){
String result = null;

if (s != null){
if (s.length() >1)
result = s.substring(0, 1).toUpperCase() + s.substring(1, s.length());
else
result = s.toUpperCase();
}
return result;
}

Sunday, October 25, 2015

Simple Java Serialization

// this is a very simple example of java serialization example
//Serializations: Assume this is my class to be serialized
import java.util.ArrayList;

public class Dish {
private String dishName;
private ArrayList<String> ingredient;

public String getDishName(){
return this.className;
}
public ArrayList<String> getIngredient(){
return this.variableNames;
}
public void setDishName(String dishName){
this.dishName = dishName;
}
public void setIngredient(ArrayList<String> ingredient){
this.ingredient = ingredient;
}
}

// This class will read and write. I reused the one found here which is awesome.
// Thanks Awesome person who did this
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
public class Builder {
    public static void writeXML(Schema f, String filename) throws Exception{
        XMLEncoder encoder =
           new XMLEncoder(
              new BufferedOutputStream(
                new FileOutputStream(filename)));
        encoder.writeObject(f);
        encoder.close();
    }

    public static Schema readXML(String filename) throws Exception {
        XMLDecoder decoder =
            new XMLDecoder(new BufferedInputStream(
                new FileInputStream(filename)));
        Schema o = (Schema)decoder.readObject();
        decoder.close();
        return o;
    }
}

// This is a class using both
public class Food {
    public static void main(String[] args) {
        String filename = "/home/...../Dishes.xml";
        String name = "Pizza";
        ArrayList<String>  ingredients= new ArrayList<String>();
        vars.add("dough");
        vars.add("cheese");
        vars.add("sauce");

        Schema s = new Schema();
        s.setDishName(name);
        s.setIngredient(ingredients);

         try { Builder.writeXML(s, filename);  }
            catch (Exception ex){ System.out.println(ex); }
         try {
         Schema s2 = Builder.readXML(filename);
         System.out.println(s2.getDishName());
         System.out.println(s2.getIngredient().toString());
         } catch (Exception ex){ System.out.println(ex); }
    }

}//Food


// Java read from a file and return the string
public static String readFile(String fileName){

try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {

String content = "";

String line = null;
   while ((line = reader.readLine()) != null) {
       content = content + line;
   }
   reader.close();
 
   return content;
 
} catch (IOException x) {
   System.err.format("IOException: %s%n", x);
   return null;
}

}

Tuesday, June 19, 2012

Google Master Search terms

-- Search on specific site
site: websiteNameHere yourTextHere
-- Related word; Synonyms
~yourTextHere
-- Exact words
"yourWordHere"
-- Exclude term
-yourWordHere
-- Between two dates
2008..2010 yourTextHere
-- File type search
filetype:PDF yourTextHere
-- Show results with the word in title
intitle:yourTextHere
-- * Will be replaced with other words common with the searched term
*yourTextHere
yourTextHere*PartTextHere*OtherPartTextHere
-- Search for papers with specific author
author:AutherNameHere
-- Definition
define: yourWordHere
-- Calculator
2*2
(3/2)*100
-- Unit converter
10 KM in Miles
-- research, use: http://www.jstor.org/

Tuesday, June 5, 2012

Floating-point errors. They cannot precisely represent all real numbers. Here is an example.


"Floating-point numbers cannot precisely represent all real numbers, and that floating-point operations cannot precisely represent true arithmetic operations, leads to many surprising situations. This is related to the finite precision with which computers generally represent numbers." (From Wiki)

Read more about my example here.
http://majedline.blogspot.ca/2012/06/floating-point-errors-they-cannot.html 

Tuesday, May 29, 2012

Online Compilers

Two cool online compilers for code snippets:
http://codepad.org and http://ideone.com

I tried this C++ Fibonacci function and the compiler worked:

#include <iostream>
 
int fib(int n)
{
     if ( n == 0 ) return 0;
     if ( n == 1 ) return 1;
 
     return fib(n-1) + fib(n-2);
}
 
int main(){
 printf("Fib Val: %i", fib(7));
 return 0;
}

Thursday, May 17, 2012

.NET C# Get Byte array from a file


public static byte[] GetBytesFromFile(string file)
       {
            try
            {
                // check if this is a file name;
                if (file == null)
                    return null;

                if (file.Trim().Length == 0)
                    return null;

                // get the size of the buffer
                int bufferSize = (int)(new System.IO.FileInfo(file).Length);

                // check if the file has anything.
                if (bufferSize == 0)
                    return null;

                // create the buffer with the proper size
                byte[] buffer = new byte[bufferSize];

                // create the stream
                System.IO.FileStream fs = 
new System.IO.FileStream(file, System.IO.FileMode.Open);

                // create the reader of the stream
                System.IO.BinaryReader reader = new System.IO.BinaryReader(fs);

                // populate the buffer
                buffer = reader.ReadBytes(bufferSize);

                // Close the stream
                fs.Close();
                fs.Dispose();

                // close the reader
                reader.Close();
                fs.Dispose();

                return buffer;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Please Make sure that the file is valid or is not in use. Details:\r\n" + ex.Message);
                return null;
            }

        }

Monday, May 14, 2012

.NET C# Create a file and open it using Default Application Type


public static void OpenFileWithDefaultApplication(string fullfileLocationAndName, byte[] data)
{
  try
  {
    System.IO.FileStream fs =   new System.IO.FileStream(fullfileLocationAndName, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite);
      fs.Write(data, 0, data.Length - 1);
      fs.Close();
      fs.Dispose();
      System.Diagnostics.Process.Start(fullfileLocationAndName);
  }
catch (Exception ex)
   {
       MessageBox.Show("There was an error opening the file: " + ex.Message);
}
}

Tuesday, May 8, 2012

JAVA Write to a file

// Write x to file
public void writeFile(String x, String file){
    try{
        // Create file
        FileWriter fstream = new FileWriter(file);
        // Create Writer
        BufferedWriter out = new BufferedWriter(fstream);
        // Write x to the file        
        out.write(x);
        //Close the output stream
        out.close();
    }catch (Exception e){
        //Catch exception if any
        System.err.println("Error: " + e.getMessage());
    }
}

Monday, May 7, 2012

JAVA Date and time formatting

public static String getDateNow(){
  DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
return (dateFormat.format(date))+"";
}

Sunday, May 6, 2012

JAVA Manipulate a String using regular expressions

// Example 1, remove all instances of a word with another in a String.
// Example 2, remove all white spaces from a String
public static String manipulateString(String s){
  String result = s;
  
  //1 replace all instances of the word foo with the word bee 
  result = result.replaceAll("(?:foo)", "bee");

  // remove all white space
  result = result.replaceAll("\\s+", "");
   
  return result;
}

JAVA Open a file using the default application editor.

// Example Foo.xls will be opened with Excel
// Make sure to import the class java.awt.Desktop

public static void OpenFile(String filename){
  try{
    // Check if class 'Desktop' is supported on the platform 
    if (Desktop.isDesktopSupported()) {
      Desktop.getDesktop().open(new File(filename));
    }else{
      System.out.println("Class Desktop is unsupported");
    }
  } catch (Exception ex){
    System.out.println("Error opening "+ filename);
  }
}

.NET C# Write to a file

public static bool WriteToFile(string fileName, string fileContent)
{
    fileName = fileName;
    System.IO.TextWriter tw;
    try
    {
        tw = new System.IO.StreamWriter(fileName, true);
        tw.WriteLine(fileContent);
        tw.Close();
        return true;
    }
    catch{}
    return false;

.NET C# Read from a file

public static string ReadFromFile(string fileName)
{
    string ret = "";
    System.IO.StreamReader sr;
    try
    {
        sr = new System.IO.StreamReader(fileName);           
        ret = sr.ReadToEnd();
        sr.Close();
        return ret;
    }
    catch{ return null; }
}