Monday, March 19, 2012
Java syntax cheat cheat
"Java Syntax Cheat Sheet" ( http://bit.ly/y7RduJ )
The Static keyword
The static keyword is a Java modifier which associates a class member or method with a class rather than an instance of a class.
For instance, the main method of a class is always static as there must be exactly one instance of the class containing main in order for the compiler to find it.
A class containing static members or methods does not require instantiation in order to use the static methods of the class.
For instance, the main method of a class is always static as there must be exactly one instance of the class containing main in order for the compiler to find it.
A class containing static members or methods does not require instantiation in order to use the static methods of the class.
Sunday, March 18, 2012
Scanner class - Part 1
DOWNLOAD THE CODE: Scanner.zip
The Scanner class has methods for reading a number of different types of data such as String, int, Double & Boolean.
By default, the Scanner class uses whitespace to delineate tokens (lines, words, phrases, values, etc).
We can use the Scanner class to read input from keyboard input or from txt files.
Use Scanner to read keyboard input
The example below uses the Scanner class to read keyboard input. Scanner contains several methods for reading different types of data. In this example, the next() & nextInt() methods are used to read the input String and the input int, respectively.
Using Scanner to read from a text file
The following shoppinglist.txt file contains a number of shopping items. Each item is followed by a number representing the quantity required.
The final token in this example is the character 1.
By default, the Scanner's next() method is using the whitespace separating the characters in the file, to delineate is string. As such, any string separated by whitespace, is being read in each pass of the loop. The statement input.next() reads the next token in the file.
Let's change the code a little to get these values onto the same line.
The while loop body now looks like this:
"Scanner (Java Platform SE 7 )" ( http://bit.ly/ABSuGl )
Simple tutorial using the Scanner class: "Java For Complete Beginners - user input" ( http://bit.ly/Lkr7uN )
"Scanning (The Java™ Tutorials > Essential Classes > Basic I/O)" ( http://bit.ly/LkD03X )
"File (Java Platform SE 7 )" ( http://bit.ly/Jxdc9T )
"The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)" ( http://bit.ly/LkGjIm )
The Scanner class has methods for reading a number of different types of data such as String, int, Double & Boolean.
By default, the Scanner class uses whitespace to delineate tokens (lines, words, phrases, values, etc).
We can use the Scanner class to read input from keyboard input or from txt files.
Use Scanner to read keyboard input
The example below uses the Scanner class to read keyboard input. Scanner contains several methods for reading different types of data. In this example, the next() & nextInt() methods are used to read the input String and the input int, respectively.
The output:
Using Scanner to read from a text file
The following shoppinglist.txt file contains a number of shopping items. Each item is followed by a number representing the quantity required.
Declare an instance of File and then declare an instance of the Scanner class. Pass the file instance to the scanner instance.
The Scanner class contains a number of Boolean functions to determine whether there is another token of a specified type. The example below uses a while loop in conjunction with the Scanner's hasNext() method of Scanner to determine if another token of any type exists. In effect, the loop has been primed to 'continue looping while there are more tokens to read'. When there are no more toekn to read, the loop will stop.
The output:
The final token in this example is the character 1.
By default, the Scanner's next() method is using the whitespace separating the characters in the file, to delineate is string. As such, any string separated by whitespace, is being read in each pass of the loop. The statement input.next() reads the next token in the file.
Let's change the code a little to get these values onto the same line.
The while loop body now looks like this:
The first call to next() reads in the name of the shopping item and is printed to screen via the print() method. The second call to next() is used to read in the quantity is appended to the next line via the println() method.
The output now looks like this:
Read on to learn about using a nominated deliminator character with the Scanner class.
"Scanner (Java Platform SE 7 )" ( http://bit.ly/ABSuGl )
Simple tutorial using the Scanner class: "Java For Complete Beginners - user input" ( http://bit.ly/Lkr7uN )
"Scanning (The Java™ Tutorials > Essential Classes > Basic I/O)" ( http://bit.ly/LkD03X )
"File (Java Platform SE 7 )" ( http://bit.ly/Jxdc9T )
"The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)" ( http://bit.ly/LkGjIm )
Labels:
delineator,
Demo Code Project,
hasNext(),
input,
IO,
Links,
next(),
nextInt(),
Read input,
Scanner,
text,
Text File,
Token
System.in.read()
The System.in.read() function returns an integer (whole number) representing the next character's unicode code. As such, it is suitable in situations where you only need to read the first byte of input.
Returns the 7-bit ASCII code of the next character.
The char data type
The
char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff'
"Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)" ( http://bit.ly/xesQuj )
The Java Tutorials
Great site to get you started
"Lesson: Language Basics (The Java™ Tutorials > Learning the Java Language)" ( http://bit.ly/FP6evY )
"Lesson: Language Basics (The Java™ Tutorials > Learning the Java Language)" ( http://bit.ly/FP6evY )
What is a String?
DOWNLOAD THE CODE: IntroToStringPkg.zip
A string is a collection of any keyboard characters such as "I love chocolate 100 times more than you !!!"
Where a String has not been assigned a value, by default it's value is null.
In the example below, 2 Strings are created (lines 1 and 2).
The 3rd statement concatenates (joins) the 2 Strings together (using the addition character).
The fourth statement returns the number of characters in the String called firstName, by calling the String length() function (all Strings have this function, it returns an int).
The fifth line finds the first character of the String by calling the String charAt() function (all Strings have this function, returns the character at the specified position).
The final statement concatenates all of the values together and prints to screen.
A variable, such as String, can be declared in one statement and assigned a value in another statement, as below:
String myString;
myString = "This is a String!";
...or it can be declared and assigned a value in a single statement:
String myString = "This is a String!";
Literal String values (the actual charcters) are always enclosed in double quotation marks.
See also: "Strings (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)" ( http://bit.ly/K4kbDz )
"String (computer science) - Wikipedia, the free encyclopedia" ( http://bit.ly/AgRVuH )
Here's a cat:
A string is a collection of any keyboard characters such as "I love chocolate 100 times more than you !!!"
Where a String has not been assigned a value, by default it's value is null.
In the example below, 2 Strings are created (lines 1 and 2).
The 3rd statement concatenates (joins) the 2 Strings together (using the addition character).
The fourth statement returns the number of characters in the String called firstName, by calling the String length() function (all Strings have this function, it returns an int).
The fifth line finds the first character of the String by calling the String charAt() function (all Strings have this function, returns the character at the specified position).
The final statement concatenates all of the values together and prints to screen.
The output:
A variable, such as String, can be declared in one statement and assigned a value in another statement, as below:
String myString;
myString = "This is a String!";
...or it can be declared and assigned a value in a single statement:
String myString = "This is a String!";
Literal String values (the actual charcters) are always enclosed in double quotation marks.
See also: "Strings (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)" ( http://bit.ly/K4kbDz )
"String (computer science) - Wikipedia, the free encyclopedia" ( http://bit.ly/AgRVuH )
Here's a cat:
Subscribe to:
Posts (Atom)








