Name: mc57594 Date: 02/10/97
Hi, this is Matthew Davidson. I am using the
StreamTokenizer class for a Pig Latin program. To simplify things,
I attempted to make the program treat whitespace as if it was a token instead
of eating it up, by setting the whitespace characters to ordinary
characters. This works fine for all whitespace other than the space
character. For some reason, setting the space as an ordinary character disables
the StreamTokenizer's ability to recognize the EOF character, which
should be totally unrelated. As long as the space is kept whitespace,
it recognizes ^D as the EOF and terminates; otherwise it prints out
the ^D and continues, never terminating naturally. I suspect
this is some bug in the JDK 1.0.2, as the cause and effect seem
grossly unrelated. If this is an error, I would _really_ appreciate
an email confirmation to show to my instructor. I am running this
on a SPARC, but not in Solaris. Here is the source code:
import java.io.*;
import java.lang.*;
public class PigTrans {
public static void main(String[] argv) throws IOException {
StreamTokenizer st = new StreamTokenizer(System.in);
st.eolIsSignificant(true);
//st.resetSyntax();
st.whitespaceChars(0, 9);
st.ordinaryChars(9, 13);
st.whitespaceChars(14, 31);
st.ordinaryChars(32, 47);
st.ordinaryChars(58, 64);
st.wordChars(65, 90);
st.ordinaryChars(91, 96);
st.wordChars(97, 122);
st.ordinaryChars(123, 126);
st.lowerCaseMode(false);
StringBuffer sbTemp = new StringBuffer();
char charTemp;
int i = 0;
while( (i = st.nextToken()) != StreamTokenizer.TT_EOF){
if(i == StreamTokenizer.TT_EOL){
sbTemp.append('\n');
}
else if(i == StreamTokenizer.TT_NUMBER){
sbTemp.append(st.nval);
}
else if(st.ttype != StreamTokenizer.TT_WORD){
sbTemp.append( (char)st.ttype);
}
else{
charTemp = st.sval.charAt(0);
if(!Character.isLetter(charTemp)){
sbTemp.append(st.sval);
}
else if(st.sval.length() == 1){
sbTemp.append(st.sval);
}
else{
switch(charTemp){
case 'q':
if(Character.toLowerCase(st.sval.charAt(1)) == 'u'){
sbTemp.append(st.sval.substring(2) + "quay");
}
break;
case 'Q':
if(st.sval.charAt(1) == ('u' | 'U')){
sbTemp.append(Character.toUpperCase(st.sval.charAt(2))
+ st.sval.substring(3) + "quay");
}
break;
case 'y':
if(isConsonant(st.sval.charAt(1))){
sbTemp.append(st.sval + "yay");
}
else{
sbTemp.append(st.sval.substring(1) + "yay");
}
break;
case 'Y':
if(isConsonant(st.sval.charAt(1))){
sbTemp.append(st.sval + "yay");
}
else{
sbTemp.append(Character.toUpperCase(st.sval.charAt(1))
+st.sval.substring(2) + "yay");
}
break;
default:
if(isVowel(charTemp)){
sbTemp.append(st.sval + "yay");
}
else{
if(Character.isUpperCase(st.sval.charAt(0))){
sbTemp.append(
Character.toUpperCase(st.sval.charAt(1))
+ st.sval.substring(2)
+ Character.toLowerCase(st.sval.charAt(0))
+ "ay");
}
else{
sbTemp.append(st.sval.substring(1)
+ st.sval.charAt(0)
+ "ay");
}
}
}
}
}
}
System.out.print(sbTemp);
}
private static boolean isVowel(char TestChar){
switch (TestChar){
case 'a': case 'e': case 'i': case 'o': case 'u':
case 'A': case 'E': case 'I': case 'O': case 'U':
return true;
default:
return false;
}
/*if(TestChar == ('a'||'e'||'i'||'o'||'u'||'A'||'E'||'I'||'O'||'U')){
return true;
}
else return false; */
}
private static boolean isConsonant(char TestChar){
switch (TestChar){
case 'b': case 'c': case 'd': case 'f': case 'g': case 'h': case 'j':
case 'k': case 'l': case 'm': case 'n': case 'p': case 'q': case 'r':
case 's': case 't': case 'v': case 'w': case 'x': case 'z': case 'B':
case 'C': case 'D': case 'F': case 'G': case 'H': case 'J': case 'K':
case 'L': case 'M': case 'N': case 'P': case 'Q': case 'R': case 'S':
case 'T': case 'V': case 'W': case 'X': case 'Z':
return true;
default:
return false;
}
}
}
company - University of Virginia , email - ###@###.###
======================================================================
- duplicates
-
JDK-4022254 java.io.StreamTokenizer.pushBack method ignores possible errors
-
- Closed
-