-1

Here is what I have so far. When I run the code I am getting the correct number of letters however It is outputting in a straight line instead of a reverse triangle pattern.

import java.util.*;
import java.io.*;

class Main {
    public static void main(String[] args) throws IOException {

        Scanner f = new Scanner(new File("labA21.dat"));
        while (f.hasNext()) {
            triangleTwo(f.nextInt(), String.format(""+ f.next().charAt(0)));
        }
    }

    /**
      Description: output a reverse triangle given a number of rows and a letter
      @param r a number of rows
      @param let a letter that we want to output
    */
    public static void triangleTwo(int r, String let) {

        for (int a=r;a>=1;a==) {
            for (int b=1;b<=a;b++)
                System.out.println(let);
            System.out.println();
        }
        System.out.println();
    }
}

What I'm getting:

A
A
A

A
A

A

What I'm supposed to get:

AAA
AA
A

Stephen P
  • 14,422
  • 2
  • 43
  • 67
  • 3
    [Why not upload images of code on SO when asking a question?](https://meta.stackoverflow.com/q/285551/5221149) – Andreas May 06 '20 at 19:52
  • 3
    Edit question and show what you're actually getting, and what you're expecting. – Andreas May 06 '20 at 19:54
  • print =/= println – jhamon May 06 '20 at 20:06
  • When Andreas asked you to add the current/expected output, his first comment was still valid: put text, not images – jhamon May 06 '20 at 20:08
  • I've edited your question for you — now people would be able to _copy your code_ and _paste it_ into their own editor or IDE, compile it, run it, and change it to help answer your question. _**Never**_ put _pictures of code_ in a question — put _actual code_. – Stephen P May 06 '20 at 23:50

1 Answers1

1

You almost got the right code.

The problem is the method you call to print the characters.

System.out.println(String) prints a String then a line break. This is what you want to do at the end of each rows. But not between each characters.

To print a String without including a line break at the end, use System.out.print(String).

Check the java official tutorial or the javadoc for more details.

public static void triangleTwo(int r, String let) {
    for (int a=r; a>=1; a--) {
        for (int b=1; b<=a; b++) {
            System.out.print(let); // no line break between char of the same row
        }
        System.out.println(); // line break for the end of the row
    }
    System.out.println(); // another line break, this is optional
}

Side note: Next time you post a question on SO, post the code, the current output, and the expected ouput as text. Text is more persistant than images and can be copy/paste easily by anyone without losing time rewriting the whole code.

jhamon
  • 3,603
  • 4
  • 26
  • 37