0

I don’t understand why this doesn’t run. I am trying to make a program that displays the text the user inputs as a triangle. Each new line adds a letter which forms the triangle pattern.

For example, if the user enters the word computers, the output would be:
s
sr
sre
sret
sretu
sretup
sretupm
sretupmo
sretupmoc


import java.util.Scanner;

public class BackwordsTri
{
  
    public static void main(String[] arg)
    {
        
        Scanner keyboard = new Scanner(System.in);
        String word = keyboard.nextLine();
        
        
       String newWord="";
       char ch;
       int wl = word.length();
        
      for (int i=0; i < word.length(); i++)
      {
        int q = (wl - i);
        ch= word.charAt(q); 
        newWord= ch + newWord; 
        System.out.println(newWord); 
      } 
    }
}

Alias Cartellano
  • 366
  • 1
  • 3
  • 12
  • Hey, you can tab before code or just create a code segment in the toolbar, this is not discord :^) – Brentspine Dec 17 '21 at 19:54
  • 1
    After running your code it appears as though you have an index error ar `ch= word.charAt(q);` correct this with `ch= word.charAt(q-1);` and you'll get some output. – Alias Cartellano Dec 17 '21 at 19:54

1 Answers1

0

We might need a little more information to help you out on this one. It sounds like you want to be able to print the reversed version of the user input, incrementing by 1 additional letter per line starting from the last letter of the word.

Would you be able to add in the error that you are getting or what the output is from your program? along with a formatted version of the expected output if the issue is in fact formatting?