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 23 of 23

Thread: Hello Hello

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

    Cool Hello Hello

    **hello**
    ive created two vectors parallely ,one for strings and one for floats.Every string corresponds to a float.Thii is done inside a FOR loop. After the loop i calculate the min of floats and i need to get the corresponding string ,but unfortunately i dont get the correct string.
    Is my approach wrong,or should i continue experimenting towards this solution?
    (I m trying to do this using a hashmap too)

  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Hello Hello

    Hello speaker!
    Can you post your code?

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Hello Hello

    dont get the correct string.
    Please explain what you are doing and show the code that is accessing the two Vectors. If the Vectors are parallel, then an index used with each Vector's get method will return the corresponding elements.

    Is this the same question:
    http://www.javaprogrammingforums.com...know-java.html

    Why start another post for the same question
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Hello Hello

    Quote Originally Posted by Norm View Post
    If the Vectors are parallel, then an index used with each Vector's get method will return the corresponding elements.
    yes this is me! i use an index as you say ,but it returns the values only if executed inside the for loop.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Hello Hello

    returns the values only if executed inside the for loop.
    Not true.
    You do not need to use a loop to get elements from a Vector. You can call its methods anywhere in your code.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    speaker (March 28th, 2012)

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

    Default Re: Hello Hello

    ok..the loop is done in order to put float values into one vector,and string values to another vector.
    after the loop i find easily the min value of the float vector,but i still cant associate it with the correct string.

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Hello Hello

    When you find the index for the float value in one Vector, then use that same index to get the corresponding String from the other Vector.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Hello Hello

    Vector src =new Vector();                    
    Vector totVector=new Vector();                
                        for (int j = 0; j < energySource.size(); j++) {
                            Item sourceItem = (Item) energySource.elementAt(j);  //i get Items from energySource vector                     
           					  if (supply.description.isACover(sourceItem.description)) {
                                if (reasoner.checkCompatibility(supply.description, sourceItem.description)) {
                                    ..........
                                } else {
                                    ...........
                                }
                                //rTOT1 is a float number
                                rTOT1 = (alfaTOT * rSRC + betaTOT * rSRC) / depthSrc;
                                if (rTOT1 < minScore) {
                                    minScore = rTOT1;
                                    bestFunct = supply;
                                    bestIndex = j;
                                }
                                src.add(sourceItem);//add items to new vector, parallel of totVector
                                totVector.add(rTOT1);//add floats to totVector
                                System.out.println(supply.name + " + " + sourceItem.name + ">>>rTOT1 " + rTOT1);
     
                            } else {
                                candidati.removeElementAt(j);
                                j--;
                            }                                      
                        }         
                        Object rTOT1min = Collections.min(totVector); //find the min of totVector
                        Float bestRank = (float) rTOT1min;    //casting?!                                                                            
                        System.out.println("  Best Rank : " + bestRank  );   //printing this minValue

    and my problem is that after printing the minValue ,i have to get the sorresponding Item.
    how do i get i find the correct index outside the loop?
    Last edited by speaker; March 29th, 2012 at 01:13 PM.

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Hello Hello

    Can you explain what the problem is with using the same index in two Vectors?

    Post the output from your code and explain what is wrong with it.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Hello Hello

    Quote Originally Posted by Norm View Post
    Can you explain what the problem is with using the same index in two Vectors?

    Post the output from your code and explain what is wrong with it.
    so,i thought i should add something like this :
    Item newItem = (Item) src.elementAt(totVector.indexOf(rTOT1min)) ; <-------inside the parenthesis goes the index ,correct?
    System.out.println("source chosen: "+newItem.name);

  12. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Hello Hello

    What happens when you compile and execute that code?
    what value does the indexOf() method return? Make a separate statement and print out the value returned.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Hello Hello

    well it seems to be working like this,but after a number of loops i get an ArrayOutOfBoundsException after a few loops
    heres the output
    ###### CALCOLO SCORE ######
    AirCondition_A_Cooling + coffee_request>>>>>rTOT=0.25
    AirCondition_A_Cooling + ENEL    >>>rTOT1 0.55
    AirCondition_A_Cooling + EolicGenerator1KW    >>>rTOT1 0.53846157
    AirCondition_A_Cooling + Photovoltaic_16mq    >>>rTOT1 0.8148148
    AirCondition_A_Cooling + Photovoltaic_30mq    >>>rTOT1 0.6
    AirCondition_A_Cooling + SolarCollector2mq    >>>rTOT1 0.7619048
    AirCondition_A_Cooling + SolarCollector4mq    >>>rTOT1 0.7058824
    source chosen: EolicGenerator1KW
      Best Rank : 0.53846157
    RankTotal  is : 0.3365385
    AirCondition_A_Heating + coffee_request>>>>>rTOT=0.25
    AirCondition_A_Heating + ENEL    >>>rTOT1 0.55
    AirCondition_A_Heating + EolicGenerator1KW    >>>rTOT1 0.53846157
    AirCondition_A_Heating + Photovoltaic_16mq    >>>rTOT1 0.8148148
    AirCondition_A_Heating + Photovoltaic_30mq    >>>rTOT1 0.65
    AirCondition_A_Heating + SolarCollector2mq    >>>rTOT1 0.7619048
    AirCondition_A_Heating + SolarCollector4mq    >>>rTOT1 0.7058824
    source chosen: EolicGenerator1KW
      Best Rank : 0.53846157
    RankTotal  is : 0.3365385
    Coffeemachine_Brewing + coffee_request>>>>>rTOT=0.0
    Coffeemachine_Brewing + ENEL    >>>rTOT1 0.55
    Coffeemachine_Brewing + EolicGenerator1KW    >>>rTOT1 0.53846157
    Coffeemachine_Brewing + Photovoltaic_16mq    >>>rTOT1 0.8148148
    Coffeemachine_Brewing + Photovoltaic_30mq    >>>rTOT1 0.65
    Coffeemachine_Brewing + SolarCollector2mq    >>>rTOT1 0.7619048
    Coffeemachine_Brewing + SolarCollector4mq    >>>rTOT1 0.64705884
    source chosen: EolicGenerator1KW
      Best Rank : 0.53846157
    RankTotal  is : 0.16153848
    DishWasher_A_Intensive60C + coffee_request>>>>>rTOT=0.275
    DishWasher_A_Intensive60C + ENEL    >>>rTOT1 0.6
    DishWasher_A_Intensive60C + EolicGenerator1KW    >>>rTOT1 0.5769231
    DishWasher_A_Intensive60C + Photovoltaic_16mq    >>>rTOT1 0.8148148
    DishWasher_A_Intensive60C + Photovoltaic_30mq    >>>rTOT1 0.675
    DishWasher_A_Intensive60C + SolarCollector2mq    >>>rTOT1 0.7619048
    DishWasher_A_Intensive60C + SolarCollector4mq    >>>rTOT1 0.74509805
    source chosen: EolicGenerator1KW
      Best Rank : 0.53846157
    RankTotal  is : 0.35403848
    DishWasher_A_Normal60C + coffee_request>>>>>rTOT=0.275
    DishWasher_A_Normal60C + ENEL    >>>rTOT1 0.55
    DishWasher_A_Normal60C + EolicGenerator1KW    >>>rTOT1 0.46153846
    DishWasher_A_Normal60C + Photovoltaic_16mq    >>>rTOT1 0.8148148
    DishWasher_A_Normal60C + Photovoltaic_30mq    >>>rTOT1 0.65
    DishWasher_A_Normal60C + SolarCollector2mq    >>>rTOT1 0.7619048
    DishWasher_A_Normal60C + SolarCollector4mq    >>>rTOT1 0.6862745
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 29 >= 7
    	at java.util.Vector.elementAt(Vector.java:470)
    	at poliba.sisinflab.HomeReasonerWithEnergy.main(HomeReasonerWithEnergy.java:289)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 5 seconds)

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Hello Hello

    what value does the indexOf() method return? Make a separate statement and print out the value returned.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Hello Hello

    Quote Originally Posted by Norm View Post
    what value does the indexOf() method return? Make a separate statement and print out the value returned.
    hmmmm..it gives me a the min Float number..i thought it would give me an integer

  16. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Hello Hello

    Can you show what was printed out. And the code that was used.
    What indexOf() method are you using. Most versions of that method return an int value.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Hello Hello

    ok im sorry im panicking here..i get an integer which denotes the index

  18. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Hello Hello

    You need to add some printlns to debug what the code is doing to get the exception. Print out the value of the float number and the value returned by the indexOf() method just before the statement that causes the error.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Hello Hello

    Quote Originally Posted by Norm View Post
    You need to add some printlns to debug what the code is doing to get the exception. Print out the value of the float number and the value returned by the indexOf() method just before the statement that causes the error.
    ###### CALCOLO SCORE ######
    AirCondition_A_Cooling + coffee_request>>>>>rTOT=0.25
    AirCondition_A_Cooling + ENEL    >>>rTOT1 0.55
    AirCondition_A_Cooling + EolicGenerator1KW    >>>rTOT1 0.53846157
    AirCondition_A_Cooling + Photovoltaic_16mq    >>>rTOT1 0.8148148
    AirCondition_A_Cooling + Photovoltaic_30mq    >>>rTOT1 0.6
    AirCondition_A_Cooling + SolarCollector2mq    >>>rTOT1 0.7619048
    AirCondition_A_Cooling + SolarCollector4mq    >>>rTOT1 0.7058824
    integer=1
    source chosen: EolicGenerator1KW
      Best Rank : 0.53846157
    RankTotal  is : 0.3365385
    AirCondition_A_Heating + coffee_request>>>>>rTOT=0.25
    AirCondition_A_Heating + ENEL    >>>rTOT1 0.55
    AirCondition_A_Heating + EolicGenerator1KW    >>>rTOT1 0.53846157
    AirCondition_A_Heating + Photovoltaic_16mq    >>>rTOT1 0.8148148
    AirCondition_A_Heating + Photovoltaic_30mq    >>>rTOT1 0.65
    AirCondition_A_Heating + SolarCollector2mq    >>>rTOT1 0.7619048
    AirCondition_A_Heating + SolarCollector4mq    >>>rTOT1 0.7058824
    integer=1
    source chosen: EolicGenerator1KW
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 29 >= 7
      Best Rank : 0.53846157
    RankTotal  is : 0.3365385
    Coffeemachine_Brewing + coffee_request>>>>>rTOT=0.0
    Coffeemachine_Brewing + ENEL    >>>rTOT1 0.55
    Coffeemachine_Brewing + EolicGenerator1KW    >>>rTOT1 0.53846157
    Coffeemachine_Brewing + Photovoltaic_16mq    >>>rTOT1 0.8148148
    Coffeemachine_Brewing + Photovoltaic_30mq    >>>rTOT1 0.65
    Coffeemachine_Brewing + SolarCollector2mq    >>>rTOT1 0.7619048
    Coffeemachine_Brewing + SolarCollector4mq    >>>rTOT1 0.64705884
    integer=1
    source chosen: EolicGenerator1KW
      Best Rank : 0.53846157
    RankTotal  is : 0.16153848
    DishWasher_A_Intensive60C + coffee_request>>>>>rTOT=0.275
    DishWasher_A_Intensive60C + ENEL    >>>rTOT1 0.6
    DishWasher_A_Intensive60C + EolicGenerator1KW    >>>rTOT1 0.5769231
    DishWasher_A_Intensive60C + Photovoltaic_16mq    >>>rTOT1 0.8148148
    DishWasher_A_Intensive60C + Photovoltaic_30mq    >>>rTOT1 0.675
    DishWasher_A_Intensive60C + SolarCollector2mq    >>>rTOT1 0.7619048
    DishWasher_A_Intensive60C + SolarCollector4mq    >>>rTOT1 0.74509805
    integer=1
    source chosen: EolicGenerator1KW
      Best Rank : 0.53846157
    RankTotal  is : 0.35403848
    DishWasher_A_Normal60C + coffee_request>>>>>rTOT=0.275
    DishWasher_A_Normal60C + ENEL    >>>rTOT1 0.55
    DishWasher_A_Normal60C + EolicGenerator1KW    >>>rTOT1 0.46153846
    DishWasher_A_Normal60C + Photovoltaic_16mq    >>>rTOT1 0.8148148
    DishWasher_A_Normal60C + Photovoltaic_30mq    >>>rTOT1 0.65
    DishWasher_A_Normal60C + SolarCollector2mq    >>>rTOT1 0.7619048
    DishWasher_A_Normal60C + SolarCollector4mq    >>>rTOT1 0.6862745
    integer=29
    	at java.util.Vector.elementAt(Vector.java:470)
    	at poliba.sisinflab.HomeReasonerWithEnergy.main(HomeReasonerWithEnergy.java:290)
    Java Result: 1

  20. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Hello Hello

    What is the size of the Vector at line 290?
    Is 29 the value returned by the indexOf() method?
    What object was the indexOf() method searching the Vector for?

    Where is the first line of the stack trace that shows the type of error?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Hello Hello

    Quote Originally Posted by Norm View Post
    What is the size of the Vector at line 290?
    this is the line: newItem=(Item) src.elementAt(totVector.indexOf(rTOT1min));

    Is 29 the value returned by the indexOf() method?
    yes it returns that value.it shouldnt.

    What object was the indexOf() method searching the Vector for?
    hmmm....it suppose to find the corresponding Item for the minValue(float)

  22. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Hello Hello

    Did you print out the value that indexOf was searching for?
    What is the size of the Vector when the error occurred? Can you print it out?

    Where is the first line of the stack trace from the error. Your posted output does not show it.

    You need to print out all these values so you can see what the computer sees when it executes the code where the error occurs.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Hello Hello

    that was very helpful. i realised that one of the vectors had always 6 elements ,while the other was incremented by 6 after every loop[duh!]
    So i clear the problematic vector after every loop:
    totVector.clear();
    this seems to work.:-)
    thnk you very much and forgive me for my ignorance.

  24. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Hello Hello

    The output from println can help you see what the computer sees. I find it very useful.
    If you don't understand my answer, don't ignore it, ask a question.

  25. The Following User Says Thank You to Norm For This Useful Post:

    speaker (March 30th, 2012)