• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Searching for a match in an array

 
Ranch Hand
Posts: 33
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh my goodness this is really making me crazy! This is the second practice exercise in the book that's making me nuts! Please help me with this. I have a program here that must search for match in an array of doubles:



If a match is found, then it must be multiplied by the weight (from user input). In the calculate portion of the code, I've been doing:



I've also did a user-defined method to search for a match in the array but it was unsuccessful:



Please help me solve my problem. Thank you so much!>
 
Ryan Daguman
Ranch Hand
Posts: 33
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also tried using the binarySearch of the Arrays class here but it was unsuccessful as well:

 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ryan Daguman wrote:
I've also did a user-defined method to search for a match in the array but it was unsuccessful:


What arguments did you try it with that were unsuccessful? The following does print out 7.



One trick with comparing doubles is that they aren't always equal - you can have rounding error due to the way the values are stored. One technique for dealing with this is to use a threshold:
 
Ryan Daguman
Ranch Hand
Posts: 33
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes it printed 7 but how can I use the value assigned to that index of that array? How can I use the value of index 7 which is 33.45? Like when I entered 5 for weight, how can I use the value of index 7, 33.45 as a multiplier for weight?
 
Ryan Daguman
Ranch Hand
Posts: 33
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like for Choice and ComboBox components, you use the getSelectedIndex() or getItemSelectable() to get the index of what was chosen. So how can I do it with ints, doubles, etc?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In that particular example, when you have i = the index of the value you just found, then produceCode[i] gets that value from the array.

I don't see anything Swing-related in this question so I'm moving it to a more suitable forum.
 
Ryan Daguman
Ranch Hand
Posts: 33
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Repost, I edited it so that it'll be legible.
 
Ryan Daguman
Ranch Hand
Posts: 33
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is another edit. There are so many indents so I edited it again so that it'll become easy to read.
 
Ryan Daguman
Ranch Hand
Posts: 33
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's the complete code and I hope this helps and relay to your clearly what I'm trying to solve:

>
 
Ryan Daguman
Ranch Hand
Posts: 33
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok to make this simpler, I removed the keypad and the text area and replaced them with two buttons namely Calculate and Erase and three textfields for first: the produce code; second: the weight; and third: for the output. So once I typed in both pieces of information (the produce code and the weight), the program should search for a match in the produce code array and multiply it times the weight. Please refer to the code below:



Please help me with my problem. Thank you.>
 
Bartender
Posts: 322
24
Eclipse IDE Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ryan,

In your updated code, can you explain the logic behind your for loop beginning at line 53? What are you expecting the user to input for the produceCode?
a) Is the user supposed to type in the index value of the element in produceCode? For example, the user enters 1 and the program calculates 5.50 * weight?
b) Is the user supposed to type in the value of 5.50 for the produceCode and your program will determine the index is 1?

I'm guessing it is 'a', but as you have written the last version of the code the for loop is iterating through the array of produceCode elements and changing all the values to whatever the user input value is. For example, if I enter 2, all the values in the array will be updated to 2.0. Then, line 61 and 62 calculates the result for each of the elements (which are all 2.0) in the produceCode array * weight. Then, changes the String in the outputField over and over to that resulting value.

Never underestimate the power of a System.out.println statement to quickly see issues like this. If you insert System.out.println(result); between line 61 & 62 you will see what I'm getting at. It will print out 2.0 10 times if you enter 2 for the produceCode and 1 for the weight.

Cheers!
Chris
 
Ryan Daguman
Ranch Hand
Posts: 33
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hi Ryan,

In your updated code, can you explain the logic behind your for loop beginning at line 53? What are you expecting the user to input for the produceCode?
a) Is the user supposed to type in the index value of the element in produceCode? For example, the user enters 1 and the program calculates 5.50 * weight?
b) Is the user supposed to type in the value of 5.50 for the produceCode and your program will determine the index is 1?



Yes you are correct. The user will type in the index value of the element in produceCode. Example, the user enters 1 and then the program calculates 5.50 * weight. I was kind of enervated as midnight approaches here in my country when I created that loop. Sorry, I think the logic of that loop will not help in traversing all the elements of the produceCode array. So, after the user enters the index value of the element in produceCode, searching for a match is next. So how will I code that before I proceed to multiplying the value assigned to that index of the produceCode with weight? Thank you for your help.
 
Chris Barrett
Bartender
Posts: 322
24
Eclipse IDE Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ryan,

Actually, you don't need to search at all. If the user knows the index position of the produceCode element they want, the user did the searching for you. You can get rid of the loop and request the index position's value, then multiply that value by the weight.


Of course, that's pretty raw. It assumes the user always enters an Integer (else a NumberFormatException will be thrown) and that the Integer is a valid index position (or an ArrayIndexOutOfBoundsException will be thrown), but that should help with the immediate issue.

Cheers!
Chris
 
Ryan Daguman
Ranch Hand
Posts: 33
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh my goodness I never thought of that. The exercise says "create and store ten different produce codes in an array with local market prices. Once both pieces of data are entered, your program should search for a match in the produce code and multiply it times the weight." So I thought using a loop to traverse all the elements of the array and do a search is the right way of doing it. I never thought of doing result = produceCode[Integer.parseInt(strProduce)] * weight; I thank you so much sir Chris Barrett and I appreciate your help. I thank you as well for your advice that I should never underestimate the System.out.println. Maraming salamat po!
 
Chris Barrett
Bartender
Posts: 322
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome, Ryan. Happy to hear my input was useful.

Good luck with the rest of the exercises.

Cheers!
Chris
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic