|
Strings in Java
An Introduction |
|
Prof. David Bernstein |
| Computer Science Department |
| bernstdh@jmu.edu |
String Objects:
String objects are immutable (i.e.,
they can't be changed)toLowerCase()) but the result, which is another
String, is always assigned to a variable
or passed as an actual parameter
String course;
course = new String("CS149");
course.toLowerCase();
String course;
course = new String("CS149");
course = course.toLowerCase();
+
String object is created
String course, number;
course = new String("CS");
number = new String("149");
// This line won't compile because it isn't a statement
course + number;
String course, number;
course = new String("CS");
number = new String("149");
course += number; // course = course + number;
+ operator
need not be a String if it can be converted into
one
int number;
String course, dept;
dept = new String("CS");
number = 149;
course = dept + number;
+ is also used as the "positive"
operator and the addition operator, it is easy to make
mistakesString s; s = "Value: "; s += 'a' + 'b'; JMUConsole.println(s);The output will be
Value: 195 because
'a' + 'b'
is evaluated first and evalutes to the sum of the ASCII values
String.format() which behaves just like
printf() but returns a String rather
than printing it
String s;
s = String.format("Value: %c%c", 'a', 'b');
JMUConsole.println(s);
The output will be Value: ab
new Operator?new operator creates (i.e., allocates memory
for an initializes) an objectString variables before
without using new
String
object for each String literalString s = "CS"; assigns the reference to the
literal to the variable s
String t = new String("CS"); creates a
String object that contains the characters
'C' and 'S' and assigns the reference
to that object to the variable t
new Operator? (cont.)new operator you should
use it (even though it's a little inconvenient) because it
will keep you from making some subtle mistakesString literals because
it is convenientString MethodsString Methods (cont.)
/**
* Count the number of vowels in a String
*
* @author Prof. David Bernstein, James Madison University
* @version 1.0
*/
public class VowelCounter
{
/**
* The entry point
*
* @param args The command-line arguments
*/
public static void main(String[] args)
{
char letter;
int n, vowels;
String line;
JMUConsole.open();
// Prompt for and read the text
JMUConsole.printf("Enter the text: ");
line = JMUConsole.readLine();
// Get the length of the String (in characters)
n = line.length();
// Initialize the accumulator
vowels = 0;
// Loop through the String
for (int i=0; i<n; i++)
{
// Get the next letter
letter = line.charAt(i);
// Check if it's a vowel (this can be done more efficiently)
if ((letter == 'a') || (letter == 'e') || (letter == 'i') ||
(letter == 'o') || (letter == 'u') ||
(letter == 'A') || (letter == 'E') || (letter == 'I') ||
(letter == 'O') || (letter == 'U') )
{
vowels++;
}
}
// Print the answer
JMUConsole.printf("Vowels: %d\n", vowels);
JMUConsole.close();
}
}
/**
* Convert a "number" in hexadecimal to a number in base 10
*
* @author Prof. David Bernstein, James Madison University
* @version 1.0
*/
public class HexConverter
{
/**
* The entry point
*
* @param args The command-line arguments
*/
public static void main(String[] args)
{
char digit;
int i, length, number, power, value;
String hex;
JMUConsole.open();
// Read the hexadecimal number
JMUConsole.printf("Enter the number in base 16: ");
hex = JMUConsole.readLine();
// Clean-up the hex number (Note that these methods
// cannot be void since String objects are immutable.)
hex = hex.trim();
hex = hex.toUpperCase();
// Initialization
length = hex.length();
number = 0;
power = 1;
// Process each character (This can be done more efficiently)
for (i=length-1; i>=0; i--)
{
digit = hex.charAt(i);
value = Character.digit(digit, 16);
number = number + (value * power);
power = power * 16;
}
JMUConsole.printf("Base 10: %d\n", number);
JMUConsole.close();
}
}