Java Console – Input

Ah the fun times of inputting data from the command line in a Java program.

Java has come a long way in this respect. From starting with IO Streams to what they have now. Currently, there is a Console class in the java.io package. Here’s a simple test I did capturing data the user enters:

import java.io.Console;
import java.io.IOException;

public class Test {

	public Test() {

	}

	public static void main(String[] args) throws IOException {
		String in = null;

		Console c = System.console();
		if (c == null) {
			System.err.println("No console.");
			System.exit(1);
		}

		while (!(in = c.readLine("> ")).equals(".quit")) {
			// do something with the content you just captured
			System.out.println(in);
		}
	}
}

That’s a very simple program now isn’t it? Just accepts whatever you enter and echos it back out to the screen. If you type in .quit it will quit the program.

So yep, just a simple program for capturing data in a Console application.

The For Loop

There are two types of for loops in Java. Your traditional loop:

for (int i = 0; i < 10; i++) {

}

Or the advanced loop that will loop through items in say an array:

for (String s : stringArray) {

}

Let's go through both of these examples and see what comes of it.

First, let's create a String Array for the loops to process.

String[] strArray = {"one", "two", "three"};

Simple enough. Just an array containing three values of one, two and three.

With the traditional for loop, you would loop through the array like this:

for (int i = 0; i < strArray.length; i++) {
    System.out.println(strArray[i]);
}

As you might have guessed, it outputs the three values:


One
Two
Three

Now, let's do the same thing, but using the other for loop. Sometimes this loop is called a For Each loop.

for (String s : strArray) {
    System.out.println(s);
}

Again we get the three values:


One
Two
Three

I prefer the new method myself. It comes in handy. But there are times that you'll want to use the traditional for loop, (for example if you want to run through a Multidensional Array).

Happy Coding!

String Utils

I read in a C++ book once that you should have your own String Utils class file at your disposal.

Of course there are several people who have created a String Utils class out there on ther internet. Apache StringUtils comes to mind. But why not have something that you call your own?

Having programmed in ColdFusion for a number of years, I find that the left and right functions have been very useful. Of course there is nothing of the sort in Java.

So, here is my attempt at writing such methods.

The premise is simple. You take a string and specify how many characters from the left or from the right you wish to get from the string.

public class StringUtils {

    public String left(String str, int num) {
        return str.substring(0, num);
    }

    public String right(String str, int num) {
        return str.substring(str.length() - num);
    }
}

Now of course there are two ways that you can have your StringUtils accessed. Either by calling the class and the methods, like above, or by having the methods be static. The choice is yours.

Hello World

Ah The first program that everyone tends to write when learning a new programming language is Hello World. So, what is Hello World? Hello World is a simple program that simplay outputs the words “Hello World”.

In Java we would write this as follows:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World");
    }

}

It’s that simple. If you haven’t guessed yet, here’s the output of the file:

Hello World

Java Html

Started writing a java program that would output HTML. Not sure why I did this exactly. But well it’s been kinda fun. At least I think it’s been kinda fun.

Not sure where I’ll use this small program. Perhaps somewhere in a … tools jar … something simple.

Yep something like that. I wonder what else I can throw in there… if I even want to use the whole HTML set of tags. One would imagine I should. Hmmm something to consider for sure.

VIM

Ah VIM… or VI. Whichever people prefer.

Attempting to use VIM for Java programming development. Yes VIM as an IDE.

So far not too bad.

My purpose for this?

Well I needed something lightweight to run on my netbook. So tada VIM to the rescue! Oh yes.

We’ll see how well it works out.

Pet Project

Been working on a small program for note taking. I call it CommandLine. Yeah I’m sure that’s nothing new and exciting.

It’s meant to be 100% keyboard interactive, you shouldn’t have to use your mouse. So far the project is working out rather nice. We’ll see how well it ends up… by which I mean how often I use it.

The commands are pretty simple. To list your files you simply type ‘ls’. To save a file you’re working on, you type ‘save’. To end the program you either type ‘quit’ or ‘exit’. If you just type the word today, it will create a file for today using year.dayofyear notation.

Like I said, it’s nothing really exciting. Just a little program I cooked up for kicks. I plan to implementing some kind of backup/archive feature plus having the ability to delete text files etc. all without having to touch the mouse.

We’ll see how well this goes.

Utils

So… been thinking. There are some things that I code over and over again… sometimes it’s a copy and paste type of thing. Why not make my own utils.jar file that I can just include in other projects?

Yeah… something like that. So I think I’ll be working on that next.

Right now I have three categories that come to mind.

  1. String
  2. Xml
  3. Text
I know one of the main things I tend to do is read in a file and then work with that content. Afterwards I save the output to a file. So yes, def will be creating methods/classes to read in files, and then export files. Should be an interesting project to work on.

ColdFusion Good Times

ColdFusion has come a long way since I first started using it back in 2003. I started using version 5.0. It was at that time owned by Macromedia.

Later on Macromedia was bought out by Adobe. I’m sure some were afraid that Adobe would cease production of ColdFusion, but they were good and played nice, keeping CF around.

ColdFusion is currently in version 9. Not sure when version 10 will be released, but I’m sure it will have some good stuff to go with it. The updates usually do.

Some things I find interesting about ColdFusion are the following:

  1. Rabid application development
  2. Integrate Java in with your pages, via cfc’s or scripting
  3. Works nicely and plays well with others
I’ll come up with a better list later, but right now those come to mind. I mean it better play nice with Java, it was re written using Java altogether.

Java Logging

So, I’ve been trying to figure out the best way to log stuff in my Java programs. I’ve heard all there is to hear about Log4j, which I might add I keep mispelling… so it can’t have that great of a title right?

I’ve also heared a lot about the built in logging utilities in Java that have been around since version 1.4.
For a little bit, I was attempting to create my own logging platform… for now I’ve tabled the sucker. I might get back to it one of these days, but for now… yeah one of the two options above will work out nicely.
Anywho… my next attempt is to figure out how to transform the outputted log file into either a database… or an xml file… or something. Not sure why I want to do this… probably a proof of concept type of thing. We’ll see…