-1

How do I do the == operator here?

Also I need help, I have a list array here that I want to add the location of any whitespaces in my array

length is just a varaible that holds length of word

for (int i = 0; i<length; i++) {
        if (word.charAt(i)) == (' '){
            whitespaces_array.add(i);
         }
Mo R
  • 3
  • 2

3 Answers3

0
if (checked value == required value) {
   do stuff;
}

You don't need the parentheses around ' ', and you do need to move the second closing parenthesis from just after word.charAt(i) to after the required value you are checking against.

Once you have done that, please put a space in front of the curly bracket, just as you did in the top line of the for() loop. Java doesn't need it, but it looks an awful lot neater to most humans :)

sunrise
  • 404
  • 2
  • 9
0

As revealed in a comment, the actual problem is that you had not given enough information in your question.

In Java, Integers are objects, whereas ints are also integers, yet they are primitive data types instead. An int maps more or less directly to a simple set of bits that are manipulated via arithmetic operations. I will not go into depth here about the differences, but the key is that Integer != int.

The proper code would look more like the following:

ArrayList<Integer> whitespaces_array = new ArrayList<Integer>();

for ( int i = 0; i < length; ++i ) {
    if ( word.charAt( i ) == ' ' ) {
        whitespaces_array.add( i );
    }
}
  • I done this and theres a syntax error on the final bracket. It says Syntax error, insert "}" to complete MethodBody – Mo R Nov 08 '20 at 05:29
  • @MoR Well, that's beyond this question, and answer. Although, these are relatively basic questions, you should look at a modern, up to date tutorial/course in order to expect to learn Java. –  Nov 08 '20 at 05:35
  • ok no problem im new to java, but i managed to fix this. Thanks – Mo R Nov 08 '20 at 05:47
0

Just want to add something connected to this code that helped me

(ArrayList< Integer> list1 )

2nd line in this comment is the parameter you put in when referencing list Arrays in methods when they use integars, you wouldnt put int list1, as someone stated above int and Integar are different

Mo R
  • 3
  • 2