• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sequential Search an Array  RSS feed

 
Travis Westburry
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, i am in desperate need of more help this week. My professor is sub par and makes no effort to clear things up.

The Problem:
import a file and search for a specific piece of data that is requested by a user.
The output must return something similar to:
Sequential found ID number 77470, and its price is $49.55.
or
Sequential did not find ID number 77777.


I have no idea where to go from here, or even if this is correct....

Main Class:


Payroll Class:

 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you looking for from us? Does this compile? Does it run? What output did you get and what did you expect? Why do you think you need help?

Generally speaking, posting a big hunk of code and saying "I don't know if this is right" isn't going to get you much around here. I'd suggest you read our FAQ on How To Ask Questions on The JavaRanch.
 
Mahesh Kumaraguru
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Input file is

C:\Users\Public\Documents\JavaTestFiles\employees.txt



MainClass.java is



Payroll.java is



Employee,java is



Outputs are

run:
Enter the Employee number you would like to search:
0
Searching for Employee with ID = 0
Employee with id 0 not found
BUILD SUCCESSFUL (total time: 5 seconds)


run:
Enter the Employee number you would like to search:
2345
Searching for Employee with ID = 2345
Employee ID = 2345, Salary = 987654.32, Name = Deepti Chansouria
BUILD SUCCESSFUL (total time: 6 seconds)
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like asking the obvious -- does the second example now work as expected? does it compile? errors? And as Fred responded, what do you want us?
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Travis Westburry
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have only a day left to complete this assignment and it is really tripping me up.

I am supposed to load an array from a text file, separate it into two arrays. The file contains a list of about 20 'items' that are displayed like so:

7943 - 89.16
3024 - 21.32
5604 - 60.75

I am supposed to return the integer with its corresponding double, but i am only able to return each subscript.
I think that i am close, i just can't seem to get past the parrallel aspect, and i'm pretty sure that my search method is incorrect. Would somebody be willing to show me what i am doing wrong?

MainClass:


StoreInventory Class:
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you really using parallel arrays? That is very poor programming and very error‑prone. you should have an Item class which encapsulates number and price.

I have merged your two topics since their subjects appeared so similar to each other.
 
Travis Westburry
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Believe me, it is not by choice, my instructor is requiring it. It seems illogical to me, but i still have to do it none the less
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, if you have to you have to. If you have found the index in array 1 try looking at the same index in array 2.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Travis Westburry wrote:I have only a day left to complete this assignment and it is really tripping me up.
Don't waste your valuable time by writing such a things. Instead this sentence, use this time in explaining more about your problem.

Couple of things were not mentioned yet, might could help from loosing some marks.

1. You should not close scanner which pointing to a System.in (MainClass). I think Campbell Ritchie explained in details why here (<- link).
2. In your "StoreInventory" class, shouldn't you pass as arguments array sizes when instantiating class "StoreInventory" on Line 5 in a "MainClass"?
3. Why you're not using constructor of "StoreInventory" class to initialise your members?
 
Taylor Hall
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't waste your valuable time by writing such a things. Instead this sentence, use this time in explaining more about your problem.


You shouldnt waste your valuable time critquing something as small as this. It was obviously written to provide readers (like myself) with sense of urgency as well as detail.
Travis immediately followed up this statement by informing us of exactly what he needs help with and exactly what his instructor expects.
I for one, appreciate all the previous posts as they have helped me in my own endevours.
Good luck Travis!
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Travis, I took important parts you mentioned:
import a file and search for a specific piece of data that is requested by a user.
The output must return something similar to:
Sequential found ID number 77470, and its price is $49.55.
I am supposed to load an array from a text file, separate it into two arrays.
I am supposed to return the integer with its corresponding double

1. So, you mentioned you need to use two arrays, one for storing integers, another for storing doubles. Each of two arrays are being populated by reading corresponding values from file.
2. In this case you should be getting two arrays of the same length by reading given file's content. Each index of two separate arrays suppose to correspond to the pair of numbers you'll need to print based on users search.
3. Do you think would it work if you'd look for a users wanted "id" in a first array (or you can search by the price also), once you find that - record index. So you'd know, that this index in both arrays corresponds to the pair you're looking for.
And what is left to do - to print two array's contents at places of your found and recorded index.

I'm not sure about your full requirements, so my suggestions could be inaccurate. If you still need help, tell and post what you got recently. I still be here for an hour or so, so might I could help at some point.

Addition: sequential search is a linear search, so, the elements not necessarily needs to be sorted (so you don't need to sort your arrays and use binary search, actually it simplifies your things, because by sorting you could loose synchronisation by sorting id's and prices). Just use simple "for" or "while" loop to go through array starting from 0 position to n-1 and loop for your desired "ID number" or "Price" and that is it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!