StringDemo
String class is used to store text. System.out.prinln(“\nThe length of “+greeting1+” is “+greeting1.length()); is used to output the length of the text stored in String greeting1.
There is one other way to achieve the same purpose: int len=greeting2/length(); System.out.println(len).If we want to convert all letters in the String to uppercase, we write: System.out.println(greeting2Upper).
int comma = invertedName.indexof(‘,’) is used to find the first comma “,” in the inverted name (Lincoln, Abraham) and record its index.
String lastname = invertedName.substring(0,comma) is used to output all letters from index 0 to comma (index 6) in invertedName.
ForDemo
DoWhileDemo
The above DoWhileDemo and ForDemo have the same result. We use For statement when we know how many time we want to repeat while we use DoWhile statement when we do NOT know.
EnhancedForLoop
ArrayDemo
(line 17) String[] staffNames = new String[staff] I created an String array called staffNames which has a length of the number of staff that I inputed. I added an for loop. In the array staffNames, when the index is 0 which is less than staff number, I input the first staff’s name, then i++. Then i=1, i=2…
System.out.println(staffNames[i]) asked the program to output all the String stored in array staffNames.
CityNames
if(option.chatAt(0) == ‘D’ || option.chatAt(0) == ‘d’) 0 represents the zeroth index number, if any text in Array cityNames have a ‘D’ or ‘d’ in the zeroth index number, it will be output. chatAt() is a method.
MaxandMin