• 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
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

input command and indexOf

 
Ranch Hand
Posts: 30
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you use an input command with the indexOf statement?  If so how would it look?  
 
Greenhorn
Posts: 21
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
indexOf() method is applicable for any String object. E.g. int i = "my_string".indexOf("_");
i == 2;

Not clear, what do you mean by "input command"?
 
David Ausere
Ranch Hand
Posts: 30
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my code which is obvious doo doo.


My expected outcome is I input the student ID.  Then removeID is given the Index position of that input.
 
Ranch Hand
Posts: 67
1
Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I assume you're looking for something like this
 
David Ausere
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tried that but it just returns a value of -1 every time.
 
Rancher
Posts: 5114
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

returns a value of -1 every time.  


Can you post the code and both the Strings used in the indexOf method so we can see what you are talking about?
 
David Ausere
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure thing.

This is my updated code (Not working that I am throwing my hands up in the air with)


I have a menu Option 1,2,3,4

I can add a Student.  I can View and not I am on the deleting portion.

I tried this method



But obviously there are some errors in my logic with this way.  So I was thinking..

Can I search the array for the Student ID and get the Array position for that ID and then delete that ID.  I tried and I failed miserably at attempt.

So then I am thinking can I search for the First and Last name get a match with the array position and then delete that one.  Which I am failing miserably at as well.

Need some guidance for sure.
 
Norm Radder
Rancher
Posts: 5114
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are the Strings that you are using with the indexOf method?

Where is the indexOf method used in your code? I can't see where the statement is that you are having problems with.
 
David Ausere
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Norm,

I had deleted that code out of frustration unfortunately.  But at the top of the screen you can see what I "tried".
 
Norm Radder
Rancher
Posts: 5114
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, there is a lot of confusion.  I don't see an indexOf in the first post.  I'd rather not guess.
Can you post the code, the Strings that were used and the error messages?
 
David Ausere
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Norm,

All the code is there.  The whole program.  The indexOf is just one way I tried to accomplish my goal.  So I tried what Zephro suggested, but all I got was  return of -1 not the index.  
 
Norm Radder
Rancher
Posts: 5114
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, there is a lot of code on this thread.  I don't see where there is an indexOf method used in the program so I can't recommend how you change the code to use the method correctly.

accomplish my goal


Is this your goal:

search the array for the Student ID


Normal array searches are done with a loop through the elements in a array. An if test is used to look for desired elements.
If the array contains Student objects, what methods does the Student class have you can use to detect the desired object?
 
David Ausere
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are the 2 seperate things I tried.  Neither worked.



And




 
David Ausere
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My arraylist name is
ArrayList<Student> Roster = new ArrayList<>();
 
Norm Radder
Rancher
Posts: 5114
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Neither worked.  


Please copy the full text of the error messages and paste them here so we can see where the error is and also see what the compiler thinks is wrong.
 
David Ausere
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my Superclass if that helps.
 
Marshal
Posts: 80665
478
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ivan Pronin, welcome to the Ranch
 
Saloon Keeper
Posts: 5617
214
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@David,

if I understand correctly, you want to remove a Student whose ID you have. Right?

If so, have a look at the API of the Collection class. It has a very handy method: removeIf(Predicate<? super E> filter).
For instance: Roster.removeIf(student -> student.shoeSize == 40);
 
David Ausere
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Piet,

This way MOSTLY works!  Issue I am running into with code is this.

Lets say I have 5 students.  I delete the 5th (ID 5) student and viola he gets deleted.  Here is where the problem arises.  Now I cannot delete the last student in the array after that (student ID 4) but I CAN delete all the other students.

Here is what my code looks like.  Do you see what may be causing this?

 
David Ausere
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or if I delete ANY student the last student cannot be deleted after that.
 
Norm Radder
Rancher
Posts: 5114
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you make a small simple program that compiles, executes and shows the problem.  
Build an ArrayList, add elements and then use the removeIf() method the in the way you are reporting the problem so we can see the problem that you are seeing.

Or if that is too hard, add some print statements to your code that prints out the values of the variables used and the contents of the arraylist to show what you are talking about.
 
David Ausere
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the program all in one.



Run it.  Add 5 or so students then start deleting students.  You will see.
 
Norm Radder
Rancher
Posts: 5114
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Add 5 or so students then start deleting students.


Could you write a test driver that does all that to make our testing easier and so that the tester would see exactly what you see when the problem happens?

One way is to load all the responses to the Scanner next methods in the Scanner class's constructor as a String:
 
replace the ... with all the necessary responses for the testing.

Or put all of the user input in a file: StudentData.txt
and change the Scanner constructor to read from that file:
   


That message should include the values of the variables that cause it to be printed.
 
Piet Souris
Saloon Keeper
Posts: 5617
214
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whatever the cause, there is a flaw in the way you determine the Student ID. See the code for (menuChoice == 1). If you start afresh, then add 4 students, they get the ID's of 1, 2, 3, 4.

Now, delete student 1. Assuming that is successfull, can you now see, in the code (menuChoice == 3), why you cannot delete student 4? And also, if you add a new student, what ID does that student get?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic