Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #21 (permalink)  
Old 04-23-2008, 04:25 PM
Member
 
Join Date: Apr 2008
Posts: 33
raj reddy can only hope to improve
hi Sanjeev


The code is exactly working fine.

BUT I NEED TO GET THE PRIZE LEVELS DEPENDING UPON THE NUMBER OF COUNTS;


I HAVE GIVEN THE count++ in the for loop.


but am not getting the updated count value ;



can u please check and resubmit the code whether the count++ i have given in the loop is at correct location or not.





import java.util.ArrayList;
public class CompareList
{

public static void main(String[] args){
ArrayList list1 = new ArrayList();
list1.add(10);
list1.add(20);
list1.add(30);
list1.add(40);
ArrayList list2 = new ArrayList();
list2.add(11);
list2.add(12);
list2.add(13);
list2.add(14);
list2.add(15);
list2.add(16);
list2.add(17);
list2.add(18);
list2.add(19);
list2.add(20);

int s = 1;

int count=1;

for (int i=0;i<list2.size();i++) {
if(s <= 4){
System.out.println("here..:"+list2.get(i));
for (int j=0;j<list1.size(); j++) {
if(list2.get(i).equals(list1.get(j)))

count++;

System.out.println("equals..:"+list1.get(j));
}
s++;
}else{
System.out.println("Checked Four elements, Now checking next Four");
s = 1;
i--;
}
}


if(count==al2.size()-4) {
System.out.println("The level of the prize is FOUR "+count);
}else if(count==al2.size()-3) {
System.out.println("The level of the prize is THREE"+count);
} else if(count==al2.size()-2) {
System.out.println("The level of the prize is TWO "+count);
} else if(count==al2.size()-1) {
System.out.println("The level of the prize is ONE "+count);
} else if(count==al2.size()-0) {
System.out.println("The level of the prize is ZERO "+count);
}
else System.out.println("There is no prize level ");

}
}

Last edited by raj reddy : 04-23-2008 at 04:28 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #22 (permalink)  
Old 04-23-2008, 04:51 PM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Delhi(India)
Posts: 237
sanjeevtarar is on a distinguished road




I did not understand your count logic.
You will get count value : Number of matches (start count with 0).
count is giving correct value

check this code
Code:
import java.util.ArrayList; public class CompareList { public static void main(String[] args){ ArrayList list1 = new ArrayList(); list1.add(10); list1.add(20); list1.add(30); list1.add(40); ArrayList list2 = new ArrayList(); list2.add(11); list2.add(12); list2.add(13); list2.add(14); list2.add(15); list2.add(16); list2.add(17); list2.add(18); list2.add(19); list2.add(20); int s = 1; int count=0; for (int i=0;i<list2.size();i++) { if(s <= 4){ System.out.println("Values..:"+list2.get(i)); for (int j=0;j<list1.size(); j++) { if(list2.get(i).equals(list1.get(j))){ count++; System.out.println("equals..:"+list1.get(j)); } } s++; }else{ System.out.println("Checked Four elements, Now checking next Four"); s = 1; i--; } } System.out.println(count); if(count==list2.size()-4) { System.out.println("The level of the prize is FOUR "+count); }else if(count==list2.size()-3) { System.out.println("The level of the prize is THREE"+count); } else if(count==list2.size()-2) { System.out.println("The level of the prize is TWO "+count); } else if(count==list2.size()-1) { System.out.println("The level of the prize is ONE "+count); } else if(count==list2.size()-0) { System.out.println("The level of the prize is ZERO "+count); } else System.out.println("There is no prize level "); } }

What exactly mean...of .... if(count==list2.size()-4)




__________________
sanjeev,संजीव
Bookmark Post in Technorati
Reply With Quote
  #23 (permalink)  
Old 04-23-2008, 05:13 PM
Member
 
Join Date: Apr 2008
Posts: 33
raj reddy can only hope to improve
if(count==list2.size()-3) means






the count value will be added if one value or more values are matchable for every loop



