Java: Join String Array

Java has a string split function, but it does not have a combine/join/unsplit string function. Below is an example modified from the StringUtils library.

package test;

public class Join {
    public static void main(String[] args) {
        String[] toppings = {"Cheese", "Pepperoni", "Black Olives"};
        System.out.println(join(toppings, ";"));
        System.out.println(join(toppings, null));
    }

    public static String join(final String[] array, final String separator) {
        if (array == null) {
            return null;
        }
        final StringBuilder buf = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                buf.append(separator==null ? "" : separator);
            }
            if (array[i] != null) {
                buf.append(array[i]);
            }
        }
        return buf.toString();
    }
}

Output:

Cheese;Pepperoni;Black Olives
CheesePepperoniBlack Olives

Java PadLeft/PadRight

Java left pad/right pad string functions. These’re also equivalent to C# and VB.Net PadLeft and PadRight methods.

package test;

public class Pad {
    public static String lpad(String str, int n, CharSequence c) {
        return (new String(new char[n-str.length()])).replace("\0", c) + str;
    }

    public static String rpad(String str, int n, CharSequence c) {
        return str + (new String(new char[n-str.length()])).replace("\0", c);
    }
    
    public static void main(String[] args) {
        System.out.println(lpad("astring", 10, "0"));
        System.out.println(rpad("astring", 10, "0"));
    }
}

Output:

000astring
astring000

Java: Read/Write File

Below is the source code for reading and writing text files. Unless the file size is horrendously huge, this utility should suffice.  The source code works for all versions of Java.

package test;

import java.io.*;

public class FileReadWrite {

    public static void main(String[] arg) throws IOException {
        writeFile("C:\\temp\\b.txt", "Initial contents of this file.");
        
        String outfile = readFile("C:\\temp\\b.txt");
        System.out.println("Reading file contents: "
                + outfile);

        writeFile("C:\\temp\\b.txt", "This file has been overwritten...");

        outfile = readFile("C:\\temp\\b.txt");
        System.out.println("Reading new file contents: "
                + outfile);
    }

