0

I have code where I have a constructor for an object with mixed data types:

public GuidedTour(String tourID, String tourDescription, double tourFee,
        String tourDate, int tourSize, String tourLeader)

I need to be able to allow the user to update the tourSize.

I can search and find particular tours, but am having trouble working out how to replace a substring with an inputted int value int newTourSize.

So the code I am trying to make the replacement is:

String tourDetails;
String newDetails = tourDetails.replace(tourSize, newTourSize);

It won't let me do it, as I am using int values. I have trying parsing the int into a String.

I am stuck on how to do this, beyond putting all the values back in, the original values with the new value, I am sure it would be better to find and replace that part of the substring and write this new string into the array.

I'm floundering a bit (but have improved much since my first java questions here!)

Any help?

0

3 Answers 3

5

It seems like your class looks like this:

public class GuidedTour {
    private String tourDetails;

    public GuidedTour(String tourID, int tourSize) { // other arguments omitted for brevity
        this.tourDetails = tourID + tourSize;
    }

    public String getTourDetails() {
        return tourDetails;
    }
}

And that you're now wondering how to change the tourSize into the tourDetails.

Well, it's easy. Don't forget about the parts of the tour details. Replace your class by something like

public class GuidedTour {
    private String tourID;
    private int tourSize;

    public GuidedTour(String tourID, int tourSize) { // other arguments omitted for brevity
        this.tourID = tourID;
        this.tourSize = tourSize;
    }

    public String getTourDetails() {
        return tourID + tourSize;
    }

    public void setTourSize(int newTourSize) {
        this.tourSize = newTourSize;
    }
}

Now you don't need to do any String replacement anymore. You just set the new tour size, and ask the GuidedTour instance to recreate the tourDetails String by reassembling all the parts.

0
4

If you just want to convert a primitive typed value to a String, do that:

String.valueOf(tourSize)

The String class contains several static methods for creating a String object from various inputs.

2

An addition of empty string "" using String Concatenation Operator "+" to any primitive type will result in a string. It is a equivalent operation to String.valueOf(data). For example:

String strSize = tourSize+"";
2
  • 1
    Don't do this! Suppose your tourSize was 7 and your newTourSize is 10. Your tourDescription happens to be "Explore the 7 wonders of the world". Guess what's going to happen! Nov 15, 2013 at 7:41
  • @DavidWallace, thank you for pointing out. I have removed the operation from answer. I assumed that op is careful about multiple appearance of same integer in his data string
    – Sage
    Nov 15, 2013 at 7:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.