this count should be added by using count++



ie for every 4 values (which exist in list1) and for every 4 values (which exist in list2)



suppose 2 values were there means count will be 2 for that loop




if(count==list2.size()-3) means




in the above one list2.size()-3 is





the actual value of list2.size() is 4


if we subtract 1 value from that one is 3



so


if 3(this is count value)=3 (this is list2.size()-3) value

{

System.out.println("The level of the prize is THREE"+count);
}



SO IN THE ABOVE CODE IN WHICH FOR LOOP I HAVE TO PLACE THE count++

Last edited by raj reddy : 04-23-2008 at 05:16 PM.
Bookmark Post in Technorati
Reply With Quote
  #24 (permalink)  
Old 04-24-2008, 02:21 PM
Member
 
Join Date: Apr 2008
Posts: 33
raj reddy can only hope to improve
Hi sanjeev








Requirement : count variable should increment upto the last comparison of the arrays.

ie count=1,2,3,4,5....upto last comparison of the arrays like that


and also count variable should be increased if and only if the two arrays values are equal

everything is fine but only the count variable is not incrementing for the last comparison of the arrays

Let us think that there are two arrays al1 and al2;




MY CODE:

al1={1,2,3}


al2=(1,2,3,4,5,6,7,8,9}



int count=0;


for (int i=0;i<al1.size();i++) {
{

for (int j=0;j<al2.size(); j++) {



if(al1.get(i).equals(al2.get(j)))

count++;


}


Actually the count variable should increment for every comparison of the al1 and al2.


MY PROBLEM IS : THe variable is incrementing fine for every comparison , but for the last comparison of the arrays the count variable is not incrementing.


can u validate the count variable such a way that i have to get the last comparison value of the two arrays.

PLS SEND ME THE UPDATED CODE
Bookmark Post in Technorati
Reply With Quote
  #25 (permalink)  
Old 04-25-2008, 04:36 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,264
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
What you mean that last comparison not increment the counter. It should be 3, and seems to me your code give the correct value.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best?Vote Now
Bookmark Post in Technorati
Reply With Quote
  #26 (permalink)  
Old 04-25-2008, 04:37 PM
Member
 
Join Date: Apr 2008
Posts: 33
raj reddy can only hope to improve
hi eranga

when the two arrays were comparing


ie if(al1.get(i).equals(al2.get(j)))


the count variable should be incremented


but for the last comparison of the values in the arrays

the count variable is not incrementing

if u have any queries reply me
__________________
reddy
Bookmark Post in Technorati
Reply With Quote
  #27 (permalink)  
Old 04-25-2008, 04:48 PM
Member
 
Join Date: Apr 2008
Posts: 33
raj reddy can only hope to improve
hi Eranga


the count variable should be incremented for every element comparison of the arrays

for eg

int count=0;

al1.(2)=al2.(2)
count++;

the above arrays were equal, so the count variable will be incremented to 1;

then go for next comparison

al1.(3)=al2.(3)
count++;

the count variable will be incremented to 2;


like that the count variable will be go on incrementing if the number of equal comparisons were there


what my problem is :


the count variable is not incrementing for the last comparison of the arrays


can u check in the code i have sent above
__________________
reddy
Bookmark Post in Technorati
Reply With Quote
  #28 (permalink)  
Old 04-28-2008, 05:13 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,264
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Your code give the correct value.

May be you refer the count variable in wrong place. According to your array lists the count variable hold the 3 as final result. It can't be 2 as you said.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best?Vote Now
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to compare the elements of these two arraylists raj reddy Web Frameworks 0 04-17-2008 04:12 PM
A Map implemented with ArrayLists Java Tip java.lang 0 04-16-2008 11:29 PM
recursively searching through arraylists newtojava7 New To Java 1 03-17-2008 03:36 AM
arraylists problem newtojava7 New To Java 1 03-12-2008 08:38 AM
Compare 2 XML Peter XML 1 07-05-2007 03:58 AM


All times are GMT +3. The time now is 10:50 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org