Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

countXY challenge

Hi,

i am working on below challenge
http://codingbat.com/prob/p199171

i wrote my code as below
public int countYZ(String str) {
  int i=0;
  // public static void main(String args[]){
      String Str1 = str;
      char[] Str2 = new char[10];

     // try{
         Str1.getChars(0, 10, Str2, 0);
      //   System.out.print("Copied Value = " );
        // System.out.println(Str2 );
         
  
  for(int j=0;j<Str2.length;j++){
  if(Character.isLetter(str2)==y||Character.isLetter(str2)==z)
  i++;
  }
  return i;
}

Open in new window



i am getting below errors.

Compile problems:


Error:      if(Character.isLetter(str2)==y||Character.isLetter(str2)==z)
                            ^^^^
str2 cannot be resolved


see Example Code to help with compile problems

please advise
Avatar of CPColin
CPColin
Flag of United States of America image

You called your variable Str2, but you're trying to use str2. Variable names are case-sensitive.
Avatar of gudii9

ASKER

public int countYZ(String str) {
  int i=0;
  // public static void main(String args[]){
      String str1 = str;
      char[] str2 = new char[10];

     // try{
         str1.getChars(0, 10, str2, 0);
      //   System.out.print("Copied Value = " );
        // System.out.println(Str2 );
         
  
  for(int j=0;j<str2.length;j++){
  if(Character.isLetter(str2)==y||Character.isLetter(str2)==z)
  i++;
  }
  return i;
}

Open in new window


i fixed. now getting below errors

Compile problems:


Error:      if(Character.isLetter(str2)==y||Character.isLetter(str2)==z)
                   ^^^^^^^^
The method isLetter(char) in the type Character is not applicable for the arguments (char[])
You can pass only one char at a time to that method. It also returns a boolean, which probably isn't what you're looking for.
Avatar of gudii9

ASKER

You can pass only one char at a time to that method. It also returns a boolean, which probably isn't what you're looking for.

you mean isLetter() method?
Avatar of gudii9

ASKER

public class CountYZ {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("i is-->" + countYZ("fez day"));

	}

	public static int countYZ(String str) {
		int i = 0;
		// public static void main(String args[]){
		String Str1 = str;
		char[] Str2 = new char[20];

		// try{
		// Str2= Str1.getChars(0, 10, Str2, 0);
		Str1.getChars(0, 10, Str2, 0);
		// System.out.print("Copied Value = " );
		// System.out.println(Str2 );

		for (int j = 0; j < Str2.length; j++) {
			if (Character.isLetter('y') || Character.isLetter('z'))
				i++;
		}
		return i;

	}
}

Open in new window


above gives below error

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 10
      at java.lang.String.getChars(Unknown Source)
      at CountYZ.countYZ(CountYZ.java:18)
      at CountYZ.main(CountYZ.java:6)


please advise.
You don't need to convert the String object into a char[] array; String has the charAt() method, which will do what you're trying to get Character.isLetter() to do.
Avatar of gudii9

ASKER


Given a string, count the number of words ending in 'y' or 'z' -- so the 'y' in "heavy" and the 'z' in "fez" count, but not the 'y' in "yellow" (not case sensitive). We'll say that a y or z is at the end of a word if there is not an alphabetic letter immediately following it. (Note: Character.isLetter(char) tests if a char is an alphabetic letter.)

countYZ("fez day") → 2
countYZ("day fez") → 2
countYZ("day fyyyz") → 2

challenge hinted that approach. let me try charAt() on string approach
Avatar of gudii9

ASKER

public class CountYZ {
	  //static int=0;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		/*
		 * Given a string, count the number of words ending in 'y' or 'z' -- so
		 * the 'y' in "heavy" and the 'z' in "fez" count, but not the 'y' in
		 * "yellow" (not case sensitive). We'll say that a y or z is at the end
		 * of a word if there is not an alphabetic letter immediately following
		 * it. (Note: Character.isLetter(char) tests if a char is an alphabetic
		 * letter.) * countYZ("fez day") → 2 countYZ("day fez") → 2 countYZ(
		 * "day fyyyz") → 2
		 */
		System.out.println("count is-->" + countYZ("fez day"));
	}

	public static int countYZ(String str) {
       
		for (int j = 1; j < str.length(); j++) {
			if (str.charAt(j)=='y' || str.charAt(j)=='z')
				j++;
		}
		return j;

	}
}

Open in new window


i tried as above getting below error
 
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
      j cannot be resolved to a variable

      at CountYZ.countYZ(CountYZ.java:24)
      at CountYZ.main(CountYZ.java:15)
ASKER CERTIFIED SOLUTION
Avatar of CPColin
CPColin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

