Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove the space between printed lines?

Tags:

java

println

I created this program to help draw a map in a text adventure I am creating. So far all it does is draw a black square of detentions you input. Is there a way to remove the space between each line so there aren't white lines running through the square?

Here is my code:

import java.util.Scanner;

public class MapGrid {

    static String createGrid(int x, int y) {
        String output = "";
        String block = new String("\u2588\u2588"); //A string of two unicode block symbols: ██

        if(x * y != 0) { //If neither x nor y is 0, a map of length x and height y will be returned
            for(int n = 1; n <= y; n++) {
                for(int i = 1; i <= x; i++) {
                    output += block;
                }
                output += "\n";
            }
        }
        return output;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        try(Scanner sc = new Scanner(System.in)) {

            System.out.println("Please enter the length of your map");
            int length = sc.nextInt();

            System.out.println("Please enter the height of your map");
            int height = sc.nextInt();

            System.out.println(createGrid(length,height));
        }
    }

}

This is what it prints when the user inputs 5 and 5:

enter image description here

I took a screenshot because it was hard to see what I was talking about with this font

There is a small gap between each new line of blocks that makes it not look right. There probably isn't a way to fix this, but I thought I'd ask just in case there was.

like image 245
tarboy9 Avatar asked Nov 03 '15 21:11

tarboy9


People also ask

How do I remove spaces between lines in Python?

Python String strip() function will remove leading and trailing whitespaces. If you want to remove only leading or trailing spaces, use lstrip() or rstrip() function instead.


1 Answers

You probably want to redirect your System.out to another place.
If you want to work with Swing then you can look here

Then it's the duty of the other place where you redirect it.
But right now it's only a thing of your IDE (Eclipse). It is independent from your Java logic.

If your think serious about it. You don't want to play your text adventure in your Eclipse IDE. So if you are using Windows the next question is if you want to use the Window command line. This may already be enough for you. But I still would recommend to make first thoughts about in which kind of window you want to have your text output

like image 101
Ernst Robert Avatar answered Sep 25 '22 10:09

Ernst Robert