#what to change so it'll work

1 messages · Page 1 of 1 (latest)

bold hare
#

im trying to check how many "a" in the array and i cant equal the strings

bronze iglooBOT
#

<@&987246399047479336> please have a look, thanks.

bold hare
#

please

inland compass
#

try .equals

#

word is a String, cannot do == , as the error is saying

bold hare
#

not working

#

i tried before

flint umbra
#

charAt returns a char

#

you cant compare a char to a String like that

bold hare
#

oh

#

so how

flint umbra
#

String letter should probably be char letter

#

but if thats not possible, just use letter.charAt(0)

#

so ... == letter.charAt(0)

bold hare
#

but how

#

i need to check if theres a

nova trench
#

First of all Strings are arrays of char so you can reference to them without using charAt, just do myStr[index]
Then as dioxin said you can't compare chars to string, you have to compare chars with chars

char f = 'a'
char s = 'a'
if(f == s)
  // this will work
bold hare
#

its print nothing

inland compass
#

whats in the array btw ? words or letters ?

bold hare
#

it shoud print 4

#

words

inland compass
#

right , there a 2 approaches then , either count all the hits u need, or just replace the non matches and count the lenght

nova trench
bold hare
#

i mean return

nova trench
#

U call the function but you do nothing with the value that it returns

bold hare
#

im tryuing to return how many times a apperd

nova trench
#

Yes, we got it
U have your function, which returns an int, this int is equals to how many letters it finds in the words it gets in the array when it is called, but this value that the function returns needs to be stored somewhere and then you can print it

bold hare
#

so what should i do

nova trench
#

Functions can either return void or return something, when it returns void you will just call it, it will be executed and then it will automatically returns null. When it returns something, that something must be stored into a variable

public static int sum(int a, int b) {
  return a+b;
}

public static void main(String[] args) {
  int abSum = sum(3, 9);
}

When the function is called, it sums the numbers and they are stored in abSum variable

bronze iglooBOT
bold hare
#

but i return sum where i stored how many times a appare

inland compass
#

(those are example Tvrsier is showing )

flint umbra
#

not in java

#

in c, sure

#

in java, you have no access the to underlying array

#

gotta use charAt to access characters

worthy bison
#

Actually, Strings in java are not even related to arrays

flint umbra
#

although theres nothing forcing String to use an array

worthy bison
flint umbra
#

still an array

#

why are you nit picking

#

ArrayList also encapsulates everything inside. could say "ArrayList are not related to arrays" using that premise

worthy bison
# flint umbra why are you nit picking

You are nitpicking Thinkfused

My point is that, as a java fashion, String encapsulate whatever is inside, so don't assume everything and use it like a regular class

flint umbra
#

yes, but that wasnt the point. the point was they wanted to interface with String as one would in C

#

i was just saying its not possible, although you could use charAt

karmic fjord
#

A string is a Sequence of Char, so a CharSequence