~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

do you think that i should install java FX Ensumble(Native) 2.2 on the user computer and run the program

Don't think that should make a difference but you can try it out. Did you check that the user's computer actually supports Arabic? Send across an arabic text file and open it up on the user's PC to check it out.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

i use this way it changes the writing from ???? to something like this áÊÇá so i am thinking could i use something other than Cp1256 and utf-8 ???

Nope, unfortunately it doesn't work that way.

or the problem could be in the jdk and jre i use ???

Don't think so. But for the sake of completeness, can you post the Java version and which Windows OS / Office version you are using?

Also, reply to the questions I posed in my previous post and try out the properties thing I mentioned.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

There isn't much I can do sitting here unfortunately. One last thing you can try is to explicitly set the encoding when getting the connection. I'm assuming you are using JDBC-ODBC bridge driver. Put the following key-value pair in properties before passing it to getConnection: prop.put("charSet", "Cp1256"); or prop.put("charSet", "utf8"); as mentioned here.

EDIT: Also, are you sure Arabic characters can be displayed on the user's computer? How are viewing this data on users computer (i.e. directly in MS Access or in some application which you have written)? Can you send across a text file containing arabic data to the user's computer and check if the user can actually view it?

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Your question isn't very clear; can you provide a sample input/output for the use case in consideration?

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

When does this happen; when you run it on the user's PC or on your PC? If you are trying this on your user's PC, can you also try it on your PC? Also, print out the method output for the methods mentioned here in both the cases.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

how can i pass the -Dfile.encoding=Cp1256 when starting up Java process.

How does the user currently open up your application? I'm assuming via a .bat file? The idea is to modify the Java command line used. For e.g. if your command line is java -jar SomeAppJar.jar, then modify it to something like java -Dfile.encoding=Cp1256 -jar SomeAppJar.jar.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Have you tried out the JAVA_TOOL_OPTIONS trick? It doesn't matter whether you add it to the user or the system variable tab. Also read the original link I posted thoroughly so that you know what should be the value for JAVA_TOOL_OPTIONS.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

thanks for replay but i open environment variable but i couldnot find the JAVA_TOOL_OPTIONS variable do you want me to add it to the user variable or to the system variable ???

Yes, you'll need to create this entry. If this doesn't work out, try passing -Dfile.encoding=Cp1256 when starting up Java process.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

