Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
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
  #1 (permalink)  
Old 03-26-2008, 10:08 AM
Member
 
Join Date: Mar 2008
Posts: 8
googgoo is on a distinguished road
Help me please!!!
I want to compare between char array and char
charArray keeps {'A','B'}
inputChar keeps 'A'
I want to compare charactor by charactor.
I expect this code below result is :
>>index[0] equals
>>index[1] not equals
but when compile this code result is:
>>index[0] equal
I should to solve this problem?

Code:
//compare 2 char with for loop import java.util.Arrays; class TestCheckMethod{ public static void main (String args[]){ String str = "AB"; int len,len2; char[] charArray = str.toCharArray(); char inputChar; inputChar = 'B'; System.out.print("\n"); System.out.print("String is "+str); System.out.print("\n"); System.out.print("\n"); for(int i = 0;i<charArray.length;i++){ System.out.println("char array is>>"+charArray[i]); } len = charArray.length; System.out.println("length of char array is>>"+len); System.out.print("\n"); System.out.println("input char is>> "+inputChar); Check(charArray,inputChar); } /////////////////////////////////////////////////////// //compare charArray and inputChar public static int Check(char[] charArray , char inputChar){ int index = 0; for(index = 0;index<charArray.length;index++){ if(charArray[index] == inputChar){ System.out.println(">>index "+index+" equals"); return index; }else{ System.out.println(">>index "+index+" not equals"); return index; } } return index; } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-26-2008, 05:04 PM
Member
 
Join Date: Mar 2008
Posts: 5
markus-sukram is on a distinguished road
It is unnecessary to create a new character array, a string is already an array of characters.
for (i = 0; i < str.length; i++){
if(str.charAt(i) == 'B') {System.out.println("index "+i+" is equal to B");}
else {System.out.println("index "+i+" is not equal to B");}
}
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-03-2008, 10:59 AM
Member
 
Join Date: Mar 2008
Posts: 8
googgoo is on a distinguished road
Thank for that.
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



All times are GMT +3. The time now is 09:03 AM.


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