Help me please!!!


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Help me please!!!

  1. #1
    Join Date
    Mar 2008
    Posts
    6

    Help me please!!!

    I want to compare between char array and char

    Code:
    //compare 2  char  with for loop
    import java.util.Arrays;
    class TestCheck{
    	public static void main (String args[]){
    		String str = "AB";
    		int len,len2;
    		char[] charArray = str.toCharArray();
    		char inputChar;
    		inputChar = 'A';
    		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);
    	
    		///////////////////////////////////////////////////////
    		//compare two arrayList
    		
    			for(int index = 0;index<charArray.length;index++){
    					if(charArray[index] == inputChar){
    						System.out.println("index  "+index+"  equals");
    					}else{
    						System.out.println("index  "+index+"  not equals");
    					}
    			}
    	
    		
    	
    	
    	}
    }
    This code no error,but I want to declare method Check() for compare charArray and inputChar
    I 'm niebie in java.I want some advice for declare method Check.
    Thanks.

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Ok, you say you aren't getting any errors?

    Do you get the right result when you run this?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  3. #3
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Some thoughts:

    you can have your str for your class and pass in your inputChar as an agument to your checkChar( char inputChar ) method, then loop through your string to test if inputChar == str.charAt(index) ... return index if found or -1 if not found ...

    also - you assign the value of length to len ... why aren't you using len as your loop sentinel rather than accessing your arrayList every loop?

  4. #4
    Join Date
    Mar 2008
    Posts
    6
    Thank for that.

  5. #5
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Did what nspils post resolve your issue or do you still have questions?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center