The real solution would be to manage the encodings explicitly (preferably using UTF-8) when inserting/querying db data. As a hack, you can try setting the JVM encoding using the tricks mentioned in this thread. Instead of using UTF8, use windows-1256 (or Cp1256 if it doesn't work).

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

What does System.getProperty("file.encoding") and Charset.defaultCharset() print on your and the user machine? Are the values different?

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Well, that's one issue then. When you store data from your machine, the default encoding of the machine is used because of which everything works out well. But when you run your app on a machine which has a different encoding (i.e. non-arabic), you end up storing arabic data with a non-conformant encoding hence the garbage characters.

Just to confirm: can you copy over your Access DB file to the users computer and try opening it up there. Does it still show as ????

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Does your box/Windows installation have Arabic set as the default language? Are you explicitly setting any sort of encoding in your code when writing/storing data in Access?

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Is it possible for your to copy the Access DB file from that computer and open in up on your computer? Does it still show ??? or does it display the data correctly? Since you say it works on your box, this seems like a case of missing font pack/regional pack or something along those lines.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

so in a multithreaded program if i had my song class that was shared between two or more classes then they could possibly both have the ability to change song objects. then this could lead to the program not functioning correctly. If you have time and don't mind making an example I would love to see it.

It need not be a multi-threaded program. Mutation related bugs can slip in even for a single-threaded program though they are normally more painful and hard to find in case of a multi-threaded one.

As an example, let's say that you plan on providing a "replace" functionality to your users of the playlist class. So basically, they can pick an existing song from the playlist and replace it with a new one. If you resort to mutation (to save memory as they all say) and instead of creating a new song, try modifying the existing song, this is what happens:

MP3_Player ipod = null; // initialize with songs; assume
Song toRemoveSong = ipod.getSongByName("Hotel California");

// we need to maintain a list of removed songs
List<Song> removedSongs = new ArrayList<Song>();
removedSongs.add(exisingSong);

// In some other method; try replacing the song
Song oldSong = ipod.getSongByName("Hotel California");
oldSong.setTitle("Baby");
oldSong.setArtist("Justin Bieber");

// Show the songs user has removed from the ipod
showToUser(removedSongs);

In case you haven't noticed yet, we are hosed. Since Song is mutable, the list of removed songs now contains 'Baby' instead of 'Hotel California' and you have lost the song …

nova4005 commented: Thank you for the great examples +1
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

I do have one thing to ask though just for clarification on point 4. I think I understand but you mean that I should lean more towards using the constructor than using the mutator methods?

Yes. So instead of:

Person p = new Person();
p.setName("sos");
p.setAge(666);

prefer

Person p = new Person("sos", 666);

To add to this, if you don't plan on changing the fields after the object is created, you can also mark them as final and be rest assured that there won't be any accidental assignment or mutation to these fields. It doesn't make a lot of difference in a single threaded program but become really important when you start using threads. Shared mutable state (i.e. sharing a Person object between two threads wherein the fields are mutate/can be set) can lead to headaches as the system grows in size. So for e.g. if you are asked to add a new song to the playlist and remove an existing one, instead of "mutating" or modifying the object you plan on removing, it's better to actually remove a given song object and add a new one. In short, stay away from unnecessary mutation! If you want to know more or need a detailed snippet to showcase the problem, just let me know and I'll write it out for you.

Does that come with the netbeans installation or is it a plugin that I can install seperately?

Some IDE's like Eclipse come …

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

A few more if you are game:

  • Use camel casing when naming methods/classes/variables etc. as opposed to snake case (e.g. MP3Player as opposed to MP3_Player)
  • Don't blindly provide getters and setters for every field in your class. For e.g. you already have addSong and removeSong methods in your code; why provide a getter for songList. This is made worse by the fact that since songList is mutable, I can add/remove songs without triggering your checks which is not a good thing. If you still have to provide a list of all the songs, wrap up your list in an immutable wrapper which the user can iterate but not mutate.
  • Think twice before adding methods; for e.g. why do you need a default constructor for MP3_Player?
  • Favor constructor initialization over setters
  • Be extra careful when validating user input. Currently you check for blank titles i.e. "". What happens if I pass in " "? Is that a valid title? If not, does your code catch that?
  • Try out testing frameworks like JUnit for testing/validating your code.
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Does it still fail; it shouldn't fail. What does your new XML look like?

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

You need to think why a code is written before adding in code. When you write: hello helloworld = (hello) context.getBean("helloworld");, what are you expecting it to return back? How do you think Spring will get back the hello object for you?

Take a few minutes and read the error message carefully. The error basically means "the context XML file which you have loaded doesn't have a bean called 'helloworld'" which is correct. Now the question is how can we fix this error? You already have defined a bean in the XML file but it isn't named "helloworld". How do you think can we do away with this error? Can you think of a way of replacing or modifying the code such that your class Main works?

On a related note, always put your class in package and follow Java coding conventions. hello is not a good name for a class. Also put this and the Main class in some sort of a package like test.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

I no longer program in Spring but IIRC, id names in Spring follow the Java variable naming conventions and hence ids can't be just numbers (which is what the exception message is trying to convey here). Change id="1" to id="one" and see how it goes.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

StoreReporter.class.getResourceAsStream doesn't do what you expect it to do. It tries to load a resource and return the corresponding stream relative to the class. Replace it with a new File(yourpath) and let us know how it goes.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Which IDE are you using for development? Use a debugger a check why that line has a null root. The assignment on line 56 seems to be causing the problem because you don't do a null check on the root before using it on line 49.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Trace your algorithm on a piece of paper before making changes. The code right now directly returns the left subtree node if the left sub-tree is non-null, something which is not desirable. Your top two conditions (the first two if's in the search function) are correct. The third if..else needs to be replaced by two if's. Something like:

  • If left sub-tree is not null, search left sub-tree (you had this correct in the previous attempt)
  • If right sub-tree is not null, search right sub-tree
  • Return null if all else fails
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

That's a good question!

JUnit and Servlets (basically your web apps) are frameworks. What a framework does is it runs the code you write which adheres to some common guidelines (e.g. in the case of a JUnit test, you either extend the class with some JUnit class or decorate the methods with the @Test annotation).

As to how it happens, its done entirely via reflection in both the cases. JUnit has the concept of "runners" or basically classes which run the test code. When you right click on the file and say run as JUnit test case, the Eclipse JUnit plugin calls one of the runner classes (in case of Eclipse BlockJUnit4ClassRunner) with the fully qualified name of your class to be tested. After that, it's all reflection magic by creating an instance of your test class, grabbing the special JUnit methods and executing them in some sort of wrapper/controlled environment.

In case of web apps, you specify the servlet fully qualified name of the servlet class in the web.xml file. The servlet container takes this name, tries to load and instantiate the class dynamically and if it succeeds, casts the object an appropriate servlet type (e.g. HttpServlet). I know this is all hand-waving and the details might deviate but the general idea remains the same. The web container/servlet container is very much aware of all the applications you have deployed, the URL's it is supposed to handle, the mapping between a given URL and a servlet etc. Thus, when …

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Replace the else in the search method with another if statement which checks if the right sub-tree is not null and checks it.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Now I have a problem with finding values on the right subtree.
I have added some code, that will search for a letter that was entered by the user, if it is found it will increase the frequency of the letter.

It will output for the root, and the left subtree, but does not return anything for the right subtree.

This is because you never search the right sub-tree if the value isn't found on the left sub-tree. You need to change your condition so that both left and right sub-trees are searched in your search method of the Tree class. Also, I hope you are aware that you lose the "find node in logarithmic time" feature offered by the search trees if you don't create your tree based on value but the number of nodes it has.

Another question.... Am I supposed to compare string values for the input for a balanced binary tree?

I'm not sure I understand this question: you are already searching for the nodes based on their string values?

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

That's a pretty strange binary tree. Normally, insertion criteria is based on the "value" attribute of each node which helps you determine where to insert a node into a tree and at the same time, how to "search" for a node inside the given tree. With the number of nodes used as an insertion criteria, how do you plan on finding a given node in the tree?

Anyways back to the question; with the current code, the output of A B D F C E is expected. Trace the tree on a piece of paper for the input you suggested. What kind of tree do you get? Any reason why you think A B D E C F should be the output?

EDIT: Gah, replying to a stale thread...

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

The documentation told me that base 10 was assumed if you didn't pass in the second argument!!

Oh noes, there is no such thing specified in the specification. The MDN goes to great lengths to even mention this:

An integer that represents the radix of the above mentioned string. Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified.

:)

EDIT: If you plan on always dealing with base 10 numbers, using the Number constructor is a safer option.

tux4life commented: :) +0
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Moral of the story: Don't trust parseInt() to work the same in Firefox as it does in Chrome.

