Re: COBOL is dynamic (depends)
- From: "Oliver Wong" <owong@xxxxxxxxxxxxxx>
- Date: Thu, 26 Oct 2006 17:41:28 GMT
"Frank Swarbrick" <Frank.Swarbrick@xxxxxxxxxxxxxx> wrote in message
news:4qc64qFmdaonU1@xxxxxxxxxxxxxxxxx
Years ago for a project I had to deal with some messages that were[...]
something
like the following:
Field 1 - 4 alphanumeric characters
Field 2 - 2 numeric characters
Field 3 - either 16, or 33 or 49 characters in length, where if the first
character is a 'U' the length is 33, if the first character is a 'T' the
length is 49, otherwise the length is 16
Field 4 - 4 numeric characters
Field 5 - zero to 49 characters with the length being specified by field
4.
Field 6 - 1 character end of message delimiter ('!')
To be honest, I have no idea how I'd do this in, say, C or Java, but I
can't
imagine it would be any simpler.
It's not too bad, but it's not quite as nice a COBOL, since you need to
explicitly keep track of the length of C and E, rather than letting the
compiler do it for you. This Java program produces the same output, but it
hardcodes the input to the examples you provided, because input from the
console is actually somewhat of a pain to do in Java.
<code>
public class Depends {
private final String data;
private final int CLength, ELength;
public Depends(String input) {
this.data = input;
switch (data.charAt(6)) {
case 'U':
CLength = 33;
break;
case 'T':
CLength = 49;
break;
default:
CLength = 16;
break;
}
ELength = Integer.valueOf(data.substring(6 + CLength, 6 + CLength + 4));
}
public String getA() {
return data.substring(0, 4);
}
public String getB() {
return data.substring(4, 6);
}
public String getC() {
return data.substring(6, 6 + CLength);
}
public String getD() {
return data.substring(6 + CLength, 6 + CLength + 4);
}
public String getE() {
return data.substring(6 + CLength + 4, 6 + CLength + 4 + ELength);
}
public String getF() {
assert 6 + CLength + 4 + ELength + 1== data.length();
return data.substring(data.length() - 1);
}
public String toString() {
return "**********" + getC().charAt(0) + "\n" +
data + "\n" +
getA() + "\n" +
getB() + "\n" +
getC() + "\n" +
getD() + "\n" +
getE() + "\n" +
getF() + "\n";
}
public static void main(String[] args) {
System.out.println(new
Depends("ABCD990123456789ABCDEF00491234567890123456789012345678901234567890123456789!").toString());
System.out.println(new
Depends("ABCD99U0123456789ABCDEFFEDCBA98765432100033123456789012345678901234567890123!").toString());
System.out.println(new
Depends("ABCD99T0123456789ABCDEFFEDCBA9876543210FFFFFFFFFFFFFFFF00161234567890123456!").toString());
}
}
</code>
- Oliver
.
- References:
- COBOL is dynamic (depends)
- From: Frank Swarbrick
- COBOL is dynamic (depends)
- Prev by Date: Can a Cobol Program on IBM pass parameters to a Java Program and get back the results?
- Next by Date: Re: Can a Cobol Program on IBM pass parameters to a Java Program and get back the results?
- Previous by thread: COBOL is dynamic (depends)
- Next by thread: Re: COBOL is dynamic (depends)
- Index(es):