11 Replies - 2512 Views - Last Post: 26 January 2010 - 02:42 PM Rate Topic: -----

#1 Eagles05  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 01-November 09

For Loop - Only Once!

Post icon  Posted 26 January 2010 - 12:33 PM

I have a for loop that goes through a Music objects array. The array includes CD names, songs, and singers. A user enters a singer, and the for loop is supposed to write out all the CDs that the singer wrote.

My method looks like this.
public void printCDs(String singer)
{
	for(int i = 0; i < numSongs; i++)
   {
		 if(songs[i].getSinger.equals(singer)
		 {
			  System.out.println(songs[i].getCD)
		  }
	}
}



The problem with this is that it lists some CDs more than once. How would I change the loop to write out a CD only once?

Is This A Good Question/Topic? 0
  • +

Replies To: For Loop - Only Once!

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: For Loop - Only Once!

Posted 26 January 2010 - 12:44 PM

Quote

The array includes CD names, songs, and singers

i'm a bit confused.. CD names, songs and singers are of the same Object? or of the same hierarchy? how all are in the same array?
from your code it seems that songs array contains only song objects...

anyways, there are couple of ways to accomplish what you ask, but i think it would be better if you post the rest of the code, so i, or anyone else who is willing to help, could suggest the best and most suitable way for you :)
Was This Post Helpful? 0
  • +
  • -

#3 333OnlyHalfEvil  Icon User is offline

  • D.I.C Addict

Reputation: 24
  • View blog
  • Posts: 664
  • Joined: 20-March 09

Re: For Loop - Only Once!

Posted 26 January 2010 - 12:46 PM

Do you mean if you have the same CD/Singer listed in the array twice it will list the same CD twice? If that's the case and you don't want the code to do that, make an array that holds the displayed values and have the loop check the array of displayed values before displaying a new one.
Was This Post Helpful? 0
  • +
  • -

#4 Eagles05  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 01-November 09

Re: For Loop - Only Once!

Posted 26 January 2010 - 01:13 PM

Quote

i'm a bit confused.. CD names, songs and singers are of the same Object? or of the same hierarchy? how all are in the same array?
from your code it seems that songs array contains only song objects...

anyways, there are couple of ways to accomplish what you ask, but i think it would be better if you post the rest of the code, so i, or anyone else who is willing to help, could suggest the best and most suitable way for you :)


That's right, my songs array contains only song objects. The song objects have data members of singer, CD, and song.
Was This Post Helpful? 0
  • +
  • -

#5 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: For Loop - Only Once!

Posted 26 January 2010 - 01:19 PM

I would use a preliminary ArrayList; loop through the CD array, and if the element is not already in the list, add it. Otherwise, if it is already there, don't add it. That way you will only have one copy of each CD

After that you could go through the ArrayList and print out each element

This post has been edited by erik.price: 26 January 2010 - 01:20 PM

Was This Post Helpful? 0
  • +
  • -

#6 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: For Loop - Only Once!

Posted 26 January 2010 - 01:29 PM

yeah erik.price is correct.
anyway, if you want to choose a Collection, i would suggest a Set implemintation in your case. that way you dont even have to check if an object is already there.

however, if you want to use a simple array, as in your code, and not a Collection, you can sort your array by CD.
then iterate the songs array and keep the last CD name in a variable. then, compare the next CDs. if the CD was already printed, dont print it again.
Was This Post Helpful? 0
  • +
  • -

#7 Eagles05  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 01-November 09

Re: For Loop - Only Once!

Posted 26 January 2010 - 01:51 PM

View Posterik.price, on 26 Jan, 2010 - 12:19 PM, said:

I would use a preliminary ArrayList; loop through the CD array, and if the element is not already in the list, add it. Otherwise, if it is already there, don't add it. That way you will only have one copy of each CD

After that you could go through the ArrayList and print out each element


How do you know if it's already there though?
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,255
  • Joined: 27-December 08

Re: For Loop - Only Once!

Posted 26 January 2010 - 02:07 PM

With ArrayList, you can use the contains() method to check. When using a Set, it is automated. So for example, if you try to add an existing element, nothing will happen. And when using a for loop, you sort and check for duplicates. So for example, if your array or Collection looks like 1, 1, 3, 4, 5, 5, you would get the first element and check for duplicates. Then, when you get to a non-duplicate, repeat the process.
Was This Post Helpful? 0
  • +
  • -

#9 MikeJavaDev  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 26-January 10

Re: For Loop - Only Once!

Posted 26 January 2010 - 02:14 PM

I too would use a preliminary ArrayList; search in the CD array, and if the element is currently in the list, add it. else use break;That way you will only have one copy of each CD

Still i think i can simulate what you want.As seems from your qn songs[] is an array of objects of a class ,say Songs & getSinger is a function of that class that returns a String object, simply a string.

Now what you need to do is run your method u worked out once and then use a code that removes duplicate entries of the singers that is the array objects...I call this further after this is Packing of Array...

i have a code that packs integer arrays, if u can modify the datatypes to String it is just worth a abg of diamonds for you.


import java.io.*;
import java.util.*;
public class ArrayPack
{
LinkedList ll = new LinkedList();
int arr[] = {1,2,2,2,3,3,5,7,7,12};//Simulation Integr Array, convert this to String
void packing()
{
int j=0;
while(j<(arr.length))
{
if(linearsearch(arr[j]) ==false)
{
ll.add(arr[j]);
}
j++;
}
System.out.println(""+ll);
}
boolean linearsearch(int n)
{
boolean x=false;
int a=ll.size();
for(int i=0;i<a;i++)
{
if((Integer)ll.get(i)==n)
{
x=true;
}
}
return x;
}
}
To speed up the process you could rather use BinarySearch but that requires sorted data, so isn't viable.

i hope i have understood your problem in the right sense and tried a good little bit to help you out...
If u are able to pack the String Arrays then please mail me the code at milindshah92@gmail.com quoting subject as "Pack String Array "or post it here...

Thanks in advance
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,255
  • Joined: 27-December 08

Re: For Loop - Only Once!

Posted 26 January 2010 - 02:17 PM

View PostMikeJavaDev, on 26 Jan, 2010 - 05:14 PM, said:

If u are able to pack the String Arrays then please mail me the code at milindshah92@gmail.com quoting subject as "Pack String Array "or post it here...


Fixed. We'd rather use the forums as a means to help members so that everyone may learn, not just you and the OP. :)

Also, don't forget to use code tags when posting code, like so: :code:
Was This Post Helpful? 0
  • +
  • -

#11 Eagles05  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 01-November 09

Re: For Loop - Only Once!

Posted 26 January 2010 - 02:38 PM

How would a contians() method help me. The user enters in a singer. For that singer, I want to print out all the CDs he wrote. Some CDs are listed more than once. I want to avoid duplicates.
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,255
  • Joined: 27-December 08

Re: For Loop - Only Once!

Posted 26 January 2010 - 02:42 PM

Here's an example of what I mean. Basically, it will allow you to check for duplicates. Given this information, you can simply avoid adding duplicates.

class Singer{
	private ArrayList<String> cdNames; 

	public Singer(){cdName = new ArrayList<String>();}

	public boolean add(String cd){
			//if the ArrayList doesn't contain the cd
		  if(!cdNames.contains(cd)){
			   cdNames.add(cd); //add it
			   return true; //and return that you added it
		  }
		  //otherwise, return false
		  return false;
	 }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1