Combine two methods

I have two methods that have exact same conditions but dfferent return values. Anyway to combine these two methods into one and still have two different values?

//getParameter values for aField and bField etc..

.........

String firstSetting ="";

String secondSetting ="";

public String methodOne(){

if ((fieldOne ==null) && (fieldTwo !=null) ){

firstSetting ="firstSetting value: " + aField +" another part of firstSetting";

}

elseif ((fieldOne !=null) && (fieldTwo ==null) ){

firstSetting ="firstSetting value " + bField +" another part of firstSetting";

}

elseif ((fieldOne !=null) && (fieldTwo !=null) ){

firstSetting ="firstSetting value " + bField +" another part of firstSetting " + aField;

}

return firstSetting;

}

public String methodTwo(){

if ((fieldOne ==null) && (fieldTwo !=null) ){

secondSetting ="secondSetting value " + aField;

}

elseif ((fieldOne !=null) && (fieldTwo ==null) ){

secondSetting ="secondSetting value: " + bField;

}

elseif ((fieldOne !=null) && (fieldTwo !=null) ){

secondSetting ="secondSetting value: " + bField +" and " + aField;

}

return secondSetting;

}

.....

[2960 byte] By [florida41a] at [2007-11-27 5:38:29]
# 1

Well the methods aren't exactly the same as methodOne adds extra stuff to the end of the string. However if they were the same except for the first/second bit, you could use a parameter.

public String method(String type) {

// cut down version to illustrate

return type + "Setting value: ";

}

System.out.println(method("First"));

System.out.println(method("Second"));

floundera at 2007-7-12 15:12:08 > top of Java-index,Java Essentials,Java Programming...
# 2

You can return a String[] with 2 elements (kind of dirty)

public String[] methodOneTwo() {

// ...

String[] result = {firstSetting, secondSetting};

return result;

}

or create a class for holding both values (not that much better as previous)

static class OneTwoSetting {

private String first;

private String second;

// constructors, setters and getters

}

public OneTwoSetting methodOneTwo() {

// ...

OneTwoSetting result = new OneTwoSetting(firstSetting, secondSetting);

return result;

}

[]

S_i_m_ua at 2007-7-12 15:12:09 > top of Java-index,Java Essentials,Java Programming...