1,105,331 Community Members

want help about ArrayList and interaction

Member Avatar
chmo
Newbie Poster
3 posts since Nov 2010
Reputation Points: -2 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Firstly... I am a new in Java...
I have a music dictionary that use an arreylist to store the terms and definition
The user use the terminal to interact with this dictionary.

private void addTerms () 
    {
        addTerm("Anathema","Atmosheric metal band from Liverpool, UK."
}

Because, I would like to make it more sophisticated, if someone write not a band but a letter eg A or B or C then the dictionary to give him as feedback all the bands that their first letter is A or B or C.

I am trying to do this with substring(0,1).
eg.

if (band.substring(0,1) = bandExists(band).substring(0,1)
{
System.out.println(i dont know what to print!!!!)
}

and another question...

if it works, and prints the bans names that their first letter is "A"

1. Agathodemon
2. Anathema
3. Annihilator
etc

how possible is to write a number eg 3 and to print out now the definition of the band?

Member Avatar
kramerd
Posting Pro in Training
404 posts since Sep 2010
Reputation Points: 29 [?]
Q&As Helped to Solve: 75 [?]
Skill Endorsements: 0 [?]
 
0
 

You should probably create a class to represent a band that has a name and a description. Then you can use substring on the name part, and you should be comparing the substring to the user input. For example, say the user input is in a string called answer. Then you could compare like this:

if (band.getName().substring(0, 1).equals(answer))
   ...

In order to get numbers to print for your output, you need a variable to keep track of how many names you have output so far. For example, say your ArrayList is called musicCollection. The the loop might look something like this:

int counter = 1;
for (Band band : musicCollection) {
   if (band.getName().substring(0, 1).equals(answer)) {
      System.out.println(counter + ". " + band.getName());
      counter++;
   }
}
Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
0
 

Essentially crossposts
Number 1
Number 2
Number 3
Number 4

Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
0
 

Possibly another.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article