Solved

# String Buffer - logic

Posted on 2011-10-10
228 Views
Can someone explain to me what this statement is really doing?

threeS is a String Buffer and lets say that
ThreeDName = SORT

if (threeS.indexOf(ThreeDName) == -1) {

}
0
Question by:jkteater

LVL 47

Expert Comment

it is checking if there is string "SORT" insiide thrreeS
0

LVL 47

Expert Comment

But here SORT should be eithert in quotes or it is another string variable name?
ThreeDName = SORT
0

LVL 47

Expert Comment

In general method s0.indexOf(String s) will determine want is the charcater niumber shere String s starts  within
string or stringbuffer s0
0

LVL 47

Expert Comment

if this method does not find any occurrences of s within s0 it will return -1
So if it returns something > -1 it means that there is occurence of string s within s0
0

LVL 47

Expert Comment

For exmple if your StringBuffere contains "mother and father"
then

StringBuffer sb = new StringBufferr("mother and father");
then sb.indexOf("mother") will return 0

but

sd.indexOf("sister") will return -1

0

LVL 47

Accepted Solution

so in your case if threeS does not contain SORT it will get inside the braces, otherwise it will skip it
0

Author Comment

the reason I am asking is because that IF statement is in a for loop and works as long as the ThreeDName is not the same, if ThreeDName is the same, it never goes in to the IF

Does that make since

Basically my For loop is getting a array of names, each name as a option and a value, so it would be something like

ThreeDName = SORT
Option = sort_list
value = 199

so there can be the same ThreeDName SORT with different options and value.  But it seems like that IF statement will not allow in a second SORT
0

LVL 86

Expert Comment

>>
so there can be the same ThreeDName SORT with different options and value.  But it seems like that IF statement will not allow in a second SORT
>>

Sounds like you should be sorting an array or collection of objects having those three fields
0

LVL 86

Expert Comment

(sorting or comparing really)
0

LVL 47

Expert Comment

It will not allow to get inside  braces  of your StringBuffer contains SORT or is equal to SORT
0

LVL 47

Expert Comment

Maybe you want to post more of your code if you feel something is not clear in the broader sense,
as I farnkly did not fully understand the full picture of what you are doing,
but I hope  the meaning of indexOf() method is understandable
0

## Featured Post

### Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…