I'm really interested now, details please. And while we are at it, were you passing in the second argument to parseInt? If not, bad Dani. :)

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

if one of the attributes of my class is a String, would that be an object inside my class???? Teacher says it's simply a variable...

There is no String inside your object. Keeping aside primitives for the time being, if a class is composed of multiple fields having different reference types (anything which derives from an Object), those names are nothing but references or pointers. There is no actual data being stored in your object. This distinction is important because you have the same object referred from multiple objects. If you think that a field is inside some other object, you might mistakenly think that each object has a copy of the field data which isn't true.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

It's not a typo, it's a slang term.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Creating new sub-forums is pretty much out of question these days since the focus/shift for the new Daniweb is more towards tagging stuff rather than creating new categories for things.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

I was wondering if a microcontroller or arduino section could have a home here. Or would that be best classified under "Software Development\C", or other various languages that can be used for more than 1 type of hardware?
Or Raspberry Pi, would that be a "Linux and Unix" thing, or something else?

There are multiple options. If you are writing it in C, posting it in the C section and adding a relevant "tag" should be good enough. Similarly with Python and other langauges; depends on the language your code snippet is in. Or, if your code is in a language which is not natively supported by this forum or it contains a dash of multiple languages, you can post it in the top level Software Development category and tag it accordingly.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

