Help coderanch get a
new server
by contributing to the fundraiser

Kentaro Shinbashi

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

Recent posts by Kentaro Shinbashi

When I run JUnit, and my tests fail, I get a lot of stack trace information. Sometimes it would be helpful just to get the name of the test that failed and not the whole JUnit stacktrace.

Does anyone know if there is an option to suppress the stacktraces?

Kentaro
18 years ago
Michael, thanks.

This works perfectly. I had to add about 6 or 7 to the preferred size, but it only took a little bit of trial and error.


Thanks for your helpful suggestion.


Kentaro
18 years ago
Jeroen,




Would this compile if it was:



Meaning that x would get each array in the top level array?

Thanks.

Kentaro
18 years ago
I have a JLabel that displays a numerical value; the value changes during the program, from 0 to 100 %.

When the value changes, the label and the components that are next to it jump around a little bit, and it looks bad.

I've tried setting the text of the JLabel to a formatted string with the format width being big enough to hold this, but it still jumps around.

How can I tell the JLabel to hold its size, either dimensionally or number of characters?

Thanks.

Kentaro.
18 years ago
Hi, I am using Netbeans 5.0 now.

It's pretty convenient in some ways, but I would really like to use its editor on java files without having to create a project and all of the trees and subtrees and other stuff that netbeans creates.

Is there a way to just put it in "editor mode", so I can make new files, save them, edit them, and use the NetBeans cool features like color coding and code completion?

Thanks.

Kentaro.
Thank you, Tony and Keith.

So, if I subclass JSlider, and I want to use all of the constructors of JSlider, then I must implement them all with super.

Thanks for your help.

Kentaro
18 years ago
Hello.

I'm extending the JSlider class:
class MySlider extends JSlider{....}

I would like to do something like:
new MySlider(0, 10)

To make a slider from 0 to 10.

Aparently, the constructor JSlider(int, int) is not inherited automatically.

Do I have to make a constructor like:



Am I missing something?

Thanks.

Kentaro
18 years ago
Hi, Joe. Thanks for replying.

No, my complaint is not so much that the readObject() method does not declare that it throws an EOFException, but rather that the documentation does not tell us that the readObject() method actually will throw an EOFException upon reaching the end of the file.

Of course the declaration makes sense; polymorphism allows subclasses to be thrown via the declaration of superclasses, and this is nicer than declaring all the possible subclasses that could happen. I understand that.

The problem is that the documentation does not tell us as developers/users what will happen under a really common situation, reaching the end of a file. It really should, because it's a really bad habit to infer behavior from other functions that might have somewhat parallel functionality. Don't you agree?

So, I am aware of the documentation for other methods, like those that read primatives, and I know what they do, because the API helpfully tells me. However, the API for some reason neglects to say it about readObject(), which by the name, one might guess is the centrally important function to a class named "ObjectInputStream".

As for the interface documentation for the DataInput interface, yes, that actually is very helpful. Thank you for pointing that out. I read the documentation for the superclasses, but I neglected to read the documentation for the interfaces.

However, since an Interface declaration cannot determine the actual behavior of the ultimate implementation, the following assertion is still not going to tell me that I should believe that readObject() will throw EOFExceptions:

It is generally true of all the reading methods in this interface that if end of file is reached before the desired number of bytes has been read, an EOFException (which is a kind of IOException) is thrown.



"generally" could mean that all of the read methods except readObject throw the EOFException at EOF. I just don't know.

But I'm not trying to be nitpicky with you, Joe. I'm really just wanting to emphasize that, even looking to the interface documentation, the behavior of readObject() is not really clear. My real complaint is that I wish the API just said that, instead of me having to research it all over the class and interface hierarchy.

So, if anyone from Sun is reading, I'm just requesting that in Java 6 API, if you tell us that readBoolean, readInt, readByte, etc. all throw EOFException at EOF, and readObject does the same thing, why not say so there too?

Thanks, Joe, for your feedback and suggestions.


Kentaro.
18 years ago
Hi, Joe.

By "undocumented", I mean that the function readObject() is not specified to throw an EOFException at the end of file. Indeed, the docs say that it throws an IOException on a usual IO related problem; so do you mean that EOF is considered a usual exception?



It is not undocumented.
...
If you read the JavaDoc for ObjectInputStream, it states several times that ". . . primitive reads will throw EOFExceptions. . .".




This really isn't very helpful for me, Joe; just because a bunch of other functions that are not apropos to my problem at hand have a certain specified behavior does not mean that I can just infer that other functions will do the same.

Besides, the very fact that the docs take pains to specify this behavior for the primitive read functions and then omit mention of it for the readObject() is sort of suggestive that maybe the readObject() function does something different.

So, when I say "undocumented", I mean actually, that it is not in the documentation, at last not the documentation that I have (Java 5 API).
18 years ago
Harish,

I recommend reading the java tutorial section about the wrapper classes Integer, Boolean, Float, and Double. Also, the api docs.

This will give you a general idea of how to pass ints as objects, floats as objects, etc.


It helped me a lot in figuring out solutions.

Kentaro
18 years ago
I should also say that EOFException seems to work, but it's undocumented, which makes me nervous to use it, since they might change things in Java 6 or 7 or whenever...
18 years ago
Yes, thank you Edwin. It is a good solution.


Do you know about the case where the number of objects is not saved in the file?

Thanks.
18 years ago
Hello. I've seen this topic many times before here, and I still have not found a solid answer to the problem:

When you have a file that you've written many serialized objects to, and then you read in the objects, how do you know when you've read in all of the objects?

(Say you don't know exactly how many objects there are.)

readObject does not return null or -1 or something if there are no more objects to read. That's the first suggestion people usually come up with, and it's wrong.

readObject does not throw an EOFException when there are no more objects to read. It throws (from the API of Java 2 SE 5):
ClassNotFoundException
InvalidClassException
StreamCorruptedException
OptionalDataException
IOException
But not EOFException. That's the second suggestion I've seen, but according to the API, it too is wrong.

So, if I've got a file with a bunch of serialized objects in it, and I call readObject repeatedly to read in the objects, how do I know when I've reached the end of the file?


Thank you.

Kentaro.
18 years ago
Yong Ming Wai:

I think the above suggestions are good. But I think it is also important to know what your application is.

You want to round to the second decimal place, which suggests that you might be dealing with monetary values, like dollars:

$40.00 / 3 = $13.3333333...
Rounded up is $13.34

So, if this is what you are dealing with, then the realm of questions changes a bit. Often, when dealing with financial calculations, it is better in the longrun to use all integer representations, for example, using an int to represent cents, and simply divide by 100 to get dollars.

Or else, use BigDecimal class.

The reason for this is that over time, errors accumulate, and sometimes, if adding a very small number to a very large number, information gets lost.


So, forgive me for going one step beyond your specific question, but I thought it might be relevent. Is it what you are trying to do?


Kentaro
18 years ago
Hello, Hatake.


What the class constructor really does?



By this question, are you asking what happens with the JVM when you instantiate a class? That is, are you asking step by step what goes on in the computer?


Kentaro
18 years ago