Tony Docherty

Bartender
+ Follow
since Aug 07, 2007
Merit badge: grant badges
Cows and Likes
Cows
Total received
86
In last 30 days
0
Total given
54
Likes
Total received
510
Received in last 30 days
0
Total given
482
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Tony Docherty

Apologies if I'm being pedantic here but mkdir and mkdirs returning false means they couldn't create the directory not that the directory already exists. A return value of false probably is because the directory already exists but it could be for other reasons, such as the user doesn't have permission to create directories or the file system is unable to create directories.

4 years ago
If you create a File object for the directory you want to create you can use the isDirectory() and exists() methods to determine if a directory (or file) with that name already exists and if it doesn't use the mkDir() or mkDirs() method to create the directory. I suggest you read the Java docs for these methods to see the difference between them.
4 years ago
Yes I believe you can do this by extending FileView to create your own FileView class that returns the appropriate icon (from the getIcon() method) or return null to use the default icon. You register your FileView object through the setFileView method in JFileChooser.
5 years ago
Just to add more detail to my suggestion, Here's 2 basic ways of solving this written in plain text.

1. Look at each letter in turn and if it's not in my results list add it to the list and then iterate across the rest of the string to count how many times that letter occurs and write down the total number of occurrences.
2. Look at each letter in turn and if it's not in my results list add it to the list with the number 1 next to it. If it is in the list add 1 to the number next to it.
5 years ago
Your code looks like the sort of code you get when you just start writing code without a real plan - it's far more convoluted and complicated than it needs to be. May I suggest you get a pen and paper and write out in plain text the steps you would take to solve the problem yourself and then look to convert that to code.
5 years ago
In what way are you stuck? Please explain exactly what you want to do and what is actually happening.
5 years ago
Not sure that this will solve your problem but it's generally bad practice to perform long running tasks on the EDT, you should use a worker thread to run the task in the background. If your task is also interacting with the GUI (as yours is) you can use a SwingWorker see this tutorial: https://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html.

BTW why are you calling message.split() and then not using the returned array anywhere.
5 years ago

Leonardo Nash wrote:
- Is it bad idea to read data from same InputStream using multiple wrapper Streams (BufferedInputStream, DataInputStream, and another subclass of FilteredInputStream) ? If it is, why ?


If you mean chain wrappers together (ie wrap a FileInputStream in a BufferedInputStream which is wrapped in a DataInputStream) then this is fine but if you mean to wrap one InputStream in 2 or more different wrappers then this is not a good idea. The reason is if you read from one wrapper it will consume and return values from the underlying stream then when you read from another wrapper it will consume and return the next available values from the underlying stream and so the second read will be missing the values already read in via the first stream. Unless you are extremely careful you will have no idea what the data being read in actually means as it will be be starting from some unknown point in the stream.

Leonardo Nash wrote:
- Is it bad idea to call reset, mark methods from base InputStream that passed to wrapper InputStream (BufferedInputStream, ...) ? I think, we should call these methods from only wrapper InputStream.


The reset, mark methods from InputStream don't do anything (they are null implementations) so there is no point in calling them. As to the general principal of calling methods in base classes of wrapped classes I would say it is a dangerous policy if the methods change the state of the object. For example if you called an InputStream subclass that implemented reset() and then called the wrapping classes reset() method, if the wrapping class provides it's own implementation of reset and mark, then the result is unlikely to be resetting of the file position back to the marked point.
5 years ago
The problem with not calling flush() in the client code (assuming the android stuff works the same as standard Java) is the client is unlikely to send the string until the connection is being closed (or the buffer fills up) so the server will just sit there waiting for the message from the client.
5 years ago
Sorry I've never programmed for android so I can't give you a definitive answer to that but I would have thought you would have needed to call flush().
5 years ago
Can you show the client code as well please and does it use flush() as well.
5 years ago
Your problem is on line 42

When will this not evaluate to true?
5 years ago

Dave Tolls wrote:Sequence does not hav a no-args constructor, so cannot be serialized at all.


You can serialize it using the alternate approach I described in my earlier post provided there are getters and setters for the fields you need to serialize (or you are willing to use reflection).
5 years ago
Not sure why you are saving the state of the object but if you are trying to provide persistent storage then I would advise against using Serializable. if, on the other hand, you are implementing transient storage then Serializable is an acceptable approach, you do need a no args constructor though to deserialise an object.
An alternative approach to extending the Sequence class would be to create a wrapper class which implements the readObject and writeObject methods as stated in the API docs for Serializable. The wrapper class could then handle serialization and deserialization of the Sequence object's state through these methods.
5 years ago