I have mixed emotions about it. What if I don't want the same avatar on all web sites that I visit?

Then maybe provide an option to override the gravatar image? But IMO this seems too much effort for something not too significant, especially when there are more important things to be tackled (chat, spell checker etc.). :)

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

There are some site wide issues going on (as already highlighted by other ongoing threads in the feedback forum). Hopefully it should be fixed in some time when Dani/Deceptikun are online.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Yup, things don't seem to be working as expected and this is happening to almost everyone. Hopefully Dani/Deceptikun should be able to solve these issues as soon as they are online. :)

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

I'm think something has broke causing some basic Daniweb functionalities to not work correctly. Hopefully this should be fixed in some time as soon as Dani/Deceptikun are online.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

A lot of Daniweb functionality like editing post for moderators, moving threads etc. doesn't seem to be working at the moment. I'll sure Dani/Deceptikun will look into it when they are online. Please bear with us. :)

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

does it take time to update?

Yes, Dani said it might take around a day to update the images at the bottom of the forum.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

How so? ;)

Because you folks convert a GIF to a static image even if you don't have to. Oh the shattered hopes and dreams... :)

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

When you upload an avatar, we resize it internally to ensure that it's 80x80, and the method we use to do that only returns the first frame of an animated image.

And what happens if the original image is 80x80? You still resize it? If not, we now have a way of uploading animated avatars. If yes, you folks are bad guys. ;)

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

So the advantage of using a constructor over a setter is that a constructor can initialize more than a variable, which is what a setter does?

Not really, the main advantage of using a constructor to initialize all member fields is to ensure one-shot initialization of objects which when coupled with the use of final fields, is a good thing when writing multi-threaded code. If you use setters to initialize your object, there exists a point wherein you have an incomplete instance available which isn't a good thing. As an example:

Person p = new Person();
// some conditional check/logic
p.setName("Sanjay");
p.setAge(42);

The problem with the above code is that you have a possibility of having an "invalid" Person instance in your code; a Person object which doesn't have any fields set.

They should be private and have methods that access them...encapsulate, encapsulate, encapsulate!

Unfortunately, this kind of boilerplate is actually a Java language limitation. In C#, one can freely access public fields and if abstraction/encapsulation is required, they can be converted in "method" calls without changing any client code. Something similar can be done for Python (in which everything is public anyways).

So yes, though it's a good thing to lean towards encapsulation by creating an army of getters and setters (typical Java way of doing pretty much anything), it's worthwhile to note that it really isn't an accepted OO practice but more like a language limitation.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Two more ways of doing it:

Holder idiom is normally used when you need lazy initialization. Enum approach is used to cut down on the line count and is good if you don't want lazy initialization and error handling when creating singleton instances.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

BTW... What is up with those check marks that show around posts?

It's a new feature that was introduced recently as explained by Dani here.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

So this would likely be just as efficient as a non-recursive implementation because it uses tail recursion

Not sure if that was intended but the function you posted isn't tail recursive because the state information has to be saved on the stack frame to carry out the multiplication. :)

A tail recursive function would be something like:

int factorial1(int n, int accumulator) {
    if (n == 0)
        return accumulator;
    return factorial1(n - 1, n * accumulator);
}

int factorial(int n) {
    return factorial1(n, 1);
}