    public static String readFile(String filename) {
        StringBuilder contents = new StringBuilder();
        try {
            File aFile = new File(filename);
            BufferedReader input = new BufferedReader(new FileReader(aFile));
            try {
                String line = null;
                while ((line = input.readLine()) != null) {
                    contents.append(line);
                    contents.append(System.getProperty("line.separator"));
                }
            } finally {
                input.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        return contents.toString();
    }

    public static void writeFile(String filename, String aContents) {
        try {
            File aFile = new File(filename);
            Writer output = new BufferedWriter(new FileWriter(aFile));
            output.write(aContents);
            output.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

Output:
Reading file contents: Initial contents of this file.

Reading new file contents: This file has been overwritten…

C# Left / Right / Mid String Functions (as in Visual Basic.Net)

Visual Basic’s left, right, mid functions are nifty tricks that can be easily implemented in C#.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    public class Test
    {
        public static String left(String input, int len)
        {
            return input.Substring(0, len);
        }

        public static String right(String input, int len)
        {
            return input.Substring(input.Length - len);
        }

        public static String mid(String input, int index, int len)
        {
            return input.Substring(index - 1, index + len - 1);
        }

        public static String mid(String input, int index)
        {
            return input.Substring(index - 1);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            String str1 = "left string";
            Console.WriteLine("Test left str: " + str1);
            Console.WriteLine("Test left str: " + Test.left(str1, 4));

            String str2 = "right string";
            Console.WriteLine("Test right str: " + str2);
            Console.WriteLine("Test right str: " + Test.right(str2, 6));

            String str3 = "mid string";
            Console.WriteLine("Test mid str: " + str3);
            Console.WriteLine("Test mid str: " + Test.mid(str3, 2, 6));

            Console.WriteLine("Test mid str: " + str3);
            Console.WriteLine("Test mid str: " + Test.mid(str3, 5));
            Console.ReadKey();
        }
    }
}

Output:
Test left str: left string
Test left str: left
Test right str: right string
Test right str: string
Test mid str: mid string
Test mid str: id stri
Test mid str: mid string
Test mid str: string

Java Left / Right / Mid String Functions (as in Visual Basic.Net)

Visual Basic’s left, right, mid functions are nifty tricks that can be easily implemented in Java.

package test;

public class Test {
    public static String left(String input, int len) {
        return input.substring(0, len);
    }
    
    public static String right(String input, int len) {
        return input.substring(input.length() - len);
    }

    public static String mid(String input, int index, int len) {
        return input.substring(index - 1, index + len - 1);
    }

    public static String mid(String input, int index) {
        return input.substring(index - 1);
    }
    
    public static void main(String[] args) {
        String str1 = "left string";
        System.out.println("Test left str: " + str1);
        System.out.println("Test left str: " + left(str1, 4));    

        String str2 = "right string";
        System.out.println("Test right str: " + str2);
        System.out.println("Test right str: " + right(str2, 6));    

        String str3 = "mid string";
        System.out.println("Test mid str: " + str3);
        System.out.println("Test mid str: " + mid(str3, 2, 6));    

        System.out.println("Test mid str: " + str3);
        System.out.println("Test mid str: " + mid(str3, 5));
    }
}

Output:
Test left str: left string
Test left str: left
Test right str: right string
Test right str: string
Test mid str: mid string
Test mid str: id str
Test mid str: mid string
Test mid str: string

Downloading Forex Prices into Excel Spreadsheet

You can download historical foreign exchange data into a Microsoft Excel spreadsheet using the following XLS.

The spreadsheet provided above is an improvement over the excellent VBA code provided by the website investexcel.net.  The following two features are added:

  • Combo boxes for the currency pair to make finding currencies simple and easy.
  • Date pickers for the to and from date ranges.

If you think either the combo boxes or the date pickers are a hassle to deal with and only want one of them, you can go to the developer tab, enable design mode, and delete them.  The VBA code will work just fine without them.

Process Optimization in Manufacturing

I worked in a manufacturing environment.  This is my experience in process optimization in a manufacturing environment.

First, the setup of a manufacturing plant typically consists of a process flow with 3 types of operations/stations: the assembly stations, the test stations and the debug/disposition stations.

As the name implies, the assembly stations are the ones that assemble the raw components into intermediate semi-finished or finished products.  The test stations, on the other hand, can be functional-test stations or QA stations that test the functionality/quality of the products.  They can either be the final test stations or they can test specific attributes of semi-finished products.  As the components/products flow down the production line, the goods can go through multiple combinations of assembly/test stations depending on the requirements.  If the goods were to fail at any test stations, the debug stations will come in.  They’ll be redirected there for examination on the root causes of the failure and a disposition will be issued by the debug station.  Based on the disposition, the goods will flow back to an assembly or a test station in the manufacturing process for either rework or retest.

There are 3 optimizations that can be done based on the above setup of the manufacturing plant.

For the first optimization, it has to do with the debug station “looping” problem.  If the debug station is computerized or highly computerized, it’s therefore deterministic – if you feed the debug station with a defective product, it will give you a fixed disposition.  If you feed the debug station again with the same defective product, the diagnosis will again be the same.  This is fine, as long as the disposition is accurate.  However, what if the computerized disposition is inaccurate?  This will be a big problem.  If component A on the product is defective but the diagnosis by the debug station is to change/rework component B, the product will go into an infinite loop of changing component B, then debug, then change component B, then debug.  The product will never be manufactured!  Thus, it’s important to have a computerized system keeping close tabs to the yields, the process flows and the debug dispositions of the products.  If it’s found that the yields are low, the process keeps on looping with the same debug dispositions, actions can be taken on the debugging software to rectify the problem areas.  This debug station “looping” problem can be serious or subtle depending on the setup of the processes and nature of the products.

The second optimization a lot of plants can aim for is regarding work-in-progress (WIP) goods management.  WIP is money; for perishable and fast obsolescence products such as food and electronic products, this can be a big problem.  Thus, saving WIP is saving money which is, of course, beneficial to the bottom line.  For many products, WIP might be consumed in areas such as the long burn-in process for electronic products or it can be consumed by the need to transport the semi-finished product to faraway land for further processing or the goods might be stuck in a process that is expensive/laborious/time-consuming.  A way to reduce WIP in these instances is to precede the process with a test station to pre-filter out potential defective products so that the goods will not proceed into the expensive/laborious/lengthy process.  Ideally, the greatest amount of filtration can be achieved by copying the entire final functional test suite to the semi-finished products (of course, I understand that this might be impossible to attain for some cases).

The third and last optimization I would like to share is related to contamination control.  A lot of products require a hygienic environment; some even need a clean-room environment to operate.  Contamination control in these environments is of paramount importance as dirty surroundings will lower the quality and yield of the final products.  Plants with stringent contamination controls are usually demarcated by areas that are sanitized and “normal” areas that are not sterilized.  Great care is usually taken for the movement of goods and humans between clean and not so clean areas.  Reduction in the movement of goods can be achieved by the use of a filter station at the inlet and outlet of the demarcated areas.  Semi-finished goods that failed the filter stations can be serviced by assembly/test/debug stations within that area; they are not being allowed to transport to the other zone.  Doing so will reduce the movements of goods and achieved contamination control.

This is the experience I’ve achieved in my career.  Hopefully it’s useful to your plant.  Thanks for reading.