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

  • SEARCH
  • Home
  • 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 731575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:02:33+00:00 2026-05-14T07:02:33+00:00

I just want to know where am i wrong here: import java.io.*; class Tokens{

  • 0

I just want to know where am i wrong here:

import java.io.*;


    class Tokens{   
            public static void main(String[] args)
            {
               //String[] result = "this is a test".split("");

                String[] result = "4543 6546 6556".split("");
                boolean flag= true;         

                String num[] = {"0","1","2","3","4","5","6","7","8","9"};
                String specialChars[] = {"-","@","#","*"," "};


                for (int x=1; x<result.length; x++)
                {   
                    for (int y=0; y<num.length; y++)
                    {
                        if ((result[x].equals(num[y])))
                        {
                            flag = false;
                            continue;

                        }
                        else
                        {
                            flag = true;
                        }


                        if (flag == true)
                        break;

                    }   

                if (flag == false)
                break;

                }           

                System.out.println(flag);

            }

    }
  • 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-05-14T07:02:34+00:00Added an answer on May 14, 2026 at 7:02 am

    Asides from the regex suggestion (which is a good one), it would seem to make more sense to deal with arrays of characters rather than single-char Strings.

    In particular, the split("") call (shudder) could/should be replaced by toCharArray(). This lets you iterate over each individual character, which more clearly indicates your intent, is less prone to bugs as you know you’re treating each character at once, and is more efficient*. Likewise your valid character sets should also be characters.

    Your logic is pretty strangely expressed; you’re not even referencing the specialChars set at all, and the looping logic once you’ve found a match seems odd. I think this is your bug; the matching seems to be the wrong way round in that if the character matches the first valid char, you set flag to false and continue round the current loop; so it will definitely not match the next valid char and hence you break out of the loop with a true flag. Always.

    I would have thought something like this would be more intuitive:

    private static final Set<Character> VALID_CHARS = ...;
    
    public boolean isValidPhoneNumber(String number)
    {
        for (char c : number,toCharArray())
        {
            if (!VALID_CHARS.contains(c))
            {
               return false;
            }
        }
    
        // All characters were valid
        return true;
    }
    

    This doesn’t take sequences into account (e.g. the strings “——–** ” and “1” would be valid because all individual characters are valid) but then neither does your original code. A regex is better because it lets you specify the pattern, I supply the above snippet as an example of a clearer way of iterating through the characters.

    *Yes, premature optimization is the root of all evil, but when better, cleaner code also happens to be faster that’s an extra win for free.

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

Sidebar

Related Questions

I know I'm just thinking of the logic all wrong here but how do
Just want to know if publishing an application on Google play is allowed using
Just want to know how to read an attribute of a parent node from
I just want to know why both the paint and draw methods are used
i just want to know, how to write Code behind(Hyperlink1_Click()), for the hyper link
I just want to know what is the actual difference between private and protected
I just want to know how to access an array in SMARTY that have
I just want to know how to code a animation like the one on
I just want to know if it is possible for an iPad and iPhone
I just want to know is there any tool works with silverlight3 for UI

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.