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

Thread: Using Constants

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Using Constants

    Hello Everyone,

    I am having a problem with my code when deal with constants.
    My problem has to be solved using only two public static final constants.
    The question ask to use 6 as the number of repetition and 10 for range of numbers. I can only use for loops in my main method. They give an example where if the number of repetitions constant is set to 7 and the range constant is set to 5 the out put would look like this.
    | | | | | | |
    12340123401234012340123401234012340


    The out put I am getting is
    | | | | | | |
    1234123412341234123412341234

    I know I am missing a zero where the 4 end but I my main concern is that my vertical line starts off right but than as it progress the spacing is off. I have tried to adjust this but then it gets worst. Could someone look at my for loops and give me a suggestion on how to fix this.

    public class NumbersOutput
    {
       public static final int REPEAT =7;
       public static final int NUM = 5;
     
       public static void main (String[] arges)
      {
         for (int repeat=1; repeat<=REPEAT; repeat++)
          {
            for (int space=2; space<=NUM; space++)
                  System.out.print("*");
                  System.out.print("|");
          }
          System.out.println();
     
          for (int i=1; i<=REPEAT; i++)
          {
            for (int n=1; n<NUM; n++)
            {
              System.out.print(n);
            }
       }
     
    }

    Ok I can't seem to get the vertical lines to match up to what I want either by manually typing it or copy and past it. What the line should show on the first line is that vertical lines all line up under the zero. seven times.

    On the second line my vertical lines show up under the second number 1 but than go off by a few number every time it repeats. It should line up under the number 1 every time it repeats.
    Last edited by Williamscn; September 25th, 2014 at 01:54 PM.


  2. #2
    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: Using Constants

    I am missing a zero
    Where in the code do you expect a 0 to come from? n starts at 1.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help with constants!
    By sickfit in forum Java Theory & Questions
    Replies: 2
    Last Post: August 28th, 2014, 04:04 PM
  2. [SOLVED] Properly declaring constants in Java
    By ChristopherLowe in forum Java Theory & Questions
    Replies: 2
    Last Post: March 19th, 2012, 03:21 PM
  3. Abstract Classes and Named Constants
    By snowguy13 in forum Java Theory & Questions
    Replies: 3
    Last Post: March 7th, 2012, 02:43 PM
  4. class constants instance constants..... etc
    By chronoz13 in forum Java Theory & Questions
    Replies: 1
    Last Post: August 18th, 2009, 03:38 PM