Java - How do i make a string read and count amounts in another string ( -
i'm having trouble activating counter++. far s2 able read s1, cannot count amount of occurrences. appreciated. (i realize working in wrong string, helps me create solution here first , send second string, poor logic?)
sorry dumb question new @ programming
//i need scanner reads write scanner should count occurrences of char scanner declared scanner ask " write something" string s. = nextline etc new scanner asks letter string s1 = next line blb input = int count = stringutils.countmatches(s1); system.out.print(amount ) //
public class task07 { public static void main(string[] args) { scanner s1 = new scanner(system.in); system.out.println("write something"); string text = s1.nextline(); //reads user input value scanner s2 = new scanner(system.in); // missing smth limits length of s2 system.out.println("geb letter"); string letter = s2.nextline(); int counter = 0; boolean found; found = text.contains(letter); if (found == true) { counter++; } else { system.out.println(counter); } // use counter 6 here need way tell counter // should add +1 every time occurs in // other scanner /* problems: text recognizer boolean - doesnt activate counter - doesnt activate counter based on x times occurence - doesnt limit "letter" 1 char - */ } }
basically loop simple way count character occurrences in string. use like
int counter = 0; (int = 0; < text.length(); i++) { if (text.charat(i) == letter.charat(0)) { counter++; } }
Comments
Post a Comment