Section 5f

Mathematics with Strings (kind of)


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Concatenation

This might be another topic you wouldn't expect. You actually don't ever do math with Strings but there is an activity you can conduct with Strings that looks like arithmetic. The process is called concatenation. Strings can add other strings using the plus ('+) sign, and in Java, they can add other data types as well. Integers, doubles, characters, and more can all be "added" to a given String.

Consider the following code from the previous video in Chapter 5e.

// display A grades
char ltrGrade = ‘A’;
conIO.printChar( ltrGrade );
conIO.printString( ", " );
conIO.printInt( numAStudents );
conIO.printEndline();
ltrGrade = ltrGrade + 1;

// display B grades
conIO.printChar( ltrGrade );
conIO.printString( ", " );
conIO.printInt( numBStudents );
conIO.printEndline();
ltrGrade = ltrGrade + 1;

// display C grades
conIO.printChar( ltrGrade );
conIO.printString( ", " );
conIO.printInt( numCStudents );
conIO.printEndline();
ltrGrade = ltrGrade + 1;

This can be changed to the following.

// display A grades
char ltrGrade = ‘A’;
conIO.printString( ltrGrade + ", " + numAStudents );
conIO.printEndline();
ltrGrade = ltrGrade + 1;

// display B grades
conIO.printString( ltrGrade + ", " + numBStudents );
conIO.printEndline();
ltrGrade = ltrGrade + 1;

// display C grades
conIO.printString( ltrGrade + ", " + numCStudents );
conIO.printEndline();
ltrGrade = ltrGrade + 1;

The important point about conducting String concatenation is that somewhere in the expression, there must be a String quantity. In the above displays, the comma and space part of the expression is a String so this worked out. However, in some cases, you might just be displaying a number such as an integer. With the Console_IO_Class tools you can just printInt, however in other String operations, such as using System.out.print, you will be required to add a String quantity to make it work. Here is an example:

// display a number
int someValue = 45;
System.out.print( "" + someValue );

The pair of double quotes is accepted as a String even though it is empty, so Java will accept this expression as a String, and the output will be correct. Here is another example from the previous page and video:

// Show the number in the alphabet
// method: printString, printChar, printEndline
conIO.printString( "The number for the letter \'" );
conIO.printChar( letter );
conIO.printString( "\' in the alphabet is " );
conIO.printInt( numberInAlphabet );
conIO.printChar( PERIOD );
conIO.printEndline();

The above code can be changed to:

// Show the number in the alphabet
// method: printString, printChar, printEndline
conIO.printString( "The number for the letter \'" + letter
+ "\' in the alphabet is " + numberInAlphabet + PERIOD );
conIO.printEndline();

Because there are two String quantities in this expression, Java will accept all of the items and concatenate them into one String. Pretty handy, huh? Also note that Java allows programmers to extend code and parameters on to subsequent lines as was demonstrated above. The thing that cannot be extended across lines is a String quantity itself (i.e., the stuff between the double quotes ('"..."").

Character and String "arithmetic" is kind of unique but it can be helpful in a variety of ways while you develop programs. And it's easy. Take advantage.