Stefaan Dutry

Ranch Hand
+ Follow
since Sep 17, 2010
Stefaan likes ...
VI Editor Tomcat Server Java
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
3
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Stefaan Dutry

There's a couple ways i can see thread interaction happening:
  • Static variables
  • spawned threads from within the request thread
  • caching libraries


  • Static variables are a way to share data between request threads.
    As a real life example:
  • enums (which should be threadsafe out of the box)
  • loaded configurations inside used frameworks
  • static DateFormat object can cause realy weird results (as they're not threadsafe), so NEVER do this.


  • On the other hand, nothing stops you from creating additional threads within your request handling thread.

    A way of sharing data between request threads is by using a caching mechanism, but most of the caching libraries/frameworks handle the multithreading themselves (provided they're configured correctly).

    To be fair, most of the times you won't need to worry about the request threads interacting as long as you don't use static variables.
    When the data you share needs to be persisted, even when the tomcat server restarts, or the application is reloaded/redeployed, you'll most likely be using a database to store the data. In that case it will most likely suffice to fetch the data from the database with the following request (or the persistence framework inbetween).

    Lio Liov wrote:can I instantiate the array without specified the size



    I would suggest looking at, and using ArrayList.
    Especialy it's add method and it's toArray method.

    Although i can't think of a reason to convert it to an Array again in this case.
    11 years ago
    I would suggest checking out the java.util.LinkedList class.
    Especialy, it's add and remove methods look great for the job.

    My personal solution would be:
  • Create a class for your datatype (representing the table)
  • Internaly save your data in a LinkedList of LinkedLists
  • Create methods for your class that do what you want:
    addRow(int rowIndex)
    addColumn(int columnIndex)
    removeRow(int rowIndex)
    removeColumn(int columnIndex)


  • I hope this helps.

    Sincerely,
    Stefaan
    12 years ago
    Hello,

    When i use an include through the following code:

    and in that include file i use :

    then, when there's long message as one of the actionerrors, i don't get the full message, only the first 48 characters are displayed.

    When i use the same tag directly in my file (not in the include file) the full message is displayed.

    Is there some way around this, because it means duplicate code?

    I also tried iterating over the actionerrors and displaying them like that, but the result was exactly the same.

    Regards,

    Stefaan
    12 years ago
    I just noticed i made a mistake there.
    It's not required to name your parameter arg0 -> arg3.

    It's just de first 4 parameters that are considered, regardless their name (if any).

    Any hints towards any documentation are still welcome.

    Regards,

    Stefaan
    12 years ago
    Hello,

    I've been struggling to find a way to use nested resource messages in a jsp page.

    I finaly managed to find one, but I was wondering if there were better ways.

    in my resource file i have the following:


    in my jsp i did the following:


    it gives me the desired output:


    The main reason i was wondering if there were better ways, is because i couldn't find any documentation on the web on how to do it, i basicaly just guessed since the <bean:message> tag is also able to use attributes arg0 to arg3.

    If anyone could point me towards any documentation about this, i would be very grateful.


    Regards,

    Stefaan
    12 years ago
    When you write, for example:

    and you have this constructor:


    A new ThreeVector object gets created
    In the constructor the following happens:
  • the instance variable i of the new object is set to 2 (this.i = 2;)
  • the instance variable j of the new object is set to 3 (this.j = 3;)
  • the instance variable k of the new object is set to 2 (this.k = 2;)

  • After the object is created (and it's constructor completes) the variable vector1 gets assigned the new object.

    See the attachment for a visualisation of the result of the line of code in the stack and heap.

    I hope this clears things out a bit.


    Regards,

    Stefaan
    12 years ago


    Each object (instance of the class created by using the new operator (new ThreeVector()) has it's own set of instance variables.
    When you use the constructor, which, in this case, has 3 parameters, you are saying, create me a ThreeVector type object with the parameters i provide.
    Those parameters only get meaning when they are actualy assigned to the object in some way, otherwise those local variables just disapear after the constructor is done executing.

    If we don't assign those instance variables the values in the constructor, they will defaulted to 0 (instance variable default value for primitive int type).
    12 years ago
    Ok,

    Let's start at the beinning.
    let me repost your code in code tags so it's easier to follow :


    I see you provide a Consturctor for you class ThreeVector.
    This constructor tells it to create a ThreeVector based on 3 integer parameters. First problem i spot here, is that no matter what you pass through to the constructor, nothing happens with it.
    To solve this, i propose you make 3 instance variables (i, j and k) and mark them private. Instance are directly under the class declaration, meaning not in any method.
    example:

    Then in your constructor you need to assign the values passed to the instance variables of the current object:

    Now we have an object that actualy contains the values that we passed to it.

    With the method at line 8 (magnitude(int xx, int yy, int zz)) i assume you want to calculate the magnitude of the current ThreeVector object.
    If that is the case you don't need to pass it any parameters, you can use the instance variables i, j and k and you can change line 8 to :


    At line 16 you write out your object. What actualy gets Written out is what is returned from the ThreeVector's toString() method. Which, because it is not defined is the one inherited from it's superclass, Object (every class inherits from Object).
    If you want something meaningfull when you print the object, then simply override the toString() method to have it return something usefull. Something like :


    At line 17 i think you want to calculate the magnitude of the ThreeVector. This can be done by calling the method magnitude on the ThreeVector object vector1 and, if you want to do something with the value, assign it to a variable:



    I hope this was clear enough.
    If it wasn't, post your new version of your code with the additional question.

    Regards,
    Stefaan
    12 years ago
    Hello and welcome to the Ranch.

    Could you post us the code of what you think is your closest try so far?
    That way we can spot what the problem is and work from there to help you.
    I think that way it will benefit your learning much more if we start from what you have so far.

    Regards,
    Stefaan
    12 years ago
    The problem you have is that, when you put the primes into the array, that you, for each of the array indexes keep walking through every value and keep overwriting the previously found one
    => ending up with an array where each index contains the maximum prime below the value entered.




    I strongly suggest you have a look at ArrayList.

    This way you can add every Integer (or int via autoboxing) you come across to this arrayList instead of having to go through them for the size first and then again for adding them.

    If you change your method to return a List of integers, you can also directly loop through all values of the List with an enhanced for loop with autounboxing.



    Regards,

    Stefaan
    12 years ago
    Do you mean after you want to seperate it after you input it to process the data?
    If so, check String.split().

    If this is not what you meant, please explain further.

    Regards,

    Stefaan
    12 years ago
    Glad to hear you got it working.

    Just keep in mind that your code will only work if the numbers are formatted as 2 digits seperated by a single char.

    This is perfectly fine for a learning exercise ofcourse.
    In real life applications you want to make sure that you foresee subtle input changes so you don't have to rewrite half your code.
    ( Trust me, there are plenty of real life applications that are written equaly brittle and it's not fun to have to go through the code to fix it everywhere when input is suddenly a bit larger )

    Other than that:

    Congratulations on your learning success.
    12 years ago
    according to page 200 of Head First Java, i think he wants to create an actual Dog object.

    The best way to do it would be to make a class Dog first. Something like the following should do for a learning test I suppose:

    in a file named Dog.java:


    The compiler doesn't know what a "Dog" is, or what methods it has, unless you tell him by providing the Dog class.
    12 years ago
    The error is basicaly saying that your compiler can not locate the class with the name Dog.

    Do you have a class named Dog somewhere?

    How exactly are you trying to compile/run your code?
    (in an editor, with a command, if so which command)?
    12 years ago