• 0

[Java] help with String boxing


Question

Latest java assignment. Make a program that (assuming it receives a String and a char symbol) can center a String and create a box around it using a symbol. If the String exceeds 76 spaces (the max it can be for it to be centered in the square) then it has to sub-divide the String into parts and center them and place them line by line inside the square of characters.

I figured this might not read too easily so I will paste my code thus far bellow. The method is called Box but it uses other methods that will appear in the code above so that it makes the job easier. Notice that I am able to do the boxing of the string if the string fits within the 80 lines but I am unable to do it so that it will divide the string into parts and print that centered within the box. Basically what i need is that comes after the else if (text.length() > 76){

thnx a bunch

heres the code:

public class Utiles
{
	//methods bellow work with the logic that 1 printed line is a maximum of 80 spaces

	public static void linea (int n, char c)//this method prints one char N amount of times in a line unless that line exceeds 80 characters
	{
  if((n>80)||(n<0)){
 	 System.out.print ("Too many characters to fit on one line");
  }else{
 	 char [] spaces = new char [n];
 	 for (int i= 0; i< spaces.length; i++){
    spaces [i]=c; //assigns the char value to the i position in the array
    System.out.print (spaces [i]); //prints the i position in the array
 	 }
  }
	}

	public static void linea(char c)//prints a full line (80 chars) of a char c
	{
  linea(80, c);
	}

	public static void linea()//prints a full line of the * char
	{
  linea(80,'*');
	}

	public static void centeredLine(int n, char c)//prints a character N amount of times but centered
	{
  if (n < 79 && n > 0){
 	 int i = 80 - n;
 	 int r = (int)(i/2);
 	 int o = 0;
 	 if (i%2 == 1){
    o = r +1;
 	 }else{
    o = r;
 	 }
 	 linea(r,' ');
 	 linea(n,c);
 	 linea(o,' ');
  }else{
 	 return;
  }
	}

	public static void box(char c, String text)//boxes-in a String. If the string is bigger than the amount of lines available, it has to subdivide it into lines inside the box made up of the character chosen
	{
  if (text.length() <= 76){   //if you want aspace for a char on either side and for the text to be centered..u only have 76 char spaces left for the text (all this assuming 80 is the max)
 	 linea(c);
 	 System.out.print(c);
 	 int i = 78 - text.length();
 	 int r = (int)(i/2); //will calculate the amount of spaces needed
 	 int o = 0;
 	 if (i%2 == 1){//if the spaces are odd, it will add one more to even out the centering
    o = r + 1;
 	 }else{
    o = r;
 	 }
 	 linea(r,' ');
 	 System.out.print(text);
 	 linea(o,' ');
 	 System.out.println(c);
 	 linea(c);
  }else if(text.length() > 76){
 	 
 	 }
  }

Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.