DOWNLOAD THE CODE: StringValueOfPkg
Why use this function?
You want to treat a number like a String.
String.valueOf()
The String class has a function call valueOf(). This function will return the String representation of all the numeric data types (int, double, boolean, char, etc).
In the example below, two numbers are stored as int. The first print statement attempts to print each int as if they were strings. In this case, we would be hoping to see 100200 printed to screen. Notice that the concatenation operator is used - which, if you place between 2 numbers, becomes the addition operator.
The second print statement uses the String.valueOf() function. The function, as it is being used here, will return the String representation of each int.
The output for each statement is very different. The first print statement has caused the two int variables to be added (addition) together, whereas the second print statement has caused each int to be printed as a String.
Also see:
"Java - String valueOf() Method" ( http://bit.ly/YSRK2u )
Here's a cat:
Showing posts with label string. Show all posts
Showing posts with label string. Show all posts
Tuesday, February 26, 2013
Monday, February 25, 2013
What is a variable?
Variables are used to store values in a program. A value might be a name, a number, an address, and so on. These values can then be displayed or processed to create new information.
Think about a calculator. Each time you press a button, the calculator remembers what you have pressed. When you press the equals button, the calculator forms a mathematical equation using your series of key presses.
There are many types of values. Some values are made up of letters and numbers (such as an address) and some values are simply numbers. Each type of data can be stored in a program using a variable.
For instance, in a Java program, we could do the following:
String userName = “C2012000”;
String password = “monkey5000”;
String address = “30 Happy Place, Uptown”;
int numberOfLogins = 12;
The words String and int are types of Java variables - or data types.
Now we have 4 stored values that can be used again later in the program. Perhaps we need to print these values to the screen or perhaps we need to know where a letter must be mailed to. Perhaps we need to take an average of how many times a user has logged in over a particular time frame.
Alphanumeric & Numeric data.
Alphanumeric data is any collection of keyboard characters. It may contain letters, numbers & other special characters. Values such as these are stored in an variable that can hold alphanumeric values. This type of variable is called String.
String whatTheDogSaid = “Right on partner number 2!”;
A String may contain numbers, such as a house number in an address. But if we need to store a number so that we can do some kind of maths on that number, we need to use a variable type that can store numbers as numbers.
Numeric data are numbers. Generally speaking, data is considered numeric where it is being used for maths purposes - such as counting, adding, subtracting, and so on. Some numbers are whole numbers, such as the number of students on a bus. Some numbers contain a floating point, such as currency values, or body weight. For this reason, there is more than 1 type of variable that can hold numeric data. For instance, an int variable can store a whole number and a Double variable can store a floating point number.
int numOfStudentsOnBus = 22;
double dogWeight = 12.2;
The number of students on a bus is going to be stored as a whole number, so we store it in a variable that can store whole numbers, such as int.
A number representing weight contains a floating point so we store this number in a variable type that can store floating point numbers such as double.
There are many data types in the Java language!
Here's a cat:
Labels:
cat,
Data Types,
floating-point,
int,
string,
Variables,
whole number
Wednesday, September 19, 2012
Sunday, September 9, 2012
Concatenating adjacent numbers
Where a + symbol has a String on one or either sides, the + symbol is used as the concatenation operator. But where a + symbol has a number on either side, the + symbol is used as the addition operator.
Need to concatenate 2 adjacent numbers? Return the String value of one of the numbers in the following way:
Need to concatenate 2 adjacent numbers? Return the String value of one of the numbers in the following way:
Wednesday, September 5, 2012
Comparing String using the .equals method
Two String objects may be compared for equality by using the .equals function.
The equals method returns True or False (boolean).
String one = "Apples";
String two = "Oranges";
if (one.equals(two)) {
System.out.println("The strings match.");
}
else {
System.out.println("The strings DO NOT match.");
}
In this example, the equals method would return false, causing the statement "The strings DO NOT match" to be printed to screen.
The .equals function is available in all objects and Strings.
If you need to compare two primitive types such as int, read here....
Monday, June 11, 2012
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:
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
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:
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 )
Wednesday, May 23, 2012
The split function
DOWNLOAD THE CODE: usingSplit.java.zip
Example 1: split a string using a space as the delimiter:
Output:
Example 2: split a string using a comma (,) as the delimiter:
Output:
Sunday, April 29, 2012
Convert String to int
You may convert a String to an int, provided the String contains number only.
The following example will work in cases where an int number is contained in the string called thisString:
String thisString = "100";
int x;
x = Integer.parseInt(thisString);
The following example will work in cases where an int number is contained in the string called thisString:
String thisString = "100";
int x;
x = Integer.parseInt(thisString);
Tuesday, March 27, 2012
A handful of data types & variables
String
Any collection of keyboard characters.
Boolean
A true or false value.
Char
A single keyboard character.
int
A whole number between -2,147,483,648 and 2,147,484,647
Any collection of keyboard characters.
Boolean
A true or false value.
Char
A single keyboard character.
int
A whole number between -2,147,483,648 and 2,147,484,647
Sunday, March 18, 2012
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:
System.out.print(String)
DOWNLOAD THE CODE: PrintToConsolePkg
We can use a System.out.print(String) to display a prompt
Screen shot:
and the output:
We can use a System.out.print(String) to display a prompt
such as "Input a number : "
If we want to make sure that the line is terminated we can use:
Screen shot:
and the output:
Default value of primitive data types
Labels:
boolean,
byte,
char,
Data Types,
Default values,
double,
float,
int,
long,
Primitive data types,
short,
string
Subscribe to:
Posts (Atom)



















