This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

ArrayList help

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

So I'm trying to make a simple program which takes in an argument (target) and then looks through an ArrayList of strings. If it finds a string that begins with (target) then it will return the index of that string. If it doesn't find a string which begins with (target) then it will return -1 instead.

For some reason, the program is always returning -1, rather than the index of the string within the ArrayList when there is one which matches the search criteria.

Here is the code:



Here is what the results look like:

Running IndexTester
===================

-1
- Expected: 0
-1
- Expected: 2
-1
- Expected: 4
-1
+ Expected: -1

Score
1/4
 
Marshal
Posts: 3760
536
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does your test data look like?
 
Saloon Keeper
Posts: 8943
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you using String.indexOf() ? I don't see it in your code.

Edit: Or, more likely, String.startsWith().
 
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The expression "found = false && j < text.size()" is always false. It can never be true, and hence, the loop always terminates.

Henry
 
Joshua Harris
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
The expression "found = false && j < text.size()" is always false. It can never be true, and hence, the loop always terminates.

Henry



Thanks. I changed the && to || and now I got 3/4 correct. The new results are:

Running IndexTester
===================

0
+ Expected: 0
3
- Expected: 2
4
+ Expected: 4
-1
+ Expected: -1

Score
3/4

Kinda confused as to why it finds 2 of the strings which meet the criteria but somehow doesn't properly find the other one.
 
Joshua Harris
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Are you using String.indexOf() ? I don't see it in your code.

Edit: Or, more likely, String.startsWith().



I'm not using those. Would I need to use them? I'm not looking to return the index of a character within a string, I'm looking to return the index of an element of an ArrayList which begins with a certain string (target).
 
Carey Brown
Saloon Keeper
Posts: 8943
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joshua Harris wrote:

Carey Brown wrote:Are you using String.indexOf() ? I don't see it in your code.

Edit: Or, more likely, String.startsWith().



I'm not using those. Would I need to use them? I'm not looking to return the index of a character within a string, I'm looking to return the index of an element of an ArrayList which begins with a certain string (target).


IndexOf() was my mistake, but, startsWith(), definetly.
 
Joshua Harris
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Joshua Harris wrote:

Carey Brown wrote:Are you using String.indexOf() ? I don't see it in your code.

Edit: Or, more likely, String.startsWith().



I'm not using those. Would I need to use them? I'm not looking to return the index of a character within a string, I'm looking to return the index of an element of an ArrayList which begins with a certain string (target).


IndexOf() was my mistake, but, startsWith(), definetly.



So I changed
if (text.get(j).substring(0, 1).equals(target))
to
if (text.get(j).startsWith(target))

it gives me the same result of 3/4:

Running IndexTester
===================

0
+ Expected: 0
3
- Expected: 2
4
+ Expected: 4
-1
+ Expected: -1

Score
3/4

can you spot another problem? I really don't understand how it's only getting 1 wrong but the rest correct :s

Thanks for telling me about startsWith() though, I never actually knew about that method haha.
 
Carey Brown
Saloon Keeper
Posts: 8943
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your change looks good, so, I'd have to see a copy of your code as it stands now, and your test cases.
 
Joshua Harris
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Your change looks good, so, I'd have to see a copy of your code as it stands now, and your test cases.



Here is the entire thing



Unfortunately I can't actually see how they do the tests, there's literally just a button which says "Test Run" and that's it. No idea why it would find indexes 0 and 4 correctly, but then find 3 instead of 2.
 
Joshua Harris
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:What does your test data look like?



Hey man, sorry I didn't get back to you for a while. I can't actually see the code they use to do the tests unfortunately.
 
Carey Brown
Saloon Keeper
Posts: 8943
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for (int j = 0; found = false || j < text.size(); j++)
You are ASSIGNING false to found here. I expect that you want to COMPARE found to false. You could use found == false but the recommended syntax would be !found. When you change this the logical OR is not correct.
 
Marshal
Posts: 74750
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joshua Harris wrote: . . . Thanks. I changed the && to || and now I got 3/4 correct. . . .

No, you still have an incorrect program.

Never never write == true or == false. Not only is it poor style, but also as CB has already told you, it is easy to write = by mistake and you now have two errors for the price of one. It is often easier to read code if you use if something or while something rather than if not something or while not something.Not
if (b == true) ...
but
if (b) ...
Not
if (b == false) ...
but
if (!b) ...
The same applies for while loops, and in any other boolean expressions.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about something like this

 
Campbell Ritchie
Marshal
Posts: 74750
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Joshua Harris
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:for (int j = 0; found = false || j < text.size(); j++)
You are ASSIGNING false to found here. I expect that you want to COMPARE found to false. You could use found == false but the recommended syntax would be !found. When you change this the logical OR is not correct.



Oh right I see now. I was originally using && instead of || but since it wasn't working I just tried to experiment and change things about. So now I realise that it wasn't working because I had found = false instead of found == false (or !found) haha. Thanks for helping me! I'm still kinda confused as to why it found 2 of the strings (while using the incorrect code) which started with (target) correctly but didn't correctly find the other one. Anyway it's working now and I got 4/4 correct so thanks!
 
Joshua Harris
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Joshua Harris wrote: . . . Thanks. I changed the && to || and now I got 3/4 correct. . . .

No, you still have an incorrect program.

Never never write == true or == false. Not only is it poor style, but also as CB has already told you, it is easy to write = by mistake and you now have two errors for the price of one. It is often easier to read code if you use if something or while something rather than if not something or while not something.Not
if (b == true) ...
but
if (b) ...
Not
if (b == false) ...
but
if (!b) ...
The same applies for while loops, and in any other boolean expressions.



Thanks, I'll try to keep those tips in mind for the future!
 
Joshua Harris
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karthick Pattabiraman wrote:How about something like this



Woah that looks a little too complicated for me, I've only been studying java for a short while so to be honest I've never even seen "finder" or "break" before. Thanks for making the effort to help though!
 
Joshua Harris
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch



Thanks... I'm sure I'll enjoy this place :p
 
Karthick Pattabiraman
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Woah that looks a little too complicated for me, I've only been studying java for a short while so to be honest I've never even seen "finder" or "break" before. Thanks for making the effort to help though!



Me too. But "break" is a common construct for any programming language. and "Finder" is just the class name provided by you.
 
Karthick Pattabiraman
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch



Thanks you. Being the last one to join this forum in this thread, I assume this is intended to me.

 
Ranch Hand
Posts: 62
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joshua Harris wrote:



Is this what you want?

Sample Execution:


Enter letter: b
Your index is: 1


It returned 1 because "banana" is the first String that starts with "b" in the List.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic