Monday, June 11, 2012

File class

The Scanner & PrintWriter classes can read from & write to text files, respectively.

Each of these classes contains a constructor that accepts a File object.

The File class belongs to the java.io library (io stands for Input Output).

The example below declares a File object that references the io library directly. Notice that a String containing a filepath/filename is passed to the file's constructor. This File object called file, is then passed to the constructor of the Scanner object. In essence we are saying, read from this file at this location.


The next example declares a File object using a shorter declaration. The library reference has been done via an imports statement at the top of the code (not shown). Once again a String filepath/filename has been passed to the constructor. The file object is then passed to the constructor of the PrintWriter object. In short, we are saying, write (print) to this file at this location.


"File (Java)" ( http://bit.ly/JWg0sp )

"java.io (Java Platform SE 6)" ( http://bit.ly/JWhUcw )

String trim()

DOWNLOAD THE CODE: TrimmingWhitespace

The Java Character class defines whitespace as one of a few different types of characters, including the space character & a carriage return character.

The String class contains a trim() function which removes leading & trailing (at the beginning and at the end) whitespace.

For example, if we are reading a text file using Scanner, with a semi-colon as the delimiter, we only want to read the meaningful part of the string. Carriage returns should be discarded.

For our examples, we will use the following text file nums.txt. See screenshot below:



The code example below reads nums.txt, inside of a while loop.The semi-colon is used as the Scanner's delimiter. Each token in the string ends with a semi-colon and the next begins immediately after the semi-colon. This means that any carriage return characters may be included in the token. This is not ideal and will produce the following result:


Here is the output:


Note that the carriage return separating the lines between the tokens "five" and "six" has been read and rendered.

Using trim()
In the example below, we will trim() each token so that any whitespace such as carriage return characters are discarded.


Here is the output:




Sunday, June 3, 2012

Scope

A variable may only be referenced from within the region where it is declared.

"Variable Scope : Variable Scope « Language « Java Tutorial" ( http://bit.ly/KHplZ5 )

the double data type

A double is a numeric data type. Suitable for storing floating point (decimal) numbers such as weight, length, etc. Any kind of number with a "point something' in it such as 7.6, 10.5, 1.2, etc.

Where a double has not been assigned a value, by default it's value is 0.0d.

The format for declaring a double variable is:

   Type identifier;

Where the Type is double and the identifier is weight:

   double weight;

...and where a value of 100.50 has been assigned:

   double weight = 100.50;


You can declare a double in one statement and assign it a value in another statement:

   double weight;
   // other statements...
   weight = 100.50;



See also:

"Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)" ( http://bit.ly/xesQuj )
"Java For Complete Beginners - Double variables" ( http://bit.ly/ZzYTJ9 )

if then else statements

DOWNLOAD THE CODE: UsingIfThenElse.zip

<<< back to if statements

In the previous post on if statements, we saw how we could use an if statement to detect if a condition was true and then execute statements accordingly. An if-then-else statement allows us to branch off in multiple directions.

The example below determines if the user has entered an age that is equal to or greater than 18. If this condition evaluates to true, the user receives the message "You are 18 or over". If the age is anything else, the user receives the message "You are less than 18".



Here's the output:




In the next example, we have multiple if's to determine the entered age:


Note that each if section is contained within curly braces.

Here is the output:





also see:
also see:
"The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)" ( http://bit.ly/zflbUx )

if statements


Is used for implementing selection algorithms. Executes a certain code section only if a condition is true.

Format:

if (expression) {
   'Statements.
}

The expression to be evaluated is placed within parenthesis. Curly braces are used to open a block, immediately after the expression. If the result of evaluating the expression is true, all statements within the curly braces will execute. If the expression returns false, the statements within the curly braces will be skipped.

An expression is a combination of literal values or variables, and operators.

In the example below, the user is asked to enter a number. The if statement determines if the number is less than (less than operator < ) 100 and prints a message to screen, if it is. It then goes on to ask the user to enter their name. The if statement determines if the name has a length (String length) of more than 5 characters and prints a message to screen if it does.


Note that the if statement opens a code block with curly braces.

To recap, if the user enters a number that is less than 100, they will get the message "The number you entered was less than 100" and if the user enters a name that has a length greater than 5, they will get a message saying "The length of your name is longer than 5 characters long".

Here is the output:




Evaluating multiple expressions in an if statement

We can use the logical AND operator && to evaluate multiple expressions.


In this example, if the user enters a number that is greater than 50 AND less than 100, they will get the message "The number you entered was greater than 50 AND less than 100" and if the user enters a name that has a length greater than 5 AND less than 20, they will get a message saying "The length of your name is longer than 5 characters long AND shorter than 20".





also see:
"The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)" ( http://bit.ly/zflbUx )




Here's a cat...







The concatenation operator

DOWNLOAD THE CODE: ConcatPkg

The concatenation operator is the addition symbol +.

Concatenation is the act of joining multiple values together to form a single String.


Here's the output:


Note! The addition symbol is also used as the addition operator! If the addition symbol is placed between 2 numbers, it will treat the expression as an addition expression!

Saturday, June 2, 2012

Escape Sequences

From "Characters (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)" ( http://bit.ly/LUgCyw )


Switch

CODE DOWNLOAD: UsingSwitch.zip


A Switch can be used to evaluate a number of numeric types such as byte, short, char & int.

Let's say you want to check the value of a single variable and based on this value, one of many things may happen.

In the example below, the user enters a number. This number is then checked by the switch. The first case to match, executes a statement - in this case, printing a message to let the user know what the entered value was.


If the user entered a value not listed in the cases, the default section executes. It is good practice to always include a default case in your switch.

Notice that the final statement in each case is break. This causes the switch to stop checking down the list.

We can also execute a common statement for multiple cases, as in the example below:


Of course, we could use a number of if then else statements to achieve the same result, however, as we are only checking the value of a single variable, the switch is a more elegant and readable solution (subjective).

Here's a cat:




"The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)" ( http://bit.ly/y2TVYE )

int data type



An int is a numeric data type

An int is a whole number between -2,147,483,648 and 2,147,484,647 (a pretty big number). The number cannot contain a floating point (no decimal).

Where an int has not been assigned a value, by default it's value is 0.


The format for declaring an int variable is:

   Type identifier;

Where the Type is int and the identifier is maxDamage:


   int maxDamage;

...and where a value of 100 has been assigned:

   int maxDamage = 100;


You can declare an int in one statement and assign it a value in another statement:

   int maxDamage;
   // other statements...
   maxDamage = 100;



See also:

"Java For Complete Beginners - Variables: the int type" ( http://bit.ly/ZA1aE8 )