Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: this is easy for those who know java!!

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Thumbs up this is easy for those who know java!!

    Hello,i am having difficulty of finding the right approach of my problem.(noobAlert!)

    I have a FOR loop and inside i calculate a float number and add it in a vector.
    Every iteration of the loop corrisponds to a float number and a name(string).
    After the loop i find the min value of this vector,and my problem is that i cant find a way to "connect" it to the correspoding string.

    I have the impression that it is a stupid question,and that i m searching probably in the wrong places, nevertheless i really need some help here!thnx for your attention..


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: this is easy for those who know java!!

    What do you do with the Strings that are created in the loop? If you put the created Float values into a Vector, where do you save the Strings? Can you put the Strings into another Vector at the same time so the corresponding Strings and Float values are at the same relative locations in their Vectors and you can use the same index value to get each from its Vector.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: this is easy for those who know java!!

    There is already a vector containing Items(=strings) ,and i created another vector for the float numbers.
    Every Item corresponds to a float number which is the result of a ranking algorithm.
    I simply want to get the minValue and its corrisponding Item.:-)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: this is easy for those who know java!!

    If you have two parallel Vectors:String and Float. If you get the index of a value in one Vector, the same index used in the other Vector will get the corresponding value.
    Think of writing in two columns on a piece of paper. If you find a desired value on a row in one column, its corresponding value will be on the same row in the other column.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: this is easy for those who know java!!

    ok i know you are right but it didnt work for me,and i think i know why.
    when i declare the Item inside the loop it shows me a warning(local variable hides a field!)..is this the problem?

    for (int j = 0; j < energySource.size(); j++) {
                        Item sourceItem = (Item) energySource.elementAt(j);
                        if (supply.description.isACover(sourceItem.description)) {
                            if (reasoner.checkCompatibility(supply.description, sourceItem.description)) {
     
                                int RPOT = reasoner.rankPotential(supply.description, sourceItem.description);
                                rSRC = alfaSRC * RPOT;
                            } else {
                                SemanticDescription[] contract = reasoner.contraction(sourceItem.description, supply.description);
                                int RPOT = reasoner.rankPotential(contract[1], sourceItem.description);
                                int RPAR = reasoner.rankPartial(contract[0], sourceItem.description);
                                rSRC = alfaSRC * RPOT + betaSRC * RPAR;
                            }
     
                            rTOT1 = (alfaTOT * rSRC + betaTOT * rSRC) / depthSrc;                          //the float number for ranking
                            totVector.add(rTOT1);
     
     
     
                            if (rTOT1 < minScore) {
                                minScore = rTOT1;
                                bestFunct = supply;
                                bestIndex = j;
                            }
     
                            System.out.println(supply.name + " + " + sourceItem.name + "    >>>rTOT1 " + rTOT1);
     
                        } else {
                            candidati.removeElementAt(j);
                            j--;
                        }
     
     
                    }
     
                    //rTOT1min=>minValue of totVector
                    Object rTOT1min = Collections.min(totVector);
                    Float bestRank = (float) rTOT1min;  
                    System.out.println("  Best Rank : " + bestRank + " Source chosen : " + sourceItem.name);

    **the sourceItem is supposed to be a global variable
    Last edited by speaker; March 27th, 2012 at 08:18 PM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: this is easy for those who know java!!

    what variable is hiding what field?
    it didnt work for me,
    Do you keep the Vectors in parallel? How do you search the Vector?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2012
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: this is easy for those who know java!!

    Quote Originally Posted by Norm View Post
    what variable is hiding what field?

    Do you keep the Vectors in parallel? How do you search the Vector?
    i posted the code.i hope it helps.
    the vectors are declared together,if that is what you ask me:-)

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: this is easy for those who know java!!

    If you add elements to the Vectors at the same time, one to each and do not change one without changing the other then it should work.
    Can you explain what the problem is?

    Partial code is no use in solving the compiler problem.

    I just saw that you added a comment on the first post.
    Was that the hidden variable?
    Why not use a different name for the new, local variable so the class variable is not hidden?
    Last edited by Norm; March 27th, 2012 at 08:34 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2012
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: this is easy for those who know java!!

    you are absolutely right. I will change the variable's name as you said .Parallelism is needed,but its difficult to be achieved.
    It is compiled but i dont get the correct corresponding values ,so ill try to create new parallel vectors .

  10. #10
    Junior Member
    Join Date
    Mar 2012
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: this is easy for those who know java!!

    Hello,
    Would a hashMap be a solution?

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: this is easy for those who know java!!

    It could.
    What would the keys and values be?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Easy Java Problem
    By AATroop in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 12th, 2011, 02:05 PM
  2. Java Image Problem! Probably easy fix!
    By TrivialFate in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 14th, 2011, 07:12 AM
  3. Quick easy Java question.
    By DHG in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 25th, 2011, 03:59 PM
  4. Databases in Java made Easy with JPersist
    By Lord.Quackstar in forum JDBC and Database Tutorials
    Replies: 1
    Last Post: June 14th, 2010, 06:38 AM
  5. an easy clear java programming tutorial
    By zkil_jpf in forum The Cafe
    Replies: 2
    Last Post: April 22nd, 2010, 08:40 AM

Tags for this Thread