Saturday, December 20, 2014

Use Javascript to upload csv file and chain replace method

Uploading the csv file was the easy part thanks to this code. (Just make sure you have appropriate code to display it since the author just uses console.log.)

Once the csv file was uploaded, my goal was to select certain rows and then, from those rows, select certain fields to display. We'll start with how the code to upload the csv file works.

Uploading the csv file:
Mounir Messelmeni's processData(csv) method takes the csv file you select as an argument and stores it in a variable as a string using split(). Once the string is appropriately split, the code then uses nested for loops to first split each record and then store each record as an element of an array. (You'll notice my code is slightly different regarding the separators I used.)

Regexp patterns
My problem was that I needed to access the individual fields within the records. That sounded a lot like accessing elements in an array, except I wasn't dealing with an array but an element in an array. Naturally, my first thought was to make each element its own array so I could access the fields as elements within the new array. You've probably already guessed the problem. When I created the new array and assigned an element to it, that didn't split anything up. I just had an array with one element.

I then considered separating each char and storing certain ones into an array and that array would then become an element in another array. That made me tired thinking about it. Plus, I still had the issue of figuring out how to target certain chars.

I also thought about saving the array element containing a record as a string, separating the string, and based on the separation, creating elements for an array. But I finally decided it was just easier to search for patterns. I am not claiming this is the best way, just that it is one way.

My code starts by storing lines.toString() in var x. I then create two patterns. (The csv file only contains 2 fields per record. For demonstrative purposes, text in both fields will be replaced.)

I also created an empty string, newstr which is then assigned the variable "x" with chained replace methods. The value of newstr becomes the value of the element with the id "list". I include an alert to show you the value of x before so you can compare that with the replace values.

The full code, including the csv file I used, is here.

Wednesday, December 17, 2014

Java command line

It's a basic concept, but it threw me at first. How do the command line and a Java program you wrote in an IDE interact?

First, the command line:
     a) Save your java file in a text editor as YourFile.java. Read this for more details.
     b) Get to the command line. If you're using Win8, for example, go to the Start menu and either type in "cmd" or use search and type in "command".
     c) On the command line itself, first change to the directory in which you saved your file. (See page I referred to above.)
     d) Set the path. The page referred to above addresses this but it didn't work for me. This is what worked for me. Proceed with caution.
     e) Compile by entering this at the command prompt - javac YourJavaFileName.java
     f) Run the interpreter by entering this at the next prompt - java YourJavaFileName command parameters
     ex. java InvestmentFile calculate .04 10 5000
    //calculate is the command;
   //.04, 10 and 5000 are the arguments

Entering the command: Just make sure the user knows which command to enter in the beginning and follow up in case they still enter the wrong thing.

if(user enters right command)
{//code}
else
{//tell them what to do or use a try-catch block instead}


Now, how do the parameters in the methods connect to the arguments on the command line?

Let's assume in the example above that .04 represents the interest rate, 10 represents the number of years and 5000 represents the amount invested.

Let's say you have a method that has these 3 things as parameters. Here is how you'd normally write the method:

public double calculateROI(double rate, int yrs, double money)
{
    //code
}
 But for the command line, here is how you write the method:
public double calculateROI(args[0], args[1], args[2])
{
    //code
}

Look familiar? Yes, finally, we see a point for those args in the main method.  Here is where exception handling comes in. If the first parameter in your method is the interest rate and, obviously, you want the first argument written on the command line to be the interest rate, you have to account for handling the error of someone not doing that. Otherwise, if the number of years is entered as the first argument, you clearly won't get the right result.

This isn't unlike dealing with parameters and arguments normally. Just make sure the user enters the arguments in the right order, after you've made sure they've entered the right command.