#How do I count how many 0's are in a scanner?

39 messages · Page 1 of 1 (latest)

wise kraken
#

Hi guys i am very confused about this question my teacher is asking, basically here is what they provide us a list of and we need to return how many int 0's there are, which is two.

hello
0
10
0.0
string0
0

lilac frostBOT
#

This post has been reserved for your question.

Hey @wise kraken! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

wise kraken
#

and then they give us this line

#

which is like a string right?

#

kinda?

#

so im thinking i need to use a delimiter or split/trim functions

#

but idk where to start this

cloud osprey
#

The entire point of Scanner is that it delimits them for you. Just call next()

wise kraken
cloud osprey
#

There are many approaches, but you could try hasNextInt() and hasNextDouble() before hasNext()

wise kraken
#

like int double string

cloud osprey
#

has-stuff checks whether the String that would be obtained with next() has a format that works for the mentioned stuff

#

If you want to read with the conversion, it's nextInt() or nextDouble() and so on

wise kraken
#

so nextInt but it also has to be 0

#

hm

cloud osprey
#

if hasNextInt() => if nextInt() == 0

#

For example. I'm not entirely sure what counts as a zero in the assignmet

wise kraken
#

just the int 0

#

not 0.0 or string0 or whatever

#

so while scan.hasnext

#

and then if scan.hasnextint?

cloud osprey
#

Then just read the String without caring whether it's an int, double, word or whatever

wise kraken
#

ok maybe my code on top doesnt make sense and im just experimenting

#

but with this print statement

#

how do i get it to recognize that scan =2?

wise kraken
cloud osprey
#

No... it's just you shouldn't call hasNextInt() because you don't care about ints

#

You care about when something has nothing but just one zero

wise kraken
#

does this look at all right?

#

but my counter still says 0

#

o

#

wrong ==

#

.equals since its a string