fizzString2 challenge

Hi,

I am working on below challenge
http://codingbat.com/prob/p115243

I tried my code as below
public String fizzString2(int n) {

if(n%3==0)
return "Fizz";
else if(n%5==0)
return "Buzz";
else if((n%5==0)&&(n%3==0))
return "FizzBuzz";


  return valueOf(n)+"!";
  return null;
}

Open in new window


I am getting below result

Compile problems:


Error:      return valueOf(n)+"!";
             ^^^^^^^
The method valueOf(int) is undefined


see Example Code to help with compile problems
how to  improve my approach and design of this challenge. How do i make a

graphical venn or some other relevant diagram to design it before writing single

line of code to decide best strategy?
 Please advise
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gurpsbassiCommented:
Where is your valueOf() method?
0
ozoCommented:
return ((n%3==0?"Fizz":"")+(n%5==0?"Buzz":"")).replaceAll("^$",n+"")+"!";
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
awking00Commented:
return valueOf(n) + "!" needs to be return String.valueOf(n) + "!";
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

gudii9Author Commented:
public String fizzString2(int n) {

if(n%3==0)
return "Fizz";
else if(n%5==0)
return "Buzz";
else if((n%5==0)&&(n%3==0))
return "FizzBuzz";


  return String.valueOf(n)+"!";
  
}

Open in new window

i fixed still failing few.
Expected	Run		
fizzString2(1) → "1!"	"1!"	OK	    
fizzString2(2) → "2!"	"2!"	OK	    
fizzString2(3) → "Fizz!"	"Fizz"	X	    
fizzString2(4) → "4!"	"4!"	OK	    
fizzString2(5) → "Buzz!"	"Buzz"	X	    
fizzString2(6) → "Fizz!"	"Fizz"	X	    
fizzString2(7) → "7!"	"7!"	OK	    
fizzString2(8) → "8!"	"8!"	OK	    
fizzString2(9) → "Fizz!"	"Fizz"	X	    
fizzString2(15) → "FizzBuzz!"	"Fizz"	X	    
fizzString2(16) → "16!"	"16!"	OK	    
fizzString2(18) → "Fizz!"	"Fizz"	X	    
fizzString2(19) → "19!"	"19!"	OK	    
fizzString2(21) → "Fizz!"	"Fizz"	X	    
fizzString2(44) → "44!"	"44!"	OK	    
fizzString2(45) → "FizzBuzz!"	"Fizz"	X	    
fizzString2(100) → "Buzz!"	"Buzz"	X	    
other tests

Open in new window

please advise
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Other than the fact that you just need to add a ! to the end of the three strings (lines 4, 6, 8) which will fix a lot of the failures, the last issue that you have is that you aren't returning FizzBuzz! when required.

Can you think through the logic and work out why line 8 is never getting executed?
0
gudii9Author Commented:
public String fizzString2(int n) {


if((n%5==0)&&(n%3==0))
return "FizzBuzz"+"!";
else if(n%3==0)
return "Fizz"+"!";
else if(n%5==0)
return "Buzz"+"!";
else 
return String.valueOf(n)+"!";
  //return null;
}

Open in new window


now i am passing all tests.

Expected	Run		
fizzString2(1) → "1!"	"1!"	OK	    
fizzString2(2) → "2!"	"2!"	OK	    
fizzString2(3) → "Fizz!"	"Fizz!"	OK	    
fizzString2(4) → "4!"	"4!"	OK	    
fizzString2(5) → "Buzz!"	"Buzz!"	OK	    
fizzString2(6) → "Fizz!"	"Fizz!"	OK	    
fizzString2(7) → "7!"	"7!"	OK	    
fizzString2(8) → "8!"	"8!"	OK	    
fizzString2(9) → "Fizz!"	"Fizz!"	OK	    
fizzString2(15) → "FizzBuzz!"	"FizzBuzz!"	OK	    
fizzString2(16) → "16!"	"16!"	OK	    
fizzString2(18) → "Fizz!"	"Fizz!"	OK	    
fizzString2(19) → "19!"	"19!"	OK	    
fizzString2(21) → "Fizz!"	"Fizz!"	OK	    
fizzString2(44) → "44!"	"44!"	OK	    
fizzString2(45) → "FizzBuzz!"	"FizzBuzz!"	OK	    
fizzString2(100) → "Buzz!"	"Buzz!"	OK	    
other tests
OK	    

Open in new window


