Welcome to Dream.In.Code
Become a Java Expert!

Join 150,427 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,070 people online right now. Registration is fast and FREE... Join Now!




Help me please!

 
Reply to this topicStart new topic

Help me please!, compare char Array and char

cmorb
4 Apr, 2008 - 09:54 AM
Post #1

New D.I.C Head
*

Joined: 4 Apr, 2008
Posts: 4

Hi!
I have some problem.
I want to compare charArray and char
I keeps character in charArray such as:
char[] charArray = {'J','N','E'}
and get input from keyboard each character keeps in variable charInput
This code below check character by character and no run on index
CODE
public static int Check(char[] charArray , char inputChar){            for(int index = 0;index<charArray.length;index++){                if(charArray[index] == inputChar){                                    index++;                                    System.out.println("equals");                                                                                }            }                        return index; }


If I want to specify first index for check is index[0]
and then if index[0] are equal let check next index (index [1],... until last index)
How to solve this problem?
Thanks.

User is offlineProfile CardPM
+Quote Post

cmorb
RE: Help Me Please!
4 Apr, 2008 - 10:05 AM
Post #2

New D.I.C Head
*

Joined: 4 Apr, 2008
Posts: 4

sorry,this my code
CODE
public static int Check(char[] charArray , char inputChar){
     for(int index = 0;index<charArray.length;index++){    
           if(charArray[index] ==inputChar){
                             index++;        
                             System.out.println("equal");
                             return index;
            }
      }        
}

User is offlineProfile CardPM
+Quote Post

pbl
RE: Help Me Please!
4 Apr, 2008 - 10:16 AM
Post #3

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,594



Thanked: 233 times
Dream Kudos: 75
My Contributions
This code below check character by character and no run on index
CODE

public static int Check(char[] charArray , char inputChar){
  for(int index = 0;index<charArray.length;index++){    
      if(charArray[index] == inputChar){                    
         index++;
         System.out.println("equals");                    
      }
}

return index; }


QUOTE

If I want to specify first index for check is index[0]
and then if index[0] are equal let check next index (index [1],... until last index)
How to solve this problem?
Thanks.


Don't really understand what you mean. But let's guess:

- You do not have to increment index if it is found, you have your answer
- your code cannot compile because index is not defined outside the loop

java

public static int Check(char[] charArray , char inputChar){
for(int index = 0; index<charArray.length; index++){
if(charArray[index] == inputChar){
System.out.println("equals at index: " + index);
return index;
}
}
// not found
return -1;
}


This post has been edited by pbl: 4 Apr, 2008 - 10:25 AM
User is online!Profile CardPM
+Quote Post

pbl
RE: Help Me Please!
4 Apr, 2008 - 10:23 AM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,594



Thanked: 233 times
Dream Kudos: 75
My Contributions
oups !!! a } missing

java

public static int Check(char[] charArray , char inputChar){
for(int index = 0; index<charArray.length; index++){
if(charArray[index] == inputChar){
System.out.println("equals at index: " + index);
return index;
}
}
// not found an int method must return and int
return -1;
}

[/quote]

User is online!Profile CardPM
+Quote Post

cmorb
RE: Help Me Please!
4 Apr, 2008 - 11:03 PM
Post #5

New D.I.C Head
*

Joined: 4 Apr, 2008
Posts: 4

Thank for that.
But this code check character by character ,
but I want indentify first check in index[0]
if index[0] equal then check next index
User is offlineProfile CardPM
+Quote Post

pbl
RE: Help Me Please!
5 Apr, 2008 - 06:57 AM
Post #6

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,594



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(cmorb @ 5 Apr, 2008 - 12:03 AM) *

Thank for that.
But this code check character by character ,
but I want indentify first check in index[0]
if index[0] equal then check next index


OK English is not my mother tongue but I really don't understand what you mean

What is index[0] ?
User is online!Profile CardPM
+Quote Post

cmorb
RE: Help Me Please!
5 Apr, 2008 - 02:27 PM
Post #7

New D.I.C Head
*

Joined: 4 Apr, 2008
Posts: 4

index[0] is first element in array. smile.gif
User is offlineProfile CardPM
+Quote Post

pbl
RE: Help Me Please!
5 Apr, 2008 - 07:49 PM
Post #8

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,594



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(cmorb @ 5 Apr, 2008 - 03:27 PM) *

index[0] is first element in array. smile.gif


the array is called charArray[]
index is a int
so I don't see how you can put [] after index
still don't understand what you want to do
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 08:46PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month