0

I'm new to java and I'm trying to print an english ruler horizontally instead of vertically, any help is appreciated.

I tried to put a sample output but I need 10 reputation but it's very similar to an english ruler. Here is a link of a photo https://i.stack.imgur.com/y8beS.jpg

public class MyRuler
{

    public static void main(String[] args)
    {
        drawRuler(3, 3);
    }

    public static void drawOneTick(int tickLength)
    {
        drawOneTick(tickLength, -1);
    }

    // draw one tick
    public static void drawOneTick(int tickLength, int tickLabel)
    {
        for (int i = 0; i < tickLength; i++)
            System.out.print("|\n");
        if (tickLabel >= 0)
            System.out.print(" " + tickLabel + "\n");
    }

    public static void drawTicks(int tickLength)
    { // draw ticks of given length
        if (tickLength > 0)
        { // stop when length drops to 0
            drawTicks(tickLength - 1); // recursively draw left ticks

            drawOneTick(tickLength); // draw center tick

            drawTicks(tickLength - 1); // recursively draw right ticks
        }
    }

    public static void drawRuler(int nInches, int majorLength)
    { // draw ruler
        drawOneTick(majorLength, 0); // draw tick 0 and its label
        for (int i = 1; i <= nInches; i++)
        {
            drawTicks(majorLength - 1); // draw ticks for this inch
            drawOneTick(majorLength, i); // draw tick i and its label
        }
    }
}
nem035
  • 34,790
  • 6
  • 87
  • 99
  • 2
    Suggestions: Format your code (I did it this time), Provide the output you want from the code, and read the rules about posting questions (How to Ask) on Stack Overflow – Alexandre Santos Sep 19 '14 at 21:31
  • Remove the '\n' and replace | with - possibly – bdavies6086 Sep 19 '14 at 21:34
  • | | | | | | | | | | | | | | | | | 0 1 2 I did a sample output here the formula is the smallest mark is at 1/2^ – Partha Benjapi Sep 19 '14 at 21:41
  • @AlexandreSantos Thank you for doing that, I'm new here but I can't draw a ruler here the editor doesn't help – Partha Benjapi Sep 19 '14 at 21:48
  • Don't add improvements to your question as comments, edit your question and add them there. In this case, we would like to see how you think the ruler should look like. – Alexandre Santos Sep 19 '14 at 22:13
  • This feels fizzbuzzy. – David Ehrmann Sep 19 '14 at 23:37
  • You can't go back upward after printing a newline. So, printing a whole vertical tick at once, as you're attempting to do, means you can't print any of the later ticks. You need to go line by line, not column by column, and figure out which ticks reach each line. (And that last phrase is the whole point of the assignment you're almost certainly trying to do.) – abarnert Sep 20 '14 at 03:25

2 Answers2

0

If you're not going for a a special formula for presentation (i.e. this will likely not be scaled to an actual ruler) and just want the output to print horizontally, removing all instances of \n from your code makes it print in a line.

public static void drawOneTick(int tickLength, int tickLabel)
{
    for (int i = 0; i < tickLength; i++)
        System.out.print("|");
    if (tickLabel >= 0)
        System.out.print(" " + tickLabel);

}
Compass
  • 5,867
  • 4
  • 30
  • 42
  • yeah I tried that but didn't look like a ruler though Imagine a 3 inch ruler for example that's what the output needs to look like – Partha Benjapi Sep 19 '14 at 21:46
0

Even after looking at your sample picture I wasn't sure what you wanted to print exactly so I decided to print the top part of the ruler bellow:

enter image description here

Considering I am European and I think the imperial system is weird and a major overkill, my ruler will measure in the metric system :) (centimeters and millimeters)

Ok, so the basic idea is to separate each row of ticks or labels as it's own String like:

String1 = | | | | | | | | | | | | | | | | | | | | | | | ...   // regular ticks
String2 = |                   |                   |     ...   // ticks to labels
String3 = 0                   1                   2           // labels

We build each string separately, then we combine them with a newline '\n' character in between them so they will print properly. You have to also make sure the number of spaces is exact so that the strings align correctly.

Here is the code:

class MyRuler {

    StringBuilder ticks = new StringBuilder();
    StringBuilder ticksToLabels = new StringBuilder();
    StringBuilder labels = new StringBuilder();

    int millimetersPerCentimeter = 10;

    String drawRuler(int centimeters) {
        // append the first tick, tick to label, and label
        ticks.append("| ");
        ticksToLabels.append("| ");
        labels.append(0);

        for(int i = 0; i < centimeters; i++) {
            for(int j = 0; j < millimetersPerCentimeter; j++) {
                if(j == millimetersPerCentimeter - 1) {
                    ticksToLabels.append("| ");
                    labels.append(" " + (i + 1));
                } else {
                    ticksToLabels.append("  ");
                    labels.append("  ");
                }
                ticks.append("| ");
            }
        }       
        ticks.append("\n" + ticksToLabels.toString() + "\n" + labels.toString());
        return ticks.toString();
    }

    public static void main(String[] args) {
        MyRuler ruler = new MyRuler();
        System.out.println(ruler.drawRuler(5));
    }
}

Output:

| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 
|                   |                   |                   |                   |                   | 
0                   1                   2                   3                   4                   5
nem035
  • 34,790
  • 6
  • 87
  • 99
  • nice output but the idea of the original code is to use the same recursive functions to create the ruler it does it vertically but I need it horizontally so there is a trick that I just can't figure out – Partha Benjapi Sep 20 '14 at 02:47