On 10 Jun 2011 07:33:12 GMT, Andreas Leitgeb
<> wrote:
>Gene Wirchenko <> wrote:
>> I am writing a simple preprocessor. I have a few spots where a
>> string needs to be parsed. I want to call something like this:
>> String ReturnString="";
>> boolean DidItWork=GetString(ReturnString);
>> if (!DidItWork)
>> // too bad
>> It is not acceptable to have a special String value mean failure. I
>> want the method to be able to return any potential string.
>
>There's three basic ways to do it:
>2) instead of the string, pass a mutable container of a string:
> String[] stringContainer = new String[1];
> boolean didItWork = getString(stringContainer);
> if (didItWork) { /* stringContainer[0] has the string */ }
>
> and within GetString:
> public boolean getString(String[] strCont) {
> strCont[0] = "blah"; return true;
> }
This is a kludge but fairly simple. I did a bit more looking and
found
http://java.sun.com/docs/books/jls/s...alues.doc.html
where a simple class is created. I ended up with
***** Start of Code *****
// Parameters.java
// Experimenting with Parameters
// Last Modification: 2011-06-10
class StringValue
{
String Value;
}
class IntValue
{
int Value;
}
public class Parameters
{
public static void main
(
String[] args
)
{
IntValue a=new IntValue();
IntValue b=new IntValue();
StringValue RetVal=new StringValue();
a.Value=1;
b.Value=2;
System.out.println(
"a="+a.Value+", b="+b.Value+", RetVal="+RetVal.Value);
ChangeValues(a,b,RetVal);
System.out.println(
"a="+a.Value+", b="+b.Value+", RetVal="+RetVal.Value);
}
public static void ChangeValues
(
IntValue First,
IntValue Second,
StringValue ReturnValue
)
{
First.Value*=2;
Second.Value++;
ReturnValue.Value="change";
}
}
***** End of Code *****
[snip]
>PS: Surely, someone will soon point out coding-conventions about
> upper-/lower-casing different kinds of identifiers. I dare to agree
> in advance. (Btw., I changed to conformant casing in my examples.)
I have used a number of languages. One of the things that I
dislike about Java is the small letter first style. It is
particularly bothersome, because my variable naming convention often
has HN-like prefixes. When I use a prefix, the main part of the name
is initial-capitalised; prefixes are not capitalised. For example,
"fhIn" is file handle for In.
Sincerely,
Gene Wirchenko