public class CountYZ {
	  //static int=0;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		/*
		 * Given a string, count the number of words ending in 'y' or 'z' -- so
		 * the 'y' in "heavy" and the 'z' in "fez" count, but not the 'y' in
		 * "yellow" (not case sensitive). We'll say that a y or z is at the end
		 * of a word if there is not an alphabetic letter immediately following
		 * it. (Note: Character.isLetter(char) tests if a char is an alphabetic
		 * letter.) * countYZ("fez day") → 2 countYZ("day fez") → 2 countYZ(
		 * "day fyyyz") → 2
		 */
		System.out.println("count is-->" + countYZ("fez day"));
	}

	public static int countYZ(String str) {
       int k = 0;
		for (int j = 1; j < str.length(); j++) {
			if (str.charAt(j)=='y' || str.charAt(j)=='z')
				k=j++;
		}
		
		return k;

	}
}

Open in new window


above gave below wrong result

count is-->6(instead of 2)


please advise
Avatar of gudii9

ASKER

public class CountYZ {
	  //static int=0;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		/*
		 * Given a string, count the number of words ending in 'y' or 'z' -- so
		 * the 'y' in "heavy" and the 'z' in "fez" count, but not the 'y' in
		 * "yellow" (not case sensitive). We'll say that a y or z is at the end
		 * of a word if there is not an alphabetic letter immediately following
		 * it. (Note: Character.isLetter(char) tests if a char is an alphabetic
		 * letter.) * countYZ("fez day") → 2 countYZ("day fez") → 2 countYZ(
		 * "day fyyyz") → 2
		 */
		System.out.println("count is-->" + countYZ("fez day"));
	}

	public static int countYZ(String str) {
       int k = 0;
   	   String strArr[] = str.split("\\s");
    
	for (String str2 : strArr){
		System.out.println(str2);

		for (int j = 1; j < str2.length(); j++) {
			if (str.charAt(j)=='y' || str.charAt(j)=='z')
				k=j++;
		}
		
		
	}
	return k;
	}
}

Open in new window

\\i think i got right now

fez
day
count is-->2



how to improve my code and approach. please advise
Avatar of gudii9

ASKER

public int countYZ(String str) {
  int k = 0;
   	   String strArr[] = str.split("\\s");
    
	for (String str2 : strArr){
	//	System.out.println(str2);

		for (int j = 1; j < str2.length(); j++) {
			if (str.charAt(j)=='y' || str.charAt(j)=='z')
				k=j++;
		}
		
		
	}
	return k;
	}

Open in new window


above fails below

Expected      Run            
countYZ("fez day") → 2      2      OK      
countYZ("day fez") → 2      2      OK      
countYZ("day fyyyz") → 2      2      OK      
countYZ("day yak") → 1      2      X      
countYZ("day:yak") → 1      4      X      
countYZ("!!day--yaz!!") → 2      9      X      
countYZ("yak zak") → 0      0      OK      
countYZ("DAY abc XYZ") → 2      0      X      
countYZ("aaz yyz my") → 3      2      X      
countYZ("y2bz") → 2      3      X      
countYZ("zxyx") → 0      2      X      
other tests
X
\

please advise
Avatar of gudii9

ASKER

public class CountYZ {
	  //static int=0;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		/*
		 * Given a string, count the number of words ending in 'y' or 'z' -- so
		 * the 'y' in "heavy" and the 'z' in "fez" count, but not the 'y' in
		 * "yellow" (not case sensitive). We'll say that a y or z is at the end
		 * of a word if there is not an alphabetic letter immediately following
		 * it. (Note: Character.isLetter(char) tests if a char is an alphabetic
		 * letter.) * countYZ("fez day") → 2 countYZ("day fez") → 2 countYZ(
		 * "day fyyyz") → 2
		 */
		System.out.println("count is-->" + countYZ("fez day"));
	}

	public static int countYZ(String str) {
       int k = 0;
   	   String strArr[] = str.split("\\s");
    
	for (String str2 : strArr){
		System.out.println(str2);
int m=0;
		//for (int j = 1; j < str2.length(); j++) {
		int j=str2.length();
		System.out.println("j is"+j);
			if (str.charAt(j-1)=='y' || str.charAt(j-1)=='z')
				k=m++;
		//}
		
		
	}
	return k;
	}
}

Open in new window


trying above failing
fez
j is3
day
j is3
count is-->0
You're just going to have to figure the rest out for yourself. Sorry.
Avatar of gudii9

ASKER

We'll say that a y or z is at the end of a word if there is not an alphabetic letter immediately following it

what is meaning of above in below challenge?
String-3 > countYZ
prev  |  next  |  chance
Given a string, count the number of words ending in 'y' or 'z' -- so the 'y' in "heavy" and the 'z' in "fez" count, but not the 'y' in "yellow" (not case sensitive). We'll say that a y or z is at the end of a word if there is not an alphabetic letter immediately following it. (Note: Character.isLetter(char) tests if a char is an alphabetic letter.)

countYZ("fez day") → 2
countYZ("day fez") → 2
countYZ("day fyyyz") → 2
Go...Save, Compile, Run
The instructions seem clear to me. If they aren't clear enough, please contact the author of that challenge for clarification.
Avatar of gudii9

