Here is the above code explained with comments.
// is a one line comment
/*
* multiple line comment
*/
This is fully functional; type code(if you use the code dont just copy and paste; you will learn alot more), compile and run.
// import scanner - used for input
import java.util.Scanner;
// a class
public class NewClass {
// This is the main method needed to run program
public static void main(String[] arrrgss) {
// instanciate Scanner for user input
Scanner input = new Scanner(System.in);
// ask the user to enter name
System.out.print("Enter a name: ");
//store the input into name using scanner
String name = input.next();
/*
* Xdawn90s method
* 1.He takes the length of name to be the starting index,
* then says as long as the index is greater than 0 keep running,
* for every time loop decrement the index(i)
*/
for (int i = name.length(); i >= 0; i--) {
// print name from 0 to index
System.out.println(name.substring(0, i));
}// end of for loop
/*
* wim DC method
* The first for loop does the same as xdawns loop
* but with a twist...
*/
for (int i = name.length(); i > 0; i--) {
/*
* ...wim DC uses a second for loop to add the characters
* one letter at a time by using j to index the char in string,
* as long as j less than index i(prevents exception),
* but increment the next char before exiting the loop
*/
for (int j = 0; j < i; j++) {
// print the letter at "charAt(0)"-"charAt(1)"-...
System.out.print(name.charAt(j));
// this prevents the - from being put at last letter
if (j != i - 1) {
System.out.print("-");
}// end of if
}// end of inner for loop
// this is just to go to next line
System.out.println("");
}// end of outer for loop
/*
* nicckks' method
* This is similar to Xdawns method, only in reverse order
*/
for (int i = 0; i < name.length(); i++) {
System.out.println(name.substring(i));
}// end of for loop
}// end of main method
}// end of NewClass
Hope this explains the previously written code and aides you in learning.
Edited by mr mike, 05 January 2011 - 06:41 PM.
Added whitespace in code