# Answer to Question #6635 in Java | JSP | JSF for Josh Rider

Question #6635
Problem Description: 1. Prompt the user to enter a whole number from 1 to 9 inclusive. The number zero (0) will be used to exit the application. If any other number is entered, then an error message shall be displayed and then the prompt to enter a whole number will be re-displayed. 2. Create a pyramid structure by using only the odd numbers without going over the number entered in by the user. For example: If the user entered in 8, then construct your pyramid using the numbers 1, 3, 5, and 7. 3. The pyramid shall be constructing by repeating the odd number the same number of times as its value. For example: The number 1 will be repeated only once. The number 3 will be repeated three times. The number 5 will be repeated five times. 4. The pyramid shall be centered accordingly, with the highest odd number being the most left align. This will require you to calculate the center of the pyramid based upon the highest odd number. The number one (1) will always be in the center
import java.util.Scanner;

public class Pyramid {

private static Scanner keyboard = new Scanner(System.in);

/**

* function makes center alignment

* @param data is String for alignment

* @param length is length of the String in text.

* @return formatted String

*/

private String formatCenter(String data, int length){

StringBuilder res = new StringBuilder();

for(int i = 0; i < (length - data.length())/2; i++){

res = res.append(' ');

}

res.append(data);

for(int i = res.length(); i <= length; i++){

res.append(' ');

}

return new String(res);

}

public void main(String [] args){

String userInput;

StringBuilder output = new StringBuilder();

int length = 50;

int n;

while(true){

System.out.println("Input number from 1 to 9 inlusive, please.
"

+ "If you want to quit, input 0.");

userInput = keyboard.next();

try{

//get int value

n = Integer.parseInt(userInput);

if (n == 0){

//exit

System.out.println("Goodbye!");

return;

} else if ( (n >= 1) && (n <= 9)){

int max;

//max value in the pyramid

if (n % 2 == 0) {

max = n - 1;

} else {

max = n;

}

//formation of the line

for(int i = max; i >= 1; i -= 2){

output.delete(0, output.length());

for(int j = max; j >= i; j -= 2 ) {

output = output.append(j);

output = output.append(' ');

}

for(int j = i+2 ; j <= max; j += 2 ) {

output = output.append(j);

output = output.append(' ');

}

System.out.println(formatCenter(new String(output), length));

}

} else {

System.out.println("ERROR: Not appropriate value!");

}

} catch (NumberFormatException e){

System.out.println("ERROR: Not a decimal number!");

}

}

}

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Josh
17.02.12, 16:43

When I run in JCreator I get the following error:

java.lang.NoSuchMethodError: main