1

This code asks the user for a triangle height and a letter.

import java.util.*;
public class triangle
{
    public static void main(String [] args)
    {
        Scanner kb = new Scanner(System.in);
        int size = 0;
        Character c;

        System.out.println("Enter height of the triangle : ");
        size = kb.nextInt();

        System.out.println("Which character you want to use : ");
        c = kb.next().charAt(0);

        int i, j, k;

        for (i = 0; i < size + 1; i++)
        {
            for (j = size; j > i; j--) 
            {
                System.out.print(" ");
            }
            for (k = 0; k < (2 * i - 1); k++) 
            {
                System.out.print(c);
            }
            System.out.println();
        }
    }
}

For a height of 3 and the letter b, current output is:

  b
 bbb
bbbbb

But it should be:

  b
 cbc
dcbcd

How can I print letters in the alphabet after the given letter in the triangle pattern shown above?

MultiplyByZer0
  • 6,302
  • 3
  • 32
  • 48
JK1
  • 33
  • 5
  • 1
    Might wanna think about what to do if the user inputs a `z` , and as a hint you should probably use `ascii` to get the letter after. – Adam Buchanan Smith Jun 04 '19 at 21:02
  • 1
    `System.out.print(c);` this is always going to print the letter the user inputted. If you need to print a different letter, you're going to have to do something different. – Roddy of the Frozen Peas Jun 04 '19 at 21:11

1 Answers1

2

Your problem

Let's talk about your variable c, which is a char. (Actually, c is a Character, the boxed equivalent of a char, but that doesn't matter too much.)

In Java, char is a number type. All chars are numbers. For example, the char literal 'a' is actually the number 97:

'a' == 97; // true

And you can do arithmetic with char, just as you can with regular numbers:

'a' + 1; // 98
'a' + 2; // 99
'a' + 3; // 100

The mapping between characters like 'a' and numbers like 97 in Java follows the universal standard Unicode, a superset of the older standard ASCII. In other words, 'a' == 97 because ASCII says so. Here's an ASCII table:

Both ASCII and Java's char were thoughtfully designed so that doing arithmetic with characters works exactly like you'd expect. Study these examples:

(char) ('a' + 1); // 'b'
(char) ('a' + 2); // 'c'
(char) ('a' + 3); // 'd'

As you see, in the last line, adding 3 to 'a' and then converting it back to a char produces 'd', which is 3 letters after 'a'.

So if you want x letters after some character c, the code is just (char) (c + x).

In your case, for each letter you print in the triangle, you want to increase the user's character (in your example, 'b') by the letter-to-print's distance from the central letter. The letter-to-print is in position k, and central letter is always in position i - 1; thus the distance is Math.abs(k - (i - 1)), or Math.abs(k - i + 1). That's how much to add to the character.

With this knowledge, it's simple to fix your code. Replace:

for (k = 0; k < (2 * i - 1); k++) 
{
    System.out.print(c);
}

with:

for (k = 0; k < (2 * i - 1); k++) 
{
    System.out.print((char) (c + Math.abs(k - i + 1)));
}

Other problems

While you're here, you may as well fix some other issues with your code:

  1. In Java, it's convention for classes to begin with a capital letter. This means you should change:

    public class triangle
    {
    

    to:

    public class Triangle
    {
    

    and rename triangle.java to Triangle.java.

  2. You don't need to declare variables at the top. It's better to declare them when you first need them, allowing you to combine the declaration and initial assignment. For instance, instead of:

    int size = 0;
    // ...
    size = kb.nextInt();
    

    You can simply write:

    int size = kb.nextInt();
    

    Here's your code, refactored to fix this:

    Scanner kb = new Scanner(System.in);
    
    System.out.println("Enter height of the triangle : ");
    int size = kb.nextInt();
    
    System.out.println("Which character you want to use : ");
    Character c = kb.next().charAt(0);
    
    for (int i = 0; i < size + 1; i++)
    {
        for (int j = size; j > i; j--)
        {
            System.out.print(" ");
        }
        for (int k = 0; k < (2 * i - 1); k++)
        {
            System.out.print((char) (c + Math.abs(k - i + 1)));
        }
        System.out.println();
    }
    
  3. You should use char, not Character, for c. Character should only be used if you need to deal with boxing and unboxing (e.g. you want to define an ArrayList<Character>), which has nothing to do with your current problem. So replace:

    Character c = kb.next().charAt(0);
    

    with:

    char c = kb.next().charAt(0);
    
  4. Use System.out.print() for prompts. It allows the user to type the answer on the same line. Instead of:

    System.out.println("Enter height of the triangle : ");
    int size = kb.nextInt();
    
    System.out.println("Which character you want to use : ");
    char c = kb.next().charAt(0);
    

    Use:

    System.out.print("Enter height of the triangle: ");
    int size = kb.nextInt();
    
    System.out.print("Enter character you want to use: ");
    char c = kb.next().charAt(0);
    
MultiplyByZer0
  • 6,302
  • 3
  • 32
  • 48
  • thank you not for the answer, for the great advice!! no one tell me about any of them before. – JK1 Jun 04 '19 at 23:36