-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);
         }
5
  • 1
    Java requires you to have parentheses around the whole expression, e.g. if (word.charAt(i) == ' ') { ... Commented Nov 8, 2020 at 4:49
  • I get this syntax error on the 3rd line "Cannot invoke add(int) on the primitive type int" Commented Nov 8, 2020 at 4:51
  • @MoR What is whitespaces_array defined as? Commented Nov 8, 2020 at 4:54
  • Possible duplicate of stackoverflow.com/questions/2082449/… Commented Nov 8, 2020 at 4:56
  • @Jaden ArrayList<Integer> whitespaces_array = new ArrayList<Integer>(); Commented Nov 8, 2020 at 5:15

3 Answers 3

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 );
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I done this and theres a syntax error on the final bracket. It says Syntax error, insert "}" to complete MethodBody
@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.
ok no problem im new to java, but i managed to fix this. Thanks
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 :)

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.