Wednesday, January 28, 2015

CSS: Background and repeat

I was playing around with displaying an image via img src in HTML and background in CSS. I did this:
      #image_id{
width: 45px;
height: 45px;
background:     url(/*insert location of image here*/);
}

The result was the image displayed two times and appeared to be trying to display a third time, except that it was partially blocked by another image. I didn't have this problem using img src in HTML. Why?

Because background causes the image to repeat. The solution is to add:
        background-repeat: no-repeat;

There is also the option of having the image repeat on the x-axis or y-axis only.

I make no representation as to the validity or desirability of using background instead of img src. I do, however, hope to save someone a little time when they start going batty trying to figure out where in their code they made the image appear twice when they use background. Happy cascading!



Thursday, January 8, 2015

Simple marquee/ticker in Javascript/HTML

First, here is my code. Second, you can find code to make a marquee here or here.

Third, what I wanted to do was count the number of days since the Chibok girls in Nigeria had been kidnapped by Boko Haram. The main thing for me to figure out was how to count so that the number of days would be updated each day. I discovered Date.parse() and decided to subtract the date of the kidnapping from the current date using Date.parse(). That total is then divided by 86400000 because Date.parse() will give you the number of milliseconds since 1/1/70, so you have to convert that to days. I then used Math.round.

Finally, I just inserted the marquee code.

Now go make a ticker!

Sunday, January 4, 2015

Planning your first app/program

First of all, I'm talking about something more than "Hello Word". Second, I started this process in the fall and "completed" it at the end of December with not quite the result I expected. I learned a little more about Javascript and a lot about planning.

1) Don't get caught up in the details - survey the big picture first:
    I wanted to create a real estate app. I spent a significant amount of time researching how to access the local MLS database. What I finally discovered was that I couldn't get access to the database unless I had major financial backing.  But before I spent any time researching the database, I should've considered what OS my target (an individual in this case) used. It was a Mac. I do not own a Mac. I do not intend to own a Mac. You may naturally conclude at this point that I do not know Swift and don't plan on learning it anytime soon. Hmmm. What to do.

2) You're about to spend $$ - do you really need to?
     I found a solution to my Mac predicament. It would be a small investment but if I could make a cool, functional, pretty app, it would be worth it. Again, I spent a lot of time learning about the solution in question. Then the reality of car tags, HOA dues and bar dues hit me. I wondered if there was another way that wouldn't cost as much. Free would be good.

3) Don't overcomplicate:
    Yes, there was an option. It's called Javascript. It'd didn't accomplish half of what my original app idea would do. However, it would accomplish one essential function that was the most important part of all. I got that part right - if you can't make your app do everything, focus on what is absolutely essential, the real reason for your app existing in the first place.
     What I did not get right is, once more, spending hours upon hours, figuring out how to do a particular task with Javascript. I don't mind that I did this. I learned about uploading a csv file with Javascript and I spent some quality time with regexp. In the real world, however, customers won't wait while you spend a few months exploring options in depth without exploring the real potential of those options.
      After spending quite a lot of time overcomplicating, I realized my end user already had half of what I needed - a pivot table that I could import with html. In the end, I made a simple web page that would display multiple pivot tables and accomplished what I intended in the beginning. It was not as great as my original idea, but it was free and that increased its value significantly under the circumstances. It was, in fact, prettier and more professional looking than the result would've been with my pre-Javascript and Javascript solutions.

As I embark on my next project, I've learned to flit around. I bounce from topic to topic because I inevitably hit on something that tells me I need to study this less and that more. I also am keeping in mind the big picture and I'm trying to do a better job of connecting the dots in advance.

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.


Monday, November 17, 2014

Xojo: Creating a report from a listbox

Initial disclaimers: The code presented here is not my own. My goal is to fill in some of the blanks when it comes to learning to create a simple report from a listbox in Xojo. There isn't a lot about Xojo out there and I feel the more information that is presented in various formats, the more likely people are discover the beauty of Xojo (formerly known as RealBasic or Real Studio).

Step 1:
Put something in the listbox! The code I used comes from here. I imported a csv file so I didn't need the code to write to the listbox, just to read it. As a result, I just skipped the write portion of the code. I also added a button called "Load" to the window and put this code under the Load action event.

Step 2:
Watch this video and read the Examples folder under Printing and Reporting. That's where the rest of my code came from. The Examples folder is in Xojo and you'll see it on the sidebar when Xojo first opens.

Step 3:
Under App, choose the appropriate window. Otherwise, you won't see anything. It's a small step and one I overlooked. I drove myself nuts trying to figure out why nothing was displaying as planned.

Step 4:
As one of my Xojo friends explained, if you have a Mac, you will be able to preview the report as a PDF. Pretty cool since that's mostly what you'll want to do - preview the pdf, email the pdf. But in Windows, that functionality isn't present and, unfortunately, it's still a work in progress in Xojo itself, although, high up on the to-do list. There are a couple of options, besides just literally printing out the entire report.
      a) Use the Dyna plugin but it's not free. However, if you're creating an app to make money and not just for fun, it might be worth the cost. Like the Xojo license, it's not unreasonably priced.
      b) Create your own pdf framework. This is outside my realm of knowledge.
      c) Use OneNote. It came with my laptop on Windows 8 and I accidentally discovered that while I can't view the report as a pdf, I can view it in OneNote and that has the same effect. This isn't the prettiest report but it gives you an idea of what you can do. You'll see the Address and Subdivision fields from the report that are the column headings here. ***Note that the DataFields in the Inspector should use the same name as the original fields. The CSV report I used named the fields, "Address" and "Subd" and those are the names in the DataFields. In the text property in the Inspector, you can enter whatever text you want.
You can see how I put all the code together in GitHub. If anyone feels it would be useful, I will write down step-by-step what  I did. It didn't seem necessary as I wouldn't have really added anything to the sources cited above. So unless I hear otherwise, Happy Reporting!



What I love about Xojo - Part 2: GUIs, Multiplatform, Databases

In the first part, I loved the fact that Xojo's online book explained caching. So simple yet so important and often overlooked by instructors. It made me think that the people behind Xojo really thought things through in the development process.

The other reason Xojo(fka RealBasic, Real Studio) is so lovable is that it makes difficult things simple.

While I have a thing for C#, creating GUIs in Windows Forms is a major pain. With Xojo, you drag and drop. Seriously, that's it. Drag and drop and go to the Inspector to set values for the properties. Then you can actually work on your code instead of spending a ton of time coding for the GUI. Listbox? No problem! Labels? No biggie!

Then there is the real reason for Xojo's existence in the first place - multiplatform. Do I love Apple? Not really. Am I planning to learn Objective-C? No. I'm a Windows kind of girl. But now I want to create an app for a friend who happens to use a Mac. What's the answer? Xojo. Write it in Xojo and it can be used on Windows, Linux or Mac. (If I ever find time to finish said app, I'll write about that.)

Last, but not least, the database. It's a mistake to think you can become a real programmer without loving the database life. SQLite is part of Xojo and you can actually create the database in Xojo using the insert menu. Feel free to do it programmatically, too.

I think Xojo is a great business solution for companies/individuals who have limited resources in terms of being able to write code for multiple operating systems and maintain that code. You do have to pay for the license to deploy your app, but you can build it for free to see how you like Xojo. The license is not unreasonably priced either. Check it out here.