Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Trying to create Triangles with Characters and Spaces

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Location
    Texas
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Trying to create Triangles with Characters and Spaces

    So I am trying to create this with my program:
    AAAAAAAA
    AAAAAAA
    AAAAAA
    AAAAA
    AAAA
    AAA
    AA
    A
    I am supposed to import the number of rows and the characters from a data file.
    Here is the code I wrote for it:

    import java.util.*;
    import java.io.*;
    public class Lab11d {
    public static void main(String []args) throws IOException
    {
    Scanner input = new Scanner(new File("Lab11d.dat"));
    while(input.next().equals("y"))
    {
    int row = input.nextInt();
    char col = input.next().charAt(0);
    String output = "";
    for(int i = row; i>0; i--)
    {
    for(int j = i; i>0;j--)
    {
    if(j==row)
    {
    output+="";
    continue;
    }
    else
    output+=" ";
    }
    for(int k = row; k>0; k--)
    output+=col;
    }
    System.out.println(output);
    }
    }
    }

    Here is the data file:
    8 A
    2 B
    1 T
    3 B


    I don't have any errors that Eclipse found. When I run it, nothing is printed in the console. Does anyone have any suggestions?


  2. #2
    Junior Member
    Join Date
    Dec 2011
    Location
    Texas
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Trying to create Triangles with Characters and Spaces

    The spaces are supposed to be on the left side of the characters for the output.

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    5
    My Mood
    Fine
    Thanks
    1
    Thanked 1 Time in 1 Post

    Cool Re: Trying to create Triangles with Characters and Spaces

    try this piece of code I just made...Might help..:
    PHP Code:
    import java.io.*;
    public class 
    p_no3{
          public static 
    void main(String[] args){
          
    int e;
          
    int j;
          for (
    e=9;e>=1;e--){
               for(
    j=e;j>=1;j--){
               
    System.out.print ("A");}
                 
    System.out.println (" ");
               }
         }


  4. The Following User Says Thank You to XxClouDxX For This Useful Post:

    MJtechie12 (December 19th, 2011)

  5. #4
    Junior Member
    Join Date
    Dec 2011
    Location
    Texas
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Trying to create Triangles with Characters and Spaces

    Thank you! Your code works perfectly but unfortunately I need the spaces to be on the other side of the A. It would look like this except with spaces instead of underscores:
    AAAAA
    _AAAA
    __AAA
    ___AA
    ____A

    And I also need to know why it won't read my data file.

  6. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Trying to create Triangles with Characters and Spaces

    Quote Originally Posted by XxClouDxX View Post
    try this piece of code I just made...Might help..:
    XxClouDxX, please read the forum rules as well as the following
    The Problems With Spoonfeeding


    MJtechie12, please wrap your code in the code tags. Since you've already been handed a partial answer, what did you try to move the spaces to the other side?

  7. #6
    Junior Member
    Join Date
    Dec 2011
    Location
    Texas
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Trying to create Triangles with Characters and Spaces

    I've now done this:
    PHP Code:
    import java.io.*; 
    public class 
    test
          public static 
    void main(String[] args){ 
          
    int e
          
    int j
          
    int k;
          
    String output "";
          for (
    e=9;e>=0;e--){ 
              for(
    k=ek>=e-1k-=1)
                  if(
    e==9)
                      
    k=e+1;
                  else
                      
    output+=" ";
              for(
    j=k+ej<e;j++) 
                 
    output+="A";
               
    output+="\n";
           
          }
          
    System.out.println(output);
      }

    And now it won't output anything.

  8. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Trying to create Triangles with Characters and Spaces

    And now it won't output anything.
    Add some println's in there to debug and see which loops are being executed and perhaps which are not, use this technique to break the problem down. Programming is a workflow that requires several rounds of modification, printing debug statements, compiling and running.

    for(j=k+e; j<e;j++)

    Think very carefully about this logic - when will j=k+e ever be less than e?

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trying to create Triangles with Characters and Spaces

    Another technique that will help you understand what your code is to do is to use variable names with meanings. You app is working with rows and columns and the number of spaces on a row and the number of characters on a row. If you were to name your variables with appropriate names then reading the code would help you understand what the code is doing.

    What are e, k and j. Something like: Row, column and number of char?

Similar Threads

  1. Please help! Nested while loops and asterisk triangles!
    By rockout341 in forum Loops & Control Statements
    Replies: 12
    Last Post: September 15th, 2011, 11:50 AM
  2. Triangles problem - creating triangle objects
    By sparky5783 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 15th, 2011, 12:38 PM
  3. Loop Patterns (triangles) [Beginner]
    By me1010109 in forum Loops & Control Statements
    Replies: 2
    Last Post: October 24th, 2010, 05:13 PM
  4. Cant get spaces
    By Ksmartman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 13th, 2010, 07:06 AM
  5. How to remove the in-between spaces in a String? Please help.
    By marksquall in forum Java Theory & Questions
    Replies: 2
    Last Post: February 4th, 2010, 04:19 PM

Tags for this Thread