Results 1 to 6 of 6
  1. #1
    wizar is offline Member
    Join Date
    Nov 2010
    Posts
    18
    Rep Power
    0

    Default Eliminating a line

    I'm a little bit stuck with how to work with this piece of code:

    Java Code:
      
    for (int x = a; x <= p; x*=3) 
          {
            System.out.println(x);
          }
          for (y = b; y >= 1; y/=3)
          {
            if (y % 3 !=0)
            {
              System.out.println(y);
            }
            else 
            {
              System.out.print("");
            }
          }
    All variables are integers.
    For this code, y will not be outputted if it is a multiple of 3. However, I want x not to be outputted as well if y is a multiple of 3. I'm having trouble because I want x to be outputted before y.

    I would also like to align the output with x in a column, and y in a column beside that. I don't think I can use "/t", so I was wondering how to use another for loop correctly (I can get the rows right, but I can't limit the columns correctly)? Or is there a better way of aligning the two outputs?

  2. #2
    tashimoto is offline Member
    Join Date
    Sep 2010
    Location
    Oregon, usa
    Posts
    69
    Rep Power
    0

    Default

    You might try nesting your for-loops and Only printing x after you have tested y ... maybe something like this:

    Java Code:
    for (int x=a; x<=p; x*=3) {
        for (y=b; y>1; y/=3) {
             if (y%3 != 0) {
                 System.out.print(x + "\t");
                 System.out.println(y);
              }
         }
    }
    BTW: I didn't check for correctness or syntax

  3. #3
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    In your code, Y is local to the second loop. It has no value while X is bring printed out.

    Can you provide some sample output that you want to achieve?

  4. #4
    wizar is offline Member
    Join Date
    Nov 2010
    Posts
    18
    Rep Power
    0

    Default

    Thanks for the idea tashimoto.
    Last edited by wizar; 11-04-2010 at 02:23 AM. Reason: to avoid confusion

  5. #5
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    Hi Wizar,

    I am not quite sure what problem you try to solve and how the output should look but you could consider to create a for loop with the initialisation, test and increment combined:

    Java Code:
      for(x = a, y = b; x <= p && y >= 1; x *= 3, y /= 3) {
      }
    Are you sure you want all variables to be ints? This means that after at most 2 iterations nothing will be displayed because after 2 iterations y % 3 will always result in 0.

    Succes,
    Erik
    I'm new to Java but I like to help where ever I can. :)

  6. #6
    wizar is offline Member
    Join Date
    Nov 2010
    Posts
    18
    Rep Power
    0

    Default

    I didn't realize you could do that! Thanks! :)
    And that code is all part of a larger program, no worries.

Similar Threads

  1. tracing java application line by line using netbeans
    By chandrasekhar123 in forum NetBeans
    Replies: 1
    Last Post: 08-03-2010, 03:46 PM
  2. Replies: 2
    Last Post: 07-02-2010, 03:20 PM
  3. Replies: 4
    Last Post: 09-10-2009, 11:18 AM
  4. Replies: 9
    Last Post: 04-07-2009, 04:51 AM
  5. Reading in data from file line by line
    By bluekswing in forum New To Java
    Replies: 1
    Last Post: 10-02-2007, 01:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •