henry wu

Greenhorn
+ Follow
since Dec 01, 2000
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by henry wu

cool.. that worked! Thanks for all the help!
should i have used stringTokenizer from the start?
[ July 10, 2002: Message edited by: henry wu ]
22 years ago
Ok, changed to -
tokenizer.wordChars(0,9);
if (tokenizer.nextToken()==tokenizer.TT_WORD) {
return tokenizer.sval;
}
else {
System.out.println("This is not a valid string\n");
}
Still didn't work..
Do you guys have any recommendations for keyboard input for integers and strings other than StreamTokenizer and BufferedReader?
22 years ago
Ok the new code looks like this:
tokenizer.wordChars('0','9');
if (tokenizer.nextToken()==tokenizer.TT_WORD) {
return tokenizer.sval;
}
else {
System.out.println("This is not a valid string\n");
}
But it is still not recognizing '001' as a word.. Am I placing the wordChars function call in the wrong place?
22 years ago
cool thanks.. I'll try this out!
22 years ago
I tried doing this:
if (tokenizer.nextToken()==tokenizer.TT_WORD) {
return tokenizer.sval;
}
else {
System.out.println("This is not a valid string\n");
}
When I entered in '001' It told me that it wasn't a valid string.. so it didn't pass the tokenizer.TT_WORD test. Any ideas?
22 years ago
Hi everyone,
I made a StreamTokenizer object to capture keyboard input from the user. I want to be able to input numbers like "001" and "002" but the in tokenizer.nval it only captures "1" and "2". Is there any way around this?
StreamTokenizer tokenizer = new StreamTokenizer(new InputStreamReader(System.in));
if (tokenizer.nextToken()==tokenizer.TT_NUMBER)
{ return (int)tokenizer.nval; }
22 years ago
Ivor,
I have your 1.3 version book and it's awesome! I probably won't get the 1.4 version but just wanted to commend your writing!
Henry
23 years ago
How were the AWT questions like? That is the one area I have yet to learn..
23 years ago
You're right.. I meant it to be b=a instead of b=c.
with this code:
class Alpha {}
class Beta extends Alpha {}
class Chi extends Alpha {}
public class CastTest {
public static void main(String [] args) {
Alpha a = new Alpha();
Beta b = new Beta();
Chi c = new Chi();
a=b;
//b=c; //need explicit cast
b = (Beta)a; //should be OK to downcast
//b = (Beta)c; //can't cast to sibling classes
//c = (Alpha)c; //same as saying c=a
}
}
This is what I think:
1)[ a=b ] will work because b is a subclass of a so its implicitly cast.
2)[ b=c ] needs explicit cast since its a downcast (compile error)
3) [ b = (Beta)a] should be ok since it is explicitly downcasted (no runtime error for me)
4) [ b = (Beta)c ] can't cast to sibling classes (compile error)
5) [ c = (Alpha)c] same as 2.. can't make c=a since that is a downcast
So it looks like 1 and 3 will compile and run.
And 2,4,5 are all compile time errors. Is this correct?
Henry
Hmm.. I'm having some issues with your answers for this one chafule..
Rewrote the code so its easier to read :
class Alpha {}
class Beta extends Alpha {}
class Chi extends Alpha {}
public class CastTest {
public static void main(String [] args) {
Alpha a = new Alpha();
Beta b = new Beta();
Chi c = new Chi();
a=b;
//b=c; //need explicit cast
b = (Beta)a; //should be OK to downcast
//b = (Beta)c; //can't cast to sibling classes
//c = (Alpha)c; //same as saying c=a
}
}
This is what I think:
1) will work because b is a subclass of a so its implicitly cast.
2) needs explicit cast since its a downcast (compile error)
3) should be ok since it is explicitly downcasted (no runtime error for me)
4) can't cast to sibling classes (compile error)
5) same as 2.. can't make c=a since that is a downcast
So it looks like there are no runtime errors here.. only compile time errors? What do you think?
I kind of view it as the compiler will always cast automatically when you're going from small (byte) to big (int) (or widening).
When you're going from big to small (double -> byte) you're gonna have to explicitly cast because the double value might get cut off since byte is so much smaller than double.
Beginning Java 2 and Core Java are also good starter books..
23 years ago
I have to add that in the class that I'm trying to clone I have :
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
I'll try replacing return super.clone(); with some code and see what happens.
23 years ago