Help: Working With Strings


Help is available for each task, or you can go straight to the solution source code.

Task 1

Create a new String, "hello world", using a String constructor and concatenation.
The argument to the String constructor should be the expression

"Hello" + " " + "World"

Or something equivalent.

Task 2

Print out a substring of the "hello world" string.
Use the substring method, with the first argument 0, and the second anything less than or equal to the string's length.

Task 3

Create another "Hello World" string using a StringBuffer and its append method. Print it to System.out.
Create a new StringBuffer variable, initialize it with a new StringBuffer. Use the StringBuffer method append to add the result string as separate pieces.

Task 4

Use concatenation to convert some integers and floating point numbers to a string for printing.
Remember that '+' refers to concatenation only if its first argument is a String. Otherwise, the Java compiler thinks that + is for addition. A trick to use + for concatenation, but still have a number at the beginning of the string, is to start the expression off with "" +.

Task 5

As in the notes, use Integer.parseInt to convert a String into an Integer, then print it to System.out.
Be sure that the String argument to parseInt is a legal integer, otherwise an exception will be raised.

Copyright © 1996-1997 MageLang Institute. All Rights Reserved.