how to improve my code? please advise
0
gudii9Author Commented:
return ((n%3==0?"Fizz":"")+(n%5==0?"Buzz":"")).replaceAll("^$",n+"")+"!";

Open in new window

can yo please advise on above approach.

if(n%3==0){
                  return "Fizz"
                   }
else {
           return "";
}

+

if(n%3==0){
                  return "Buzz"
                   }
else {
           return "";
}



                      on whole above thing why are we doing

.replaceAll("^$",n+"")+"!";

Open in new window

0
ozoCommented:
The result of (n%3==0?"Fizz":"") is not being returned from the function, the resulting string is being added (concatenated) with another string, so you can't change it into separate return statements in the same way.
If you must transform it into if( ){  }else{  }
(which I don't recommend in general, but which I thought might be useful in some of your questions as a way of translating the notation into terms you seemed to be more familiar with)
then you might use something like:

public String fizz(int n){
   // n%3==0?"Fizz":""
   if( n%3==0 ){
      return "Fizz";
   }else{
       return "";
   }
}
public String buzz(int n){
   // n%5==0?"Buzz":""
   if( n%5==0 ){
      return "Buzz";
   }else{
       return "";
   }
}
public String fizzString2(int n) {
  return (fizz(n)+buzz(n)).replaceAll("^$",n+"")+"!";
}


why are we doing
.replaceAll("^$",n+"")+"!";
Try seeing what it would return without it.
0
gudii9Author Commented:
so replacing those 2 special characters ^ and $ with "" and !..
i will try
0
gudii9Author Commented:
return ((n%3==0?"Fizz":"")+(n%5==0?"Buzz":"")).replaceAll("^",n+"")+"!";

return ((n%3==0?"Fizz":"")+(n%5==0?"Buzz":"")).replaceAll("",n+"")+"!";

when i tried as above both above approaches failing fizz , buzz and both cases.

what is the literal meaning of replaceAll("^",n+"")+"!";

i have not understood this approach?
n%3==0?"Fizz":"" if n is divisible by 3 we are saying return Fizz otherwise return ""
n%5==0?"Buzz":"" if n is divisible by 5 we are saying return Buzz otherwise return ""
why we are returning "" if not divisible and why we are doing .replaceAll("",n+"")+"!"; on the whole thing?
please advise
0
ozoCommented:
replaceAll("^$",n+"")
0
gudii9Author Commented:
what is  meaning of ^$ and then n and then concatenating with ""??
0
ozoCommented:
match beginning of string immediately followed by end of string
https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
0
gudii9Author Commented:
Character classes
[abc]      a, b, or c (simple class)
[^abc]      Any character except a, b, or c (negation)

any character except $ we are taking here?

n and then concatenating with ""means what? please advise
0
gudii9Author Commented:
why we only chose $ out of all other special characters to exempt?
0
ozoCommented:
http:#a41278029 does not have [] for Character classes
it uses Boundary matchers
0
ozoCommented:
The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings.
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
0
gudii9Author Commented:
somehow i am not in same page.

return ((n%3==0?"Fizz":"")+(n%5==0?"Buzz":"")).replaceAll("^$",n+"")+"!";

can you please explain above solution completely each word to word so that i can follow 100%
0
ozoCommented:
You can try each piece separately to test what it does.
Or you can remove it from the solution to see why it is necessary.
You can also think about what it would need to do to get to the solution.
0
gudii9Author Commented:
return ((n%3==0?"Fizz":"")+(n%5==0?"Buzz":"")).replaceAll("^",n+"")+"!";

return ((n%3==0?"Fizz":"")+(n%5==0?"Buzz":"")).replaceAll("",n+"")+"!";

when i tried as above both above approaches failing fizz , buzz and both cases.

i tried by removing $ and i also tried by removing ^ both cases it is failing
0
ozoCommented:
So you see why it is necessary to restrict the match to just before the end of the string.
0
gudii9Author Commented:
restrict the match to just before the end of the string.

i did not get the question? please elaborate on this?
0
ozoCommented:
Without the $, replaceAll matches where it shouldn't
0
gudii9Author Commented:
can you please break down to simple baby steps to understand this?
0
ozoCommented:
"^$" only matches when the end of string comes right after the beginning of string, which only happens when the string is empty, which only happens when (n%3==0?"Fizz":"") evaluates to "" and (n%5==0?"Buzz":"") evaluates to ""
0
gudii9Author Commented:
"^$" only matches when the end of string comes right after the beginning of string,

any sample java example on using ^$.

Is it due to below
http://www.vogella.com/tutorials/JavaRegularExpressions/article.html

^regex      Finds regex that must match at the beginning of the line.
regex$      Finds regex that must match at the end of the line.


can we replace $ with some other character here say ! or &

please advise
0
ozoCommented:
return ((n%3==0?"Fizz":"")+(n%5==0?"Buzz":"")+"!").replaceAll("^!",n+"!");
0
gudii9Author Commented:
return ((n%3==0?"Fizz":"")+(n%5==0?"Buzz":"")+"!").replaceAll("^&",n+"!");

why it is failing with ^& where as it worked fine with ^! and ^$

please advise
0
ozoCommented:
Because you requested "can we replace $ with some other character here say !", so I added +"!" here
return ((n%3==0?"Fizz":"")+(n%5==0?"Buzz":"")+"!").replaceAll("^!",n+"!");
0
gudii9Author Commented:
Because you requested "can we replace $ with some other character here say !"




can we replace $ with some other character here say &"//note not !

i am trying to get bottom line understanding of this regular expression

"^$" only matches when the end of string comes right after the beginning of string,
0
ozoCommented:
"".replaceAll("^$",) matches
"Fizz".replaceAll("^$",) does not match
"Buzz".replaceAll("^$",) does not match
"FizzBuzz".replaceAll("^$",) does not match
"!".replaceAll("^$",) does not match
"&".replaceAll("^$",) does not match

"".replaceAll("^!",) does not match
"!".replaceAll("^!",) matches
"&".replaceAll("^!") does not match

"".replaceAll("^&",) does not match
"!".replaceAll("^&",) does not match
"&".replaceAll("^&",)  matches
"Fizz&".replaceAll("^&",)  does not match
"&Fizz".replaceAll("^&",)  matches

"&".replaceAll("&$",)  matches
"Fizz&".replaceAll("&$",) matchs
"&Fizz".replaceAll("&$",)  does not match

"&".replaceAll("^&$",)  matches
"Fizz&".replaceAll("^&$",) does not match
"&Fizz".replaceAll("^&$",)  does not match
0
gudii9Author Commented:
"".replaceAll("^$",) matches
"Fizz".replaceAll("^$",) does not match

what it means first match and second one does not match?
what is the target string and what is the string we are trying to match in above two sentences?
0
gudii9Author Commented:
[^character_group]
Negation: Matches any single character that is not in character_group. By default, characters in character_group are case-sensitive.

https://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx

Matches any single character that is not in character_group?
0
gudii9Author Commented:
how & and ! or $ makes difference as we are seeing whether number is divisible by 3 0r 5 or both?

i am bit not clear on this?
0
ozoCommented:
We were not using [ ] and have no [character groups] in the expressions we were talking about
0
ozoCommented:
When n is divisible by 3, (n%3==0?"Fizz":"") evaluates to "Fizz"
When n is divisible by 5, (n%5==0?"Buzz":"") evaluates to "Buzz"
When n is divisible by both, (n%3==0?"Fizz":"")+(n%5==0?"Buzz":"") evaluates to "FizzBuzz"
0
gudii9Author Commented:
^$" only matches when the end of string comes right after the beginning of string,

can you please elaborate on this one. I was not clear on this?

why ^$" only matches when the end of string comes right after the beginning of string,
0
gudii9Author Commented:
end of string comes right after the beginning of string,

what is meaning of end of string comes right after the beginning of string
0
ozoCommented:
When there is nothing between the beginning of the string and the end of the string, it means the string is the empty string.
0
gudii9Author Commented:

public class FizzChallenge {

      /**
       * @param args
       */
      public static void main(String[] args) {
            // TODO Auto-generated method stub
             System.out.println("values is--->"+fizzString2(3));

      }
      
      public static String fizzString2(int n) {
      //return ((n%3==0?"Fizz":"")+(n%5==0?"Buzz":"")+"!").replaceAll("^&",n+"!");
            return ((n%3==0?"Fizz":"")+(n%5==0?"Buzz":"")+"!").replaceAll("^!",n+"!");
      }
}

gave below output
values is--->Fizz!



public class FizzChallenge {

      /**
       * @param args
       */
      public static void main(String[] args) {
            // TODO Auto-generated method stub
             System.out.println("values is--->"+fizzString2(1));

      }
      
      public static String fizzString2(int n) {
      //return ((n%3==0?"Fizz":"")+(n%5==0?"Buzz":"")+"!").replaceAll("^&",n+"!");
            return ((n%3==0?"Fizz":"")+(n%5==0?"Buzz":"")+"!").replaceAll("^!",n+"!");
      }
}

above gave below output

values is--->1!

it means the string is the empty string
how to pass empty string here to test as the method expecting int?

please advise why we are worrying about String as the argument to the method is int
please advise
0
ozoCommented:
The argument to the method is int, but the  the return value of the method is String
and the argument to .replaceAll is String

(n%3==0?"Fizz":"")+(n%5==0?"Buzz":"")
will be "" when n==1
But the +"!" will ensure that .replaceAll("^!", never sees an empty string,
Which it is not looking for anyway, it is looking for a string that starts with !
0
gudii9Author Commented:
But the +"!" will ensure that
you mean below boldly highlighted one right not the italisized one?

return ((n%3==0?"Fizz":"")+(n%5==0?"Buzz":"")+"!").replaceAll("^!",n+"!");
0
gudii9Author Commented:
replaceAll("^!",n+"!")

String str=(n%3==0?"Fizz":"")+(n%5==0?"Buzz":"")+"!";
above means if you find anywhere ! in str then replace ! ("^!" as mentioned in argument) with n+"!"(n+"!" as mentioned in argument)

is my understanding is correct??
0
ozoCommented:
str.replaceAll("!",n+"!") means if you find  ! anywhere in str, replace it with n+"!"
str.replaceAll("^!",n+"!") means if you find ! at the beginning of str, replace it with n+"!"
0
gudii9Author Commented:
public class FizzChallenge {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		 System.out.println("values is--->"+fizzString2(1)); 

	}
	
	public static String fizzString2(int n) {
	//return ((n%3==0?"Fizz":"")+(n%5==0?"Buzz":"")+"!").replaceAll("^&",n+"!");
		String str=((n%3==0?"Fizz":"")+(n%5==0?"Buzz":"")+"!");
		System.out.println("value of str is---->>>"+str);
		return (str.replaceAll("^!",n+"!"));
	}
}

Open in new window

above gave below output
value of str is---->>>!
values is--->1!


why basically replacing ! with n+"!"
0
gudii9Author Commented:

public class FizzChallenge {

      /**
       * @param args
       */
      public static void main(String[] args) {
            // TODO Auto-generated method stub
             System.out.println("values is--->"+fizzString2(3));

      }
      
      public static String fizzString2(int n) {
      //return ((n%3==0?"Fizz":"")+(n%5==0?"Buzz":"")+"!").replaceAll("^&",n+"!");
            String str=((n%3==0?"Fizz":"")+(n%5==0?"Buzz":"")+"!");
            System.out.println("value of str is---->>>"+str);
            return (str.replaceAll("^!",n+"!"));
      }
}

above gave below output

value of str is---->>>Fizz!
values is--->Fizz!


i wonder what is eclipse short cut to create and assign to String str=((n%3==0?"Fizz":"")+(n%5==0?"Buzz":"")+"!"); as below by highlighing the code ((n%3==0?"Fizz":"")+(n%5==0?"Buzz":"")+"!");
0
gudii9Author Commented:
str.replaceAll("!",n+"!") means if you find  ! anywhere in str, replace it with n+"!"
str.replaceAll("^!",n+"!") means if you find ! at the beginning of str, replace it with n+"!"

can you point me to link or resource or pdf which has all these tricks on how to find at beginning(like ^!) or wholse string(like "!") at end(not sure what ?? for last) and also group characters[] and when we use group charactes like [abc]??
0
gudii9Author Commented:
return ((n%3==0?"Fizz":"")+(n%5==0?"Buzz":"")+"!").replaceAll("^!",n+"!");

how above solution worked for 15( both multiple of 3 and 5)
fizzString2(15) → "FizzBuzz!"      "FizzBuzz!"      

public class FizzChallenge {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		 System.out.println("values is--->"+fizzString2(15)); 
		// fizzString2(15) → "FizzBuzz!"	"FizzBuzz!"	
	}
	
	public static String fizzString2(int n) {
	//return ((n%3==0?"Fizz":"")+(n%5==0?"Buzz":"")+"!").replaceAll("^&",n+"!");
		String str=((n%3==0?"Fizz":"")+(n%5==0?"Buzz":"")+"!");
		System.out.println("value of str is---->>>"+str);
		return (str.replaceAll("^!",n+"!"));
	}
}

