Below is the code to print occurence of digit 4 among given numbers.Plze help me correct the code to get desired output | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Below is the code to print occurence of digit 4 among given numbers.Plze help me correct the code to get desired output

https://code.sololearn.com/crnavgboMwE5/?ref=app

13th Dec 2021, 2:39 PM
Tanu Jain
2 Answers
+ 1
Hello Tanu Jain if(s.charAt(i)=='4') Then it works. == 4 would be only true if the ascii value of this char is 4
13th Dec 2021, 4:14 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
Your code isn't working as we wanted, but why? 1. What's the variable t? Your doing a while statment using the variable t, but you say t is the first number of our input. We can use instead sc.hasNext(). hasNext() returns true if you have something to read on your input and false when you already read everything of your input. 2. You were almost there but when you need to compare char with char and not char with int, so when you say: s.charAt(i)==4, you are comparing to the fourth value of the ASCII table. Instead you need to ask: s.charAt(i)=='4' 3. Your print is inside of the while loop, you want to be out of the loop to only get one anwser, for that you need to have the variable count also out of the loop. You can see the program fixed here: https://code.sololearn.com/cXuI5X2ebtR2 I hope I helped.
13th Dec 2021, 4:28 PM
Tomás Ribeiro
Tomás Ribeiro - avatar