Takacs
Takacs

Reputation: 31

counting specific characters in string

I'm new at coding. If anyone can help me out: looking for a solution at this code, how to count how many times the "END" can be found at the randomly generated string.

Thank you guys!

public class Tombos {

  public static void main (String[] args) throws FileNotFoundException {

    PrintStream out1 = System.out;
    PrintStream out2 = new PrintStream(new File("out.txt"));

    String betuk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";  
    int i;
    for (i=0; i<1000; i++) {  
      char random = betuk.charAt((int)(26*Math.random()));      
      out2.print(random);
    }
    Scanner beolvas = new Scanner(new File("out.txt"));
    String x = beolvas.next();
    if (x.contains("END")) {
      out1.print( "tartalmaz");    // include "END"
    } else {
      out1.print( "nem tartlmaz");  // not include "END"
    }
  }
}

Upvotes: 2

Views: 142

Answers (6)

Michael Yaworski
Michael Yaworski

Reputation: 13483

Here's an algorithmic approach to it:

String x = "ENDDOFJUESADNFENDEJFDNFDENDD"; // arbitrary String

boolean moreMatches = true;
int index = 0, count = 0;

while(moreMatches) {

    if (x.indexOf("END", index) > -1)
    {
        index = x.indexOf("END", index) + 1;
        count++;
    } else {
        moreMatches = false;
    }
}

System.out.print(count); // prints 3

Please read the documentation for String.indexOf(String s, int fromIndex) to understand what I've done.

Every time I find the word "END" in the String, I search again, but this time search for "END" starting from the position after where I found it last time. When I can't find it anymore, I'm done the loop.

Upvotes: 0

Petr Mensik
Petr Mensik

Reputation: 27526

In case you are a user of Apache Commons library, StringUtils#countMatches would do the job.

int count = StringUtils.countMatches(x, "END");

If not, I would probably use String#split

int count = x.split("END").length - 1;

It's a little bit ugly but it's easy and it's an one-liner:)

Upvotes: 2

Josh
Josh

Reputation: 1553

I'm not sure what your exact intention is with the random string and the file scanner, but a basic way to do the string counting you asked about would be to use a regular expression (the Pattern class):

int endCount = 0;
Matcher matcher = Pattern.compile("END").matcher(x);
while (matcher.find()) {
    endCount++;
}

Note that if you're doing this somewhere in a loop or calling the method over and over, it'll eventually be more performant to make the Pattern.compile() call happen somewhere where it's only called once, and the resulting Pattern stored for reuse.

Upvotes: 0

Eliza Weisman
Eliza Weisman

Reputation: 843

Here's what I would do:

First, make a new int variable. Let's call it count. int count;

Then, in the body of the for loop, add one to count every time you find a matching string, like this: count = count + 1.

The program should look something like this:

int i;
int count;

for (i=0;i<1000;i++) {  
    char random=betuk.charAt((int)(26*Math.random()));      
    out2.print(random);
}

Scanner beolvas=new Scanner(new File("out.txt"));
String x=beolvas.next();

if (x.contains("END")) {
    out1.print( "tartalmaz");    // include "END"
    count = count + 1;           // add one to the count of matches
} else {
    out1.print( "nem tartlmaz");  // not include "END"
}

Upvotes: 0

user3185894
user3185894

Reputation: 37

Just make a new int variable like int count = 0; then

 if (x.contains("END)) {
count++;
}

Everytime you find a "END" increment your variable and afterwards just print your variable.

Upvotes: 0

Raul Rene
Raul Rene

Reputation: 10280

You can use the countMatches method of the StringUtils class.

int count = StringUtils.countMatches(x, "END");

It counts the number of times a String appears in another. See documentation.

Upvotes: 3

Related Questions