user3338137
user3338137

Reputation: 57

How can I use boolean expressions to fix this code?

I would like to create a method that crops a specified number of characters off of either the front or the back of or both the front and back of a provided string of characters. If the number of characters to be cropped is too long given the length of the text it returns as "Can't crop more than the length of the text."

The parameters are supposed to do the following:

@param  String text - a piece of text of any length containig ascii characters
@param  int howMany - number of characters to be cropped
@param  boolean front - true if characters should be cropped from the front of text, false don't crop from front
@param  boolean back - true if characters should be cropped from the back of text, false don't crop from back 

I currently have this code but the program crashes especially when displaying the "Cropped (both):" Say String text said "sammy" and the int howMany said "4", well the if statement would not display because int howMany is not greater than String text, BUT... the program collapses when displaying cropped (both) because it is not possible to crop 4 from the front and 4 from the back, that results in -3. I am sure I have to use boolean values here but I am not exactly sure how I am supposed to incorporate them in, they are part of my parameters but I have not used them. Can you please explain to me how I can fix this code to do the desired task? This method is called upon by the main method and arguments are passed on from that method to this one. I passed on true for all the boolean expression. This is the code I have...

public static String cropText(String text, int howMany, boolean front, boolean back)
{
  String result = "";

  if (howMany > text.length())
  {
     JOptionPane.showMessageDialog(null,"Can't crop more than the length of the text");
  }

  else
  {
     System.out.println("Cropped (both):");
     System.out.println("-----------");
     System.out.println(text.substring(howMany, text.length() - howMany));
     System.out.println("--------------");

     System.out.println("Cropped (front):");
     System.out.println("-----------");
     System.out.println(text.substring(howMany, text.length() - 0));
     System.out.println("--------------");
     front = true;

     System.out.println("Cropped (back):");
     System.out.println("-----------");
     System.out.println(text.substring(0, text.length() - howMany));
     System.out.println("--------------");
     back = true;
  }

  return result;
}

Upvotes: 0

Views: 78

Answers (3)

blalasaadri
blalasaadri

Reputation: 6208

You seem to be ignoring the booleans front and back so far; those should be relevant for how the function works too. The simplest way to solve this is probably something like the following pseudocode:

if(front should be cropped) {
    if(front can be cropped) {
        crop front
    } else {
        say you can't
    }
}

same for the back

if(front and back should be cropped) {
    if(front and back can be cropped) {
        crop front and back
    } else {
        say you can't
    }
}

This code can be optimized but should be easy enough to understand this way.

Upvotes: 0

Florent Bayle
Florent Bayle

Reputation: 11930

The problem is that when you crop from both sides, you don't crop howMany from the text, but 2*howMany. So, you have to change two things:

  • Change the if condition:

    if ((front && back && (howMany*2 > text.length())) || howMany > text.length())
    
  • Put your println in conditional blocks:

    if (front && back) {
        System.out.println("Cropped (both):");
        System.out.println("-----------");
        System.out.println(text.substring(howMany, text.length() - howMany));
        System.out.println("--------------");
    }
    
    if (front) {
        System.out.println("Cropped (front):");
        System.out.println("-----------");
        System.out.println(text.substring(howMany, text.length() - 0));
        System.out.println("--------------");
    }
    
    if (back) {
        System.out.println("Cropped (back):");
        System.out.println("-----------");
        System.out.println(text.substring(0, text.length() - howMany));
        System.out.println("--------------");
    }
    

Upvotes: 0

Floris
Floris

Reputation: 46415

Instead of

if (howMany > text.length())

You need to see whether you are asked for cropping on zero, one, or both sides.

sides = 0;
if (front) sides++;
if (back) sides++;
if(howMany * sides > text.length())

Upvotes: 1

Related Questions