Reputation: 41
Asking for a natural number n, I want to print to the console in this format:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
.
.
.
n . . . 5 4 3 2 1
Inputting 4, this is what I have so far:
1
21
321
4321
I want to add a space between the numbers. This is my code:
import java.util.Scanner;
public class PatternTwo {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int userInput;
System.out.println("Please enter a number 1...9 : ");
userInput = in.nextInt();
String s="";
int temp = userInput;
for(int i=1; i<=userInput; i++ ) {
for (int k= userInput; k>=i; k-- ) {
System.out.printf(" ");
}
for(int j =i; j>=1; j-- ) {
System.out.print(j);
}
System.out.println("");
}
}
}
Upvotes: 3
Views: 1399
Reputation: 16508
Another alternative way could be to separate the spacing for each line using printf
and using String.format
for each number.
String.format("%1$" + length + "s", inputString)
returns the inputString
if inputString.length
>= length
else the inputString padded with spaces so that the length equal to the given length.
Example:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int userInput;
System.out.println("Please enter a number 1...9 : ");// you can use this also for numbers > 9
userInput = in.nextInt();
int digits = String.valueOf(userInput).length()+1;
for(int i = 1; i<=userInput; i++){
System.out.printf("%1$"+(digits*(userInput-i+1))+"s","");
for(int k = userInput - (userInput-i); k >= 1; k--){
System.out.print(String.format("%1$"+digits+"s", k));
}
System.out.println();
}
}
Upvotes: 0
Reputation: 22472
Below is a modification of your answer that does not print unnecessary extra white space when k
equals i
by modifying your k for-loops exit condition and similarly when j
equals i
by dealing with that case separately.
The main overall change is that in your k-loop you need to print 2 spaces rather than 1 to achieve your desired right side alignment:
import java.util.Scanner;
class PatternTwo {
private static void printPatternTwo(int n) {
for (int i = 1; i <= n; i++) {
for (int k = n; k > i; k--) {
System.out.print(" ");
}
for (int j = i; j >= 1; j--) {
System.out.print(j == i ? j : " " + j);
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter an integer between 1 and 9 inclusive: ");
int userNum = -1;
while (scanner.hasNext()) {
if (scanner.hasNextInt()) {
userNum = scanner.nextInt();
if (userNum >= 1 && userNum <= 9) {
scanner.close();
break;
} else {
System.out.println("ERROR: Input number was not between 1 and 9");
System.out.print("Enter a single digit number: ");
}
} else {
System.out.println("ERROR: Invalid Input");
System.out.print("Please enter an integer between 1 and 9 inclusive: ");
scanner.next();
}
}
printPatternTwo(userNum);
}
}
Example Usage:
Please enter an integer between 1 and 9 inclusive: 12
ERROR: Input number was not between 1 and 9
Enter a single digit number: a
ERROR: Invalid Input
Please enter an integer between 1 and 9 inclusive: 5
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
Upvotes: 1
Reputation: 732
Add a space in front of the number to be printed and double the spaces above so that it is not a pyramid. Something like this:
import java.util.Scanner;
public class PatternTwo {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int userInput;
System.out.println("Please enter a number 1...9 : ");
userInput = in.nextInt();
String s="";
int temp = userInput;
for(int i=1; i<=userInput; i++ ) {
for (int k= userInput; k>i; k-- ) { // <- corrected condition
System.out.printf(" ");
}
for(int j = i; j>=1; j-- ) {
System.out.print(j);
// check if not 1 to avoid a trailing space
if (j != 1) {
System.out.print(" ");
}
}
System.out.println("");
}
}
}
EDIT
Thanks to /u/shash678 I corrected my solution to remove all unnecessary or wrong spaces
Upvotes: 4
Reputation: 1573
If you are comfortable with streams:
public class PatternTwo {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int userInput;
System.out.println("Please enter a number 1...9 : ");
userInput = in.nextInt();
String s = "";
int temp = userInput;
revRange(1, temp).forEach(n -> printNumbers(n, temp));
}
static IntStream revRange(int from, int to) {
return IntStream.range(from, to)
.map(i -> to - i + from - 1);
}
private static void printNumbers(int n, int max) {
IntStream.rangeClosed(0, n -1)
.forEach(num -> System.out.print(" "));
revRange(1, max - n)
.forEach(num -> System.out.print(num + " "));
System.out.println();
}
}
Upvotes: 0
Reputation: 51
How about a cleaner solution, which avoids using nested for-loops:
public static void main(final String[] args) {
final Scanner scanner = new Scanner(System.in);
System.out.print("Please enter a number 1...9 : ");
final int n = scanner.nextInt();
final ArrayList<String> suffixes = new ArrayList<>();
for (int i = 1; i <= n; i++) {
suffixes.add(0, i + " ");
final String prefix = String.join("", Collections.nCopies(n - i, " "));
final String suffix = String.join("", suffixes);
System.out.println(prefix + suffix);
}
}
Upvotes: 1
Reputation: 1568
When printing the number put a space in from of it.
// this works up n == 9
System.out.print(" " + j);
For n > 9 the issue is that you need to print the number and whitespaces on a fixed amount of space however the number of chars a number contains changes. A solution would be to use tab (or several if you want really big numbers).
// when printing blank spaces use a tab instead
for (int k= userInput; k>=i; k-- ) {
System.out.printf("\t");
}
// then print number with tab in front
for(int j =i; j>=1; j-- ) {
System.out.print("\t"+j);
}
Upvotes: 0