Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Test Question(Introduction Course)

  1. #1
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Question Test Question(Introduction Course)

    Hey guys,

    Here is the question: Design Java code to find and return the frequency of string b in string a.

    For example , if string a = "abcdabcfabxyabd" and string b = "ab"
    The frequency should be equal to 4

    Here is my code:

    import java.util.*;
    public class StringFrequency_A_in_B 
    {
    	 public static void main(String[] args) 
    	    {
    	    	Scanner scan = new Scanner(System.in);
    	    	String s1, s2;
    	    	String commonChars = "";
    	    	int lengthCommon = 0;
     
                    System.out.println("The program will find the characters " + "quantity/frequancy \nof the first string in the second string");
    		System.out.println("Now, enter two strings in seperate lines");
     
    		s1 = scan.nextLine(); 
    		s2 = scan.nextLine();
     
    		int len = s1.length(); // number of characters in string A
     
    		for (int i = 1; i < len ; i++)
    		{
    		    char ch = s1.charAt(i);
    		    if (s2.indexOf(ch) != -1)
    		    {
    		        commonChars = commonChars + ch;
    		    }
    		} // end of the loop
     
    		lengthCommon = commonChars.length();
     
                    System.out.println("The common characters: " + commonChars);
                    System.out.println("The frequency of common characters: ” + lengthCommon);
     
    		System.exit(0);
    	    }
    }

    The thing is that it doesn't count the frequency of "ab", but the quantity of a's and b's, so it shows 8. How do I make it count frequency of "ab" string?


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Test Question(Introduction Course)

    Don't look for common chars since char is not what you're interested in. Instead search for Strings within a String.

  3. #3
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Test Question(Introduction Course)

    So I should use String instead of chars, right??

    --- Update ---

    I have changed my code and it shows the right answer, but the output shows like a few Errors before the answer:

    Error
    Error
    Error
    Error
    Error
    Error
    4

    import java.util.*;
    public class FrequencyAinB 
    {
    	public static void main (String [] args)
    	{
    	Scanner scan = new Scanner(System.in);
    	String a,b;
    	int freq = 0;
    	int pos;
    	a = "abcdabcfabxyabd";
    	b = "ab";
     
    	int lenA = a.length() - 1;
     
    	while(lenA>0)
    	{	
    		lenA--;
    		pos = a.indexOf(b);
    		if(pos == -1)
    		{
    			System.out.println("Error");
    		}
    		else
    		{
    			a = a.substring (pos + 1);
    			freq++;
    		}
    	}
    	System.out.println(freq);
    	}
    }

    What's wrong with logic?

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Test Question(Introduction Course)

    You're while loop looks quite a bit strange. Why are you dealing with a length variable, lenA? What's the point of that? Why are you creating a substring of the a String? Why not use one of the overrides of the indexOf method that takes a second parameter?

Similar Threads

  1. Challenging Java Question: Test your Java skill by grouping these terms
    By karthickk3 in forum Java Theory & Questions
    Replies: 3
    Last Post: July 18th, 2012, 10:10 PM
  2. Introduction + Question
    By mikaelkuru in forum Member Introductions
    Replies: 1
    Last Post: April 13th, 2012, 07:28 AM
  3. Introduction
    By pradipdas in forum Member Introductions
    Replies: 0
    Last Post: March 23rd, 2012, 05:15 AM
  4. Exception in JUnit test (easy question)
    By opium in forum Exceptions
    Replies: 1
    Last Post: February 14th, 2012, 09:09 AM
  5. My introduction
    By Jeffadkins51 in forum Member Introductions
    Replies: 1
    Last Post: November 15th, 2011, 10:10 AM