ASKER

You don't need to convert the String object into a char[] array; String has the charAt() method, which will do what you're trying to get Character.isLetter() to do.
given string has more than 2 words separated by blank space etc.

without using array i wonder how to delimit at space and check last character is y or z?
my question is how to break as simple words to check each simple word to see ends with y or z

public int countYZ(String str) {
  int k = 0;
   	

		for (int j = 1; j <= str.length()-1; j++) {
			if (str.endsWith("y") || str.endsWith("z"))
				k++;
		}
		
		return k;
	}
	
	

Open in new window

i tried as above failing some
Expected      Run            
countYZ("fez day") → 2      6      X      
countYZ("day fez") → 2      6      X      
countYZ("day fyyyz") → 2      8      X      
countYZ("day yak") → 1      0      X      
countYZ("day:yak") → 1      0      X      
countYZ("!!day--yaz!!") → 2      0      X      
countYZ("yak zak") → 0      0      OK      
countYZ("DAY abc XYZ") → 2      0      X      
countYZ("aaz yyz my") → 3      9      X      
countYZ("y2bz") → 2      3      X      
countYZ("zxyx") → 0      0      OK      
other tests
X

please advise
Try String.split().
Avatar of gudii9

ASKER

public class CountYZ {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("i is-->" + countYZ("fez day"));

	}

	public static int countYZ(String str) {
		int i = 0;
		// public static void main(String args[]){
		//String Str1 = str;
		//char[] Str2 = new char[20];

		// try{
		// Str2= Str1.getChars(0, 10, Str2, 0);
		//Str1.getChars(0, 10, Str2, 0);
		// System.out.print("Copied Value = " );
		// System.out.println(Str2 );

		for (int j = 0; j <= str.length()-4; j++) {
			String[] str2=str.split(" ");
			System.out.println("value str2 is--->"+str2.toString());
			if(str2[j].endsWith("y")||str2[j].endsWith("z")){
				i++;
			}
		}
		return i;

	}
}

Open in new window


above gives below error. please advise
value str2 is--->[Ljava.lang.String;@15db9742
value str2 is--->[Ljava.lang.String;@6d06d69c
value str2 is--->[Ljava.lang.String;@7852e922
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
      at CountYZ.countYZ(CountYZ.java:24)
      at CountYZ.main(CountYZ.java:5)
You need to learn how to diagnose problems like this yourself. Ask yourself what the exception is, then look at that line in your code and try to find operations that could cause that exception. Once you've found an operation that might be causing the problem, try to figure out why it's happening.
Avatar of gudii9

ASKER

upon intospectiong myself i am able to solve simple if etc condtional check problems.

I am able to solve for , while etc loops also.

when the if and for(may be 2, 3) is together then i am bit confused
Avatar of gudii9

ASKER

public int countYZ(String str) {int k = 0;
		 // System.out.println("leng-->"+str.length());

		  str=str.toLowerCase()+" ";
				for (int j = 1; j < str.length()-1; j++) {
					if ( ( (str.charAt(j)=='y') ||  (str.charAt(j)=='z'))    && !Character.isLetter(str.charAt(j + 1) )){
						k++;
					}
				}
				
				return k;
			}
			
	
	

Open in new window


abve fails one 1 test. pleas advise
Avatar of gudii9

ASKER

Expected	Run		
countYZ("fez day") → 2	2	OK	
countYZ("day fez") → 2	2	OK	
countYZ("day fyyyz") → 2	2	OK	
countYZ("day yak") → 1	1	OK	
countYZ("day:yak") → 1	1	OK	
countYZ("!!day--yaz!!") → 2	2	OK	
countYZ("yak zak") → 0	0	OK	
countYZ("DAY abc XYZ") → 2	2	OK	
countYZ("aaz yyz my") → 3	3	OK	
countYZ("y2bz") → 2	1	X	
countYZ("zxyx") → 0	0	OK	
other tests
OK	
Correct for more than half the tests

Open in new window

Looks like you're not counting the "z" at the end of "y2bz".
Avatar of gudii9

ASKER

countYZ("y2bz") → 2      1      X

how this is one 2 i thought it is 1 as it is ending with z
The number 2 is not a letter, so it's supposed to count as a word boundary.
Avatar of gudii9

ASKER

public int countYZ(String str) {
  int k = 0;
		 // System.out.println("leng-->"+str.length());
		  str=str.toLowerCase()+" ";
				for (int j = 0; j < str.length()-1; j++) 
					if ( ( (str.charAt(j)=='y') ||  (str.charAt(j)=='z'))    && !Character.isLetter(str.charAt(j + 1) ))
						k++;
					
				
				
				return k;
				
				 
			}
			
	
	

Open in new window


above works fine when intialized to 0 not 1
Avatar of gudii9

ASKER

The number 2 is not a letter, so it's supposed to count as a word boundary.

make sense