On a related note, JVM unfortunately doesn't do tail call optimizations so you'll either have to:

  • Convert code to use a non-resursive solution
  • Use an explicit stack data structure and modify your recursive code a bit
  • Use a JVM language like Scala which detects tail recursion and performs the necessary optimization before writing out the JVM bytecode
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

But I do not know how this is done. Any idea?

I think I know what you are talking of. There is no way of setting a private field without constructor or setter injection unless you are OK with using reflection to set your fields. Look into the @Inject annotation which does exactly this. It has been some time since I have been actively involved with Spring so look at the Spring docs for details on this.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

The class won't compile?!

Because you have already assigned null once to the field s. The correct way is to do:

public class Test {
  private final String s;
  public Test(final String s) {
    this.s = s;
  }
}
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Array is a bad example because arrays in Java receive special treatment. Arrays are objects but you'll never see the source code for array of Object or array of primitives in the source distribution. They are dynamically created. For more gory details, read this

However, line one does assign seven to that variable, length.

final doesn't mean that it can never be assigned. final means something which has to be assigned once and only once.

how is length being set when neither a constructor nor a setter method can do this

As already mentioned, arrays receive special treatment in the JVM to the point that they have special opcodes assigned for creating arrays. Thus, when you attempt to create an array of given size, that size is set to the length field using the invisible constructor dynamically generated by the JVM (as mentioned above).

In the Spring framework, when doing dependency injection, even if a variable is private and there is no corresponding setter method, the variable will be changed upon initialization

Maybe I'm reading your question wrong, but a private field doesn't mean it can never be initialized by outside code. If you class has a constructor which takes care of initializing private and final fields, the outside code can simply call the constructor and the private variable will be initialized even when the outside code can't directly access the class privates.

Spring has two forms of injection: setter and constructor. …

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

Two problems with your approach.

First the major one: As mentioned by James, never use busy loops (i.e. while(true) {} in your application code. It will always result in CPU spinning at 100%. There is almost always a better way of waiting for some event than to loop over it continuously. If you ever feel that there is no alternative left, at least sleep for an acceptable amount of time (e.g. 100ms) before testing the condition again. Also, the Thread.sleep call in your code is not really required. Explicit sleeps in application logic is almost always a sign of bad design. Try to find better way to solve the same stuff.

The second minor issue is that you are always creating a thread whenever there is a request to play a new sound. This is expensive since Java has OS threads and OS threads have a significant (comparatively) overhead during creation. Plus, if you are using a recent Java version (e.g. Java 5 and above), give preference to using Executor and ExecutorService over raw threads if possible.

Here is one possile way of how I would do it, not the best but still works good.

public class Noise {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        final SoundHandler handler = new SoundHandler();
        handler.newSound("gunshots.mid");
        Thread.sleep(1000 * 100);
    }

}

class SoundHandler {

    private final Executor executor = Executors.newCachedThreadPool();

    public void newSound(String sound) {
        executor.execute(new SoundRunnable(sound));
    }

    class SoundRunnable implements Runnable {
        final String soundName; …
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

If you want a bounded queue (one with capacity) for producer consumer, use a LinkedBlockingQueue or ArrayBlockingQueue and its methods, put and take which automatically block/wake the corresponding thread.

For e.g. put on a full queue will block that particular producer whereas take on an empty queue will block that particular consumer. Perfect for your use case IMO.

~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster

What is the purpose of the method start(), why not just put that code in main()?

And...

In main() Test is instantiated as an object and start is called like this: new Test().start();

Why not just make start a static method and call it from main?

Simply put, for reasons of flexibility and good practice. Ideally, main should just be the driver method with minimal logic so that in case you need to re-use the logic somewhere else, you can intantiate the class, with each instance having specific behaviour based on the arguments passed in. Putting logic in static methods hurt scalability and re-use, at least in a much larger context if not for this small program.

Long back I had to work with some code which used static variables for maintaining state and had significant amount of code in the main method. I had to jump through hoops to ensure that the code behaved as expected when called from multiple threads.

I know these things don't apply to your small snippet but I guess that's how my brain is wired to write code. :)