The Artima Developer Community
Sponsored Link

Java Answers Forum
searching a character from a string

3 replies on 1 page. Most recent reply: Nov 8, 2002 3:18 PM by edwin mo

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
edwin mo

Posts: 7
Nickname: ct976
Registered: Oct, 2002

searching a character from a string Posted: Nov 8, 2002 9:52 AM
Reply to this message Reply
Advertisement
hi. i am having trouble with incrementing a counter for this string

i have a string, eg. String s = "computer"
i have a user input that guesses if the character entered is in the string s.
i want to count how many wrong guesses there are, each time it is entered.

i did something like this, which only gives the TOTAL number of wrong guesses:

for (int i=0; i < s.length(); i++) {
if( character != s.substring (i, i+1) {
numberGuess++;
}
}

however this doesnt work because it doesnt stop and return that number, but it just adds on continously for the entire length of the word. does anyone have suggestions?


Daniel Ray

Posts: 53
Nickname: budoray
Registered: Oct, 2002

Re: searching a character from a string Posted: Nov 8, 2002 2:24 PM
Reply to this message Reply
Well, you're not correctly testing to see if you have a match. Also, to enchance your for loop .. never have a method as the conditional ie. s.length(). This method will be called each time through the loop.

Try something like this. You'll have to modify it to test.

public void checkForMatch(String s){
boolean match = false;

int length = s.length();

String letter;
String guess = "a";

for(int i=0; i<length && match == false; i++){
letter = s.substring(i, i+1);

if(letter.equalsIgnoreCase(guess)){
match = true;
}

}
if(!match){
wrongGuess++;
}

}

Daniel Ray

Posts: 53
Nickname: budoray
Registered: Oct, 2002

Re: searching a character from a string Posted: Nov 8, 2002 2:43 PM
Reply to this message Reply
Here's a test class so that you can see it work.

public class MatchString {

public String correctWord;

public int wrongGuesses;

public MatchString(){
correctWord = "computer";

wrongGuesses = 0;

}

public void test(String guess){
boolean match = false;

int length = correctWord.length();

String letter;

for(int i=0; i<length && match == false; i++){
letter = correctWord.substring(i, i+1);

if(letter.equalsIgnoreCase(guess)){
match = true;
}

}
if(!match){
wrongGuesses++;
} else{
wrongGuesses = 0;
}

System.out.println("Wrong guesses " + wrongGuesses);

}

public static void main(String[] args) {
MatchString ms = new MatchString();

ms.test("a");
ms.test("b");
ms.test("d");
ms.test("f");
ms.test("g");

System.out.println("Wrong guesses should equal 5");

ms.test("c");
ms.test("g");

System.out.println("Wrong guesses should equal 1");

}

}

edwin mo

Posts: 7
Nickname: ct976
Registered: Oct, 2002

Re: searching a character from a string Posted: Nov 8, 2002 3:18 PM
Reply to this message Reply
thanks i got it

Flat View: This topic has 3 replies on 1 page
Topic: hey i know u guys dont like hwk help but i jst need some help getingstarted Previous Topic   Next Topic Topic: Java Refresh?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use