gudii9
asked on
countXY challenge
Hi,
i am working on below challenge
http://codingbat.com/prob/p199171
i wrote my code as below
i am getting below errors.
Compile problems:
Error: if(Character.isLetter(str2 )==y||Char acter.isLe tter(str2) ==z)
^^^^
str2 cannot be resolved
see Example Code to help with compile problems
please advise
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;
}
i am getting below errors.
Compile problems:
Error: if(Character.isLetter(str2
^^^^
str2 cannot be resolved
see Example Code to help with compile problems
please advise
You called your variable Str2, but you're trying to use str2. Variable names are case-sensitive.
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;
}
i fixed. now getting below errors
Compile problems:
Error: if(Character.isLetter(str2)==y||Char acter.isLe tter(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.
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?
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;
}
}
above gives below error
Exception in thread "main" java.lang.StringIndexOutOf
at java.lang.String.getChars(
at CountYZ.countYZ(CountYZ.ja
at CountYZ.main(CountYZ.java:
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.
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
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;
}
}
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.ja
at CountYZ.main(CountYZ.java:
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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;
}
}
above gave below wrong result
count is-->6(instead of 2)
please advise
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;
}
}
\\i think i got right nowfez
day
count is-->2
how to improve my code and approach. please advise
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;
}
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
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;
}
}
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.
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.
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;
}
i tried as above failing someExpected 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().
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;
}
}
above gives below error. please advise
value str2 is--->[Ljava.lang.String;@
value str2 is--->[Ljava.lang.String;@
value str2 is--->[Ljava.lang.String;@
Exception in thread "main" java.lang.ArrayIndexOutOfB
at CountYZ.countYZ(CountYZ.ja
at CountYZ.main(CountYZ.java:
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.
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
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
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;
}
abve fails one 1 test. pleas advise
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
Looks like you're not counting the "z" at the end of "y2bz".
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.
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;
}
above works fine when intialized to 0 not 1
ASKER
The number 2 is not a letter, so it's supposed to count as a word boundary.
make sense