Java: Strings

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

How do you use the decimal format method to set the amount of decimals you want?

DecimalFormat money1 = new DecimalFormat("0.00"); String s1 = money1.format (total mullah);

String (String x)

Builds a copy of string x

String x = ""

Builds an empty string

What does the method Integer.toString(int n) do?

Converts and integer n to a string object

\n

newline

String()

builds an empty string

How is the compareTo method used with strings?

stringName.compareTo(otherString); returns: 0 if strings are equal 1 if stringName is greater -x if stringName is less

How is the equals method used?

stringName.equals(otherStringName); returns true if equal false otherwise

StringBuffer objects are ____________?

Immutable (None of its methods can change the string. No danger)

How can you convert strings to integers or doubles?

Integer.parseInt(s1) Double.parseDouble(s2)

What does string.lastIndexOf('x', y) do?

It begins searching backwards from position y in the string. Remember, the position is still numbered from the beginning not the back!

What does string.lastIndexOf('x') do?

It begins searching for the character x from the end of the string. Remember, the position is still numbered from the beginning not the back!

What does string.indexOf('x',y) do?

It begins searching the string fox the character x, beginning at position y. Remember to put single quotes around the character!

What does String result = s1.concat(s2) do? Alternatively what else can you do?

It concatenates strings s1 and s2. Alternatively, you could say: String result = s1 + s2 or String result = s1 result += s2

What does the method s1.intValue do?

It converts s1 to an integer value

What does String.valueOf(x) do?

It converts x of either integer or double type to a string

What does the string.indexOf('x') method do?

It finds the first position of the character x in the string. Remember to put single quotes around the character!

What does s1 = s1.trim() do?

It removes all the white space (i.e. blank, new line).

What does s1.compareTo(s2) do?

It returns the difference s1-s2. Uppercase < Lowercase If s1 is smaller, returns a negative number. If s1 is larger, it returns a positive number.

What does the int length() method do?

It returns the number of characters in the string

What does s1 = s1.replace('x','y') do?

It replaces all instances of the character x with the character y. You can also use strings instead of characters.

What does the character class do?

It represents characters as objects. The methods are boolean and take one parameter (char) and return true or false.

Whad does the string.charAt(k) method do?

It returns the character at the kth position in the string

How does java handle literal strings?

Literal strings are just references to String objects

Can you use relational operators (==, !=, <, >, etc.) to compare strings?

No you cannot. This is because relational operators compare references not values.

Can you Concatenate two objects that are not strings? int x = 3; int y = 4 String s = x + y;

No. At least one of the objects must be a string. i.e. Instead you could do int x = 3; int y = 4 String s = x.toString() + y.toString;

What happens if you call a method for a reference that is equal to null? (private String errorMsg;)

NullPointerException

\\

backslash

\"

double quote

What is the method to obtain a character in a string?

string.charAt(k); k starts from zero

What are the character methods?

isLetter, isLetterOrDigit, isUpperCase, isLowerCase, isWhitespace. Other than those boolean methods, there are toUpperCase and toLowerCase that sets scharacters. i.e. char c1 = Character.toUpperCase('a'); // c1 is set to 'a'

The String class belongs to what java package?

java.lang

What is the default field of the String type?

null

What are some unique StringBuffer methods?

setCharAt, append(to hold a string), insert, deleteCharAt, delete.

\'

single quote

Should you use == to compare two strings?

No the relational operator only compares the objects and not the contents.

Remember to put strings in quotes

String s = "John";

When is the "NumberFormatException" error thrown?

When a Integer.parseInt(s1) or Double.parseDouble(s2) s1 has to be a integer s2 has to be a real number(anything neg or positive)

What is the method to obtain the length of a string?

string.length();

What is the replace and replaceFirst method?

string.replace('x', 'y'); string.replace("x", "y"); string.replaceFirst("x", "y"); replace switches out all x characters/substrings with y characters/substrings. replaceFirst only replaces the first occurrence of x substring with y.

What does the substring method do?

string.substring(x, y); Used to obtain a portion of a string. Starts from position x and ends at position[y-1]. Remember that it does not end at y! string.substring(i) Returns the substring from the i-th position to the end of the string

What is are the methods to convert strings to uppercase and lowercase?

string.toUpperCase(); and string.toLowerCase();

What does the trim method do?

string.trim(); removes whitespace characters in front of and after the string


Set pelajaran terkait

vv) Chapter 15 Brooker - using chapter 13 Campbell, chapters 10 & 13 Mader and others

View Set

Chapter 20 Earth and space 11th grade

View Set