Here's your instructor's code, explained.
Code:
// String2: what is the output?
import javax.swing.JOptionPane;
public class String2 {
public static void main( String[] args ) {
String s1 = JOptionPane.showInputDialog("Type a word in capital letters: ");
int length = s1.length();
The length of the word the user entered is now held by variable "length".
Code:
int counter = 0;
char NextLetter;
Two variables are defined - one of them is initialized.
Code:
System.out.println("You entered: " + s1);
Echoing back to the user their word.
Code:
while (counter < length) {
The start of a loop. Loop will continue until variable "counter" is not less than variable "length". This will loop 5 times if the word is 5 characters.
Code:
NextLetter = s1.charAt(counter);
Initialize variable "NextLetter" with a value. The first time through the loop, since "counter" equals zero, the expression will evaluate to s1.charAt(0), or, in simpler terms, the first character of "s1". If the user entered "APPLE", then "NextLetter" will be "A".
This has the effect of adding 32 (decimal) to the value held in "NextLetter". A quick glance at an ASCII character chart tells us a decimal 32 (hex X'20') is a blank. It also tells us that an upper case "A" is a decimal 65, and a lower case "a" is a 97. So, if we add 65+32, we get 97. We can deduce that using the ASCII character set, that if you add 32 to an uppercase letter, we get a lowercase letter.
Code:
System.out.print(NextLetter);
counter++;
}
Echo the converted character to the user and bump the "counter" so the second time through the loop, counter==1, and we work with the first "P", etc.
Code:
System.out.print("\n");
System.exit( 0 );
} // end main
} // end of program
Now, you have just a few pieces of logic to add. First, you have to take into consideration that the user will not just be entering all capital letters. If you add 32 to a lowercase character, you will get garbage output. Looking at an ASCII chart, if you add 97 (lowercase "a") and 32, you get 129, which is a capital A-ring.
Also, you'll have to look for a blank. Blanks don't get converted.
Also, before you convert a letter to uppercase or lowercase, you should probably look to see that is does in fact need to be converted.
As an example, let's say that the user enters:
Coding is fun
If the assignment is to capitalize the first letter of each word, and you subtract 32 from the first letter (because you add 32 to go from uppercase to lowercase, it follows that you would subtract 32 to go from lowercase to uppercase, right?) "C", then that would be a bug in your program, because 67-32 is 35, and you could get a hash mark (#). So, since "C" is already a capital letter, leave it be.
On the next iteration of your loop, since it is not the first letter of the entire string, and it does not follow a blank, it should be converted to lowercase if it is not already lowercase.
Skip ahead to the blank. Blanks don't get converted, but they do tell us to remember to capitalize the next non-blank character.
Next iteration, you get an "i". Since the last character was a blank, if the letter is not already capitalized, it needs to be converted. And, since this character is not a blank, we have to turn off our flag that the last character was a blank before we loop again.
That's pretty much it. Try filling in the syntax to see what you get.
Todd