Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8329087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:39:14+00:00 2026-06-09T01:39:14+00:00

I’m trying to count the number of characters that occur in a java string.

  • 0

I’m trying to count the number of characters that occur in a java string.

For example:

given the poker hand 6s/3d/2H/13c/Ad

how many times does the / character occur? = 4

The user can enter a different hand with a changing number of card variables so hardcoding a method to check for occurrences isn’t going to work.

A separator may be any one of: – / space (with only one separator type allowed to be used in one hand).
So I need to be able to check if either of the separators occurs 4 times otherwise the incorrect format has been given.

Here’s a some java code to give a better idea of what I’m trying to do:

    String hand = "6s/1c/2H/13c/Ad";
    System.out.println("Original hand: " + hand);

    // split the hand string into individual cards
    String[] cards = hand.split(hand);

    // Checking for separators
    // Need to check for the correct number of separators
    if(hand.contains("/")){
        cards = hand.split("/");
    } else if (hand.contains("-")){
        cards = hand.split("-");
    } else if (hand.contains(" ")){
        cards = hand.split(" ");
    } else {
        System.out.println("Incorrect format!");

    }

Any help would be great!

Also this is a school project/homework.

Edit 1——————————————————–

OK so here’s my code after your suggestions

    String hand = "6s 1c/2H-13c Ad";
    System.out.println("Original hand: " + hand);

    // split the hand string into individual cards
    String[] cards = hand.split("[(//\\-\\s)]");

    if (cards.length != 5) {
        System.out.println("Incorrect format!");    
    } else {

        for (String card : cards) {
            System.out.println(card);
        }
    }

The given hand above is not in the correct format because the user can only use ONE type of separator for a given hand. For example:

  • 6s/1c/2H/13c/Ad – correct
  • 6s-1c-2H-13c-Ad – correct
  • 6s 1c 2H 13c Ad – correct

How do I ensure that the user only uses ONE type of separator??

Cheers for the answers so far!

Edit 2 ——————————————

So playing around with nested if statements my code now looks like this:

    String hand = "6s/1c/2H/13c/Ad";
    System.out.println("Original hand: " + hand);

    // split the hand string into individual cards

    if(hand.contains("/")){
        String[] cards = hand.split("/");
        if(cards.length != 5){
            System.out.println("Incorrect format! 1");
        } else {
            for (String card : cards) {
                System.out.println(card);
            }
        }

    } else if(hand.contains("-")){
        String[] cards = hand.split("-");
        if(cards.length != 5){
            System.out.println("Incorrect format! 2");
        } else {
            for (String card : cards) {
                System.out.println(card);
            }
        }

    } else if(hand.contains(" ")){
        String[] cards = hand.split(" ");
        if(cards.length != 5){
            System.out.println("Incorrect format! 3");
        } else {
            for (String card : cards) {
                System.out.println(card);
            }
        }
    } else {
        System.out.println("Incorrect format! 4");
    }

This way works as intended but is ugly!

Any suggestions would be great cheers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T01:39:16+00:00Added an answer on June 9, 2026 at 1:39 am

    I wrote this before the first question edit, so I am responding to the original question and not its addendum question.

    In the documentation on String.split, it is unclear whether empty strings count as substrings. Notice that "--".split("-").length == 0. The question may implicitly guarantee that two or more characters separate delimiters, but that is a risky assumption and where Java’s String.split becomes problematic.

    This is a partial simpler implementation:

        char[] delims = {'/', ' ', '-'};
        int result = 0;
        for (char delim : delims) {
            for (int i = 0; i < hand.length(); i++) {
                if (hand.charAt(i) == delim) {
                    ++result;
                }
            }
        }
    

    Full code follows, with editorial comments intended for homework.

    interface Counter {
        int count(String hand);
    }
    class FirstCounter implements Counter {
        public int count(String hand) {
            String[] cards = hand.split(hand);
            if(hand.contains("/")){
                cards = hand.split("/");
            } else if (hand.contains("-")){
                cards = hand.split("-");
            } else if (hand.contains(" ")){
                cards = hand.split(" ");
            } else {
                // Prefer to fail fast unless your requirement
                // really is to only print "incorrect format"
                //System.out.println("Incorrect format!");
                throw new RuntimeException("Incorrect format!");
            }
            if (hand.endsWith("-") || hand.endsWith("/") || hand.endsWith(" ")) {
                return cards.length;
            }
            return cards.length - 1;
        }    
    }
    class SecondCounter implements Counter {
        public int count(String hand) {
            char[] delims = {'/', ' ', '-'};
            int result = 0;
            for (char delim : delims) {
                for (int i = 0; i < hand.length(); i++) {
                    if (hand.charAt(i) == delim) {
                        ++result;
                    }
                }
            }
            if (result == 0) {
                // This is a hack or inconsistent with requirements,
                // but necessary to match original posted code behavior
                throw new RuntimeException("Incorrect format!");
            }
            return result;
        }
    }
    class Main {
        private static int testCount = 0;
    
        static void realAssert(boolean condition) {
            if (!condition) {
                throw new AssertionError("Java has no real assert");
            }
        }
    
        static void test(Counter counter) {
            ++testCount;
            try {
                realAssert(counter.count("6s/3d/2H/13c/Ad") == 4);
                realAssert(counter.count("6s-3d-2H-13c-Ad") == 4);
                realAssert(counter.count("6s 3d 2H 13c Ad") == 4);
                // Don't forget boundary conditions
                realAssert(counter.count("6s-3d-2H-13c-") == 4);
                realAssert(counter.count("6s/3d/2H/13c/") == 4);
                realAssert(counter.count("6s 3d 2H 13c ") == 4);
                realAssert(counter.count("-6s-3d-2H-13c-") == 5);
                realAssert(counter.count("/6s/3d/2H/13c/") == 5);
                realAssert(counter.count(" 6s 3d 2H 13c ") == 5);
                realAssert(counter.count("--") == 2);
                // Remember to test error conditions
                try {
                    counter.count("foobar");
                    realAssert(false);
                } catch (RuntimeException e) {
                    // Catching RuntimeException is normally bad
                    // done only as example.
                    // Also normally bad, this is an empty catch
                    // block. These are sometimes useful, but always
                    // at least add a comment that explains that this
                    // catch block really should be empty, in this case
                    // because the test was meant to throw an Error.
                }
                try {
                    counter.count("foo/bar-baz");
                    // Left as exercise for reader, based on question
                    // it is possible this should be disallowed.
                    //realAssert(false);
                } catch (RuntimeException e) {
                    // Ditto above, intentionally empty catch
                }
                System.out.println("Test " + testCount + " succeeded");
            }
            catch (Error e) {
                // XXX: Don't catch Error in non-example code
                System.out.println("Test " + testCount + " failed");
                /* Normally don't use printStackTrace either */
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            test(new FirstCounter());
            test(new SecondCounter());
        }
    }
    

    Just for education, the regular expression approach can be good. The entire solution takes one line of Ruby, hand.split(/[\-\/ ]/, -1).length - 1.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I need a function that will clean a strings' special characters. I do NOT
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.