Home > Enterprise >  Trying to get the Value of a string turned into an array
Trying to get the Value of a string turned into an array

Time:05-16

i am currently trying to get the value of only. not sure why it isn't letting me use indexOf on wordArray.

 String bestThing = "The best thing about a boolean is even if you 
  are wrong you are only off by a bit";

 Write code that finds and returns the index of "only" in 
 "bestThing" once turned into an array

    // Display your result using System.out.println(index);
   // String bestThing = "The best thing about a boolean is even if 
    //you are wrong you are 
    //only off by a bit";

     String[] wordArray = bestThing.split(" ");
     int index = 0;
     for( int i = 0 ; i < wordArray.length; i  ){
         if (wordArray[i] =="only") {
             index = i;

         }

         }
             System.out.println(index);

CodePudding user response:

              String[] wordArray = bestThing.split(" ");
                 int index = 0;
                 for( int i = 0 ; i < wordArray.length; i  ){
                     System.out.println(wordArray[i]);
                     if (wordArray[i].equalsIgnoreCase("only")) {
                         index = i;

                     }

                     }
                         System.out.println(index);

The output for "only" gave me 14 using ".equalsIgnoreCase"

CodePudding user response:

In java you need to use String.equals so the solution would be:

 for( int i = 0 ; i < wordArray.length; i  ){
     if (wordArray[i].equals("only")) {
         index = i;

     }

     }
         System.out.println(index);
  

CodePudding user response:

You were very close. The problem is in this line.

if (wordArray[i] == "only")

It should be this instead.

if (wordArray[i].equals("only"))


In Java, there is VERY big difference between the operator == and the method .equals().

  • == -- this checks to see if 2 references are literally pointing to the exact same object. We don't care about object value, we care about if they are literally referencing the same object

  • .equals() -- this checks that 2 references are pointing to objects of equal value. We don't care if both references are pointing to the same object or not, we care about if the 2 references point to objects that have an equal value

Here is an analogy that might help.

Imagine that I have a bottle of water, and you have a bottle of water. Both bottles of water are unopened and they look exactly the same. There is no way to tell them apart.

WaterBottle a = my water bottle
WaterBottle b = your water bottle
  • Does a.equals(b)?

YES, there is no measurable difference between these 2 waters. If we spun them around quickly, there is no way for us to know whose water bottle is whose, since they are exactly the same. Because of that, .equals() returns true

  • Does a == b?

NO, my water bottle is mine, and your water is yours. Just because these 2 water bottles have the same value, does not mean that == will return true. == doesn't care about the value of the water, it cares about if a literally is b. My water bottle is very similar to yours, but my water bottle is not your water bottle. Mine is mine, and yours is yours - they are 2 separate entities that happen to look and taste exactly the same at this time.


To be a little more specific, in Java, the == operator checks to see if the references have the exact same value. What this means is this - a is a reference, and if you were to physically open up your computer and look at the literal value of a, it would be something like 0xaf9121ed. That ugly series of values is an address in your computer's memory. So, if both a and b have a reference of 0xaf9121ed, then they are ==.

.equals() is different - it is a method that exists in every Object in Java, and is actually a method that you can define/override yourself.

For example, in our example of water bottles, let's say we wrote a .equals() method like this.

If both bottles have the same color cap, then they are equals

Doing it like this, we could have a water bottle as tall as a house, and it would be equal to a normal size water bottle as long as they have the same color cap. .equals() is entirely defined by the objects method equals(), which is something that you can override yourself.


In your example, you don't care if the 2 String objects (the one that you found, and the "only" that you typed out in your if statement) are literally the same object, you are asking if the String you found in the for loop has the same value as "only". If they have the same value, then that means that your String is the one you are looking for, and then you can do whatever else you want to do (print the index of the word only in your original sentence).

  • Related