Open in new window


value of str is---->>>FizzBuzz!
values is--->FizzBuzz!
0
gudii9Author Commented:
so important conclusions from this challenge

1:no matter what number we add ! ie +"!");
2: if multple of 3 do (n%3==0?"Fizz":"")
3: if multple of 5 do (n%3==0?"Buzz":"")
4:  if multple of 3 and 5  do (n%3==0?"Fizz":"")+(n%5==0?"Buzz":"")
5: if other numbers like 1(2, 4 etc neither multiple of 3 nor 5 nor both) we get str as """"!   which is same as ! (please clarify here?? empty+empty+! result ! or ""+""+!..i thought it should be spaceSpace! like """"!)
as str since
String str=((n%3==0?"Fizz":"")+(n%5==0?"Buzz":"")+"!");
6: now if beginning ! then replace n+"!" so we end up getting 1! or 2! etc for neither multiple of 3 nor 5 nor both

is my understanding is correct??
0
ozoCommented:
(15%3==0?"Fizz":"") is "Fizz"
(15%5==0?"Buzz":"") is "Buzz"
(15%3==0?"Fizz":"")+(15%5==0?"Buzz":"") is "FizzBuzz"
(15%3==0?"Fizz":"")+(15%5==0?"Buzz":"")+"!" is "FizzBuzz!"
 "FizzBuzz!".replaceAll("^!", does not match because the beginning if the string is not next to the !
so no replacement is done
0
gudii9Author Commented:
for Java
https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
for regular expressions
http://regex.info/book.html

being a java developer if i get familarized with top link that is enough or bottom link also i need to familiarize. what is difference between above two. does java implements regular expressions different way than original regular expressions how they written?
0
gudii9Author Commented:
public String fizzString2(int n) {

if(n%3==0)
return "Fizz"+"!";
else if(n%5==0)
return "Buzz"+"!";
else if((n%5==0)&&(n%3==0))
return "FizzBuzz"+"!";

  
  else 
return String.valueOf(n)+"!";
  
}

Open in new window


why when i change order i am failing for 15, 45 etc as below
Expected	Run		
fizzString2(1) → "1!"	"1!"	OK	    
fizzString2(2) → "2!"	"2!"	OK	    
fizzString2(3) → "Fizz!"	"Fizz!"	OK	    
fizzString2(4) → "4!"	"4!"	OK	    
fizzString2(5) → "Buzz!"	"Buzz!"	OK	    
fizzString2(6) → "Fizz!"	"Fizz!"	OK	    
fizzString2(7) → "7!"	"7!"	OK	    
fizzString2(8) → "8!"	"8!"	OK	    
fizzString2(9) → "Fizz!"	"Fizz!"	OK	    
fizzString2(15) → "FizzBuzz!"	"Fizz!"	X	    
fizzString2(16) → "16!"	"16!"	OK	    
fizzString2(18) → "Fizz!"	"Fizz!"	OK	    
fizzString2(19) → "19!"	"19!"	OK	    
fizzString2(21) → "Fizz!"	"Fizz!"	OK	    
fizzString2(44) → "44!"	"44!"	OK	    
fizzString2(45) → "FizzBuzz!"	"Fizz!"	X	    
fizzString2(100) → "Buzz!"	"Buzz!"	OK	    
other tests
X	    

Open in new window


whereas below passes all tests in which both 3 and 5 check is done at the top instead of at bottom(like earlier approach)

public String fizzString2(int n) {


if((n%5==0)&&(n%3==0))
return "FizzBuzz"+"!";
else if(n%3==0)
return "Fizz"+"!";
else if(n%5==0)
return "Buzz"+"!";
else 
return String.valueOf(n)+"!";
  //return null;
}

Open in new window

0
gudii9Author Commented:
i think i got it for 15, 45 etc below if statement is eating it as condition is satisfied right there

if(n%3==0)
return "Fizz"+"!";
0
ozoCommented:
The Java documentation on regular expression pattern matching may assume a general familiarity with regular expression concepts, just as the Java documentation on arithmetic operators assumes a general familiarity with concepts of arithmetic.
To develop facility in working with Java arithmetic operators, it may be helpful to study algebra in addition to the Java documentation.
To develop facility in working working with Java regular expressions, it may be helpful to study references like Jeffrey Friedl's book in addition to the Java documentation.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.