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 5 of 5

Thread: Printing a Diamond shape Problem.

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Printing a Diamond shape Problem.

    I am new to Java and have to do this for a class:


    Write a program that displays diamonds as text using a character provided by the user. For example, a diamond of height 6 constructed from asterisks will display as follows:

    *
    ***
    *****
    *****
    ***
    *

    Besides specifying a character, the user will specify one of three possible heights for each diamond – 6, 12, or 22 – by providing one of the strings “short”, “average”, or “tall”.

    Write the following methods in the program:
    • Public static int checkSize(String size) – This method will return 6 if size equals “short”, 12 if size equals “average”, 22 if size equals “tall”, or -1 otherwise. Be sure that the comparison is not case sensitive.
    • Public static void displayPattern (int size, char ch) – This method will display a diamond of height size constructed from pattern character ch.

    Use a do-while loop in the main method to verify that the diamond size that user enters must be “short”, “average”, or “tall”. If invalid string is input, the program should ask for new input until the valid input is given.


    All of the help I found online was to make diamond shapes off of predetermined values for the shape. so im having some difficulty doing it how the assignment asks for it.
    my code thus far:
    import java.util.Scanner;
     
    public class Diamonds
    {
       public static void main(String[] args)
       {
          Scanner kb = new Scanner(System.in);
          do
          {
             System.out.println("enter diamond size (short, average, or tall):");
             String size = kb.nextString();
             equalIgnoreCase(size);
          }while (size != "short" || "average" || "tall");
          int s = checkSize(size);
     
          System.out.println("enter pattern character:");
          Char p = kb.nextChar();
          displayPattern(s,p);
       }
       public static int checkSize(String size)
       {
          if (size = "short")
          {
             return 6;
          }
          else if (size = "average")
          {
             return 12;
          }
          else if (size = "tall")
          {
             return 22;
          }
          else
         {
             return -1;
          }
       }
       public static void displayPattern (int size, char ch)
       {
          for (int s = 6; s <= n; s++)
          {
             for (int j = 0; j < (n - s); j++)
                System.out.print(" ");
             for (int j = 1; j <= s; j++)
                System.out.print(p);
             for (int k = 1; k < s; k++)
                System.out.print(p);
                System.out.println();
          }
          for (int s = n - 1; s >= 1; s--)
          {
             for (int j = 0; j < (n - s); j++)
                System.out.print(" ");
       }
    }
    im getting a bunch of errors stating that java compiler can "not find symbols" for n and p

    any help would be great.
    thankyou
    Last edited by JavaPF; November 23rd, 2011 at 07:58 AM. Reason: Use highlight tags!


  2. #2
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Printing a Diamond shape Problem.

    the diamond shape didnt come out as a diamond.
    should be like
    ---*
    --***
    -****
    ******
    ******
    -****
    --***
    ---*

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Printing a Diamond shape Problem.

    Try this for the diamond symbol

    * Spoon feeding removed by administrator *
    Last edited by JavaPF; November 23rd, 2011 at 07:58 AM. Reason: Spoonfed solution removed

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Printing a Diamond shape Problem.

    @reddy1754
    How does giving code to the OP help the OP think through his problem and come up with a solution?
    See: The Problem with Spoon-feeding

    If you are going to post code, you should add comments to it describing its logic and how you figured out the solution so the OP can learn the process of solving problems.
    Copy and pasting does not teach programming.

  5. The Following 2 Users Say Thank You to Norm For This Useful Post:

    JavaPF (November 23rd, 2011), Mr.777 (November 23rd, 2011)

  6. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Printing a Diamond shape Problem.

    Quote Originally Posted by Sev View Post
    im getting a bunch of errors stating that java compiler can "not find symbols" for n and p
    Where in the code have you declared the n variable?
    You are getting the error with p because it is unreachable in the part of code you are requesting it. Try declaring it above the main method as public static.

    Looking over your code, this is the least of your worries.
    Code like size = "short" is not going to work either. You need to read up on the basics.

    I suggest you read:

    http://www.javaprogrammingforums.com...-mistakes.html

    What IDE do you use? Eclipse will point out all of this errors to you.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  2. Problem printing a two dimensional boolean array
    By sallyB in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 23rd, 2011, 03:56 PM
  3. Error In Printing. Perhaps a nesting problem
    By Pingu00 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 23rd, 2011, 07:46 AM
  4. Strange problem printing Unicode characters
    By sophist42 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 29th, 2011, 10:53 AM
  5. Problem with printing out collinear lines!
    By rkrajnov in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 25th, 2011, 05:32 PM