This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Searching and displaying results in a multi-dimensional array. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Searching and displaying results in a multi-dimensional array." Watch "Searching and displaying results in a multi-dimensional array." New topic
Author

Searching and displaying results in a multi-dimensional array.

Scotty Steven
Ranch Hand

Joined: Jan 27, 2012
Posts: 80
I'm working on a two column array search utility, but I'm stuck on a couple of matters.

1. I want to insure that the utility only searches the first column of the array and ignores the second.
2. When it does find the word in the first column, I want it to display the second column on line 109.

I've got the majority of it written, but like I said, I'm stuck. Help is always appreciated.


Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4531
    
    5

Not a GUI problem. Moving to Beginning Java.

edit Also breaking some extra long code lines that are disturbing the page formatting


luck, db
There are no new questions, but there may be new answers.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

Hey there, Scotty. You didn't exactly specify what you're stuck on. Please TellTheDetails.

Anyway, have you considered using a Map<String, String> for this problem? It's perfect, it will solve most of your problems.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

I answered this yesterday:

Scotty Steven wrote:I'm working on a two column array search utility, but I'm stuck on a couple of matters.

1. I want to insure that the utility only searches the first column of the array and ignores the second.


What problem are you having with that? Just look in arr[x][0], and not in arr[x][1].

2. When it does find the word in the first column, I want it to display the second column on line 109.


So display arr[x][1].


And the OP even replied and thanked me.

Was this a double post? Or do we lose replies when moving a thread?
Scotty Steven
Ranch Hand

Joined: Jan 27, 2012
Posts: 80
Double post. I accidentaly posted in the Swing group. I realized it later, and reposted in beginners, which is were @Jeff Verdegan gave me the help needed. I marked this post (and the other) as soled last night, but doing so seems to have no effect.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Jeff Verdegan wrote:
And the OP even replied and thanked me.

Was this a double post? Or do we lose replies when moving a thread?


Double post. I chose to lock the other.
Scotty Steven
Ranch Hand

Joined: Jan 27, 2012
Posts: 80
Lock this one too. It's answered and done!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
We don’t close threadsd as a routine. I don’t think you have finished. There is a far better solution using a Map<String, String>
Also, to become pedantic, you don’t have a multi-dimensional array. You have an array of arrays. Which is, by the way, better than a 2D array.
Scotty Steven
Ranch Hand

Joined: Jan 27, 2012
Posts: 80
I don't know anything about Map<String, String>, but after getting the answer I was looking for last night, I formatted over 700 lines into an array, which seems to be working. I don't want to reformat the information again, as that took over 6 hours to do. I'm in if it uses the same format as an array, I'd consider it.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Scotty Steven wrote:I don't know anything about Map<String, String>, but after getting the answer I was looking for last night, I formatted over 700 lines into an array, which seems to be working. I don't want to reformat the information again, as that took over 6 hours to do. I'm in if it uses the same format as an array, I'd consider it.


There's no good reason for putting all that in your Java code in the first place. It should go into a file that your code reads. Regardless of where it is though, if you're using a decent editor with regex replace capabilities, it will be trivial to change it from one to the other. Of course, if it's in a separate file as it should be, then the data format wouldn't change anyway; you'd just change a few lines of code at the point where you're reading and storing it.

(And to be honest, I don't think a String[][] OR a Map<String, String> is appropriate here. It looks like you have a glossary, so I'd defined a class named GlossaryEntry or Term or something appropriate to your intended design and use. That class would have as member variables the term and its definition. Then for lookups, I'd use use a Map<String, GlossaryEntry>.
Scotty Steven
Ranch Hand

Joined: Jan 27, 2012
Posts: 80
I just set it up with a main method for testing purposes only. It is part of a larger program, and the array is its own class.

Pretty much everything else you said went completely above my head. I am really new at this.

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Scotty Steven wrote:I just set it up with a main method for testing purposes only. It is part of a larger program, and the array is its own class.

Pretty much everything else you said went completely above my head. I am really new at this.



700 data entries do not belong in the source code. They belong in a file or database.

As for the rest, well, you've heard of defining classes to represent the concepts you're dealing with, right? Like if I said that a school registration program would have a Course class and a Student class, or a banking program would have a Customer class and an Account class, you'd understand that, right? Well, here you're defining a glossary, and each entry has a term ("Java") and a definition, ("A programming language that..."), so doesn't it make sense to have a class like GlossaryEntry, with "term" and "definition" member variables.?
Scotty Steven
Ranch Hand

Joined: Jan 27, 2012
Posts: 80
It does make sense, but I have no clue how to even approach something like that. I trying to teach myself programming for fun, and I really haven't that far into my learning. I decided to create a project where every new skill I learn, I practice it on a digital cheat-sheet project.

Oh boy. Now I get to try and learn database design. That sounds like a huge undertaking.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Scotty Steven wrote:It does make sense, but I have no clue how to even approach something like that. I trying to teach myself programming for fun, and I really haven't that far into my learning.


Okay, fair enough. So when you get to that point in your education, come back to this program and see if you can improve it.

Oh boy. Now I get to try and learn database design. That sounds like a huge undertaking.


It is huge, and I wouldn't go there yet if I were you. Get the basics of Java down first. Start with just a text file of your entries. Tackle the database later.

Good luck!
Scotty Steven
Ranch Hand

Joined: Jan 27, 2012
Posts: 80
Sounds good. I'm doing the practice of the "core" concepts in command line, but then I'm trying to apply them to Swing display. So, in theory, I'm trying to learn two for one!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Searching and displaying results in a multi-dimensional array.