PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 18, 2022

[FIXED] How to output indenting strings in Java?

 August 18, 2022     indentation, java, output, spaces, towers-of-hanoi     No comments   

Issue

I am doing the Towers of Hanoi and I want to output all the moves (2^n-1) with increasing leading spaces. E.g.

Moving top disk from....
    Moving top disk from....
        Moving top disk from....
            Moving top disk from....

... and so on.

I tried to create a separate "space" method, but I'm unsure as to how to implement it into the program

This is a part of my code right now.

public static void towersOfHanoi(int disk, int source, int dest){
        int temp;
        if (disk == 1) {
            moveDisk(source,dest);
        }
        else {
            temp = 6 - source - dest;
            towersOfHanoi(disk-1,source,temp);
            moveDisk(source,dest);
            towersOfHanoi(disk-1,temp,dest);
        }
    }
    
    private static void moveDisk(int source, int dest) {
        System.out.println("Moving top disk from " + source + " to " + dest);
    }

Solution

I have used a string builder to implement the indents. The number of indents are defined by a static variable for the class "indents" and a for loop is used each time the moveDisk method is called.

public class Test {

    static Integer indents = -2;
    static String indent = "   ";
    public static void towersOfHanoi(int disk, int source, int dest){
        int temp;
        if (disk == 1) {
            moveDisk(source,dest);
        }
        else {
            temp = 6 - source - dest;
            towersOfHanoi(disk-1,source,temp);
            moveDisk(source,dest);
            towersOfHanoi(disk-1,temp,dest);
        }
    }

    private static void moveDisk(int source, int dest) {

        indents = indents + 1;

        StringBuilder sb = new StringBuilder();

        for(int i = 0; i<=indents; i++){
            sb.append(indent);
        }
        sb.append("Moving top disk from ");
        sb.append(source);
        sb.append( " to ");
        sb.append( dest);

        System.out.println(sb.toString());
    }

    public static void main(String[] args) {

        moveDisk(10, 10);
        moveDisk(1, 10);
        moveDisk(20, 10);

    }
}

The outcome:

Moving top disk from 10 to 10
   Moving top disk from 1 to 10
      Moving top disk from 20 to 10



Answered By - Aneesh
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing