Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

ArrayList of objects with auto incremented id number  RSS feed

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have built a rental program that allowed the user to add rentals and fill objects which i have put into an arrayList. I have successfully completed this part although i do have a problem with my auto increment id number. When the objects are filled then entered into the arrayList the id increments properly, but when i search the arrayList for a specific id number entered by the user it returns the correct entry but prints it to console with a new incremented id number. Has anyone came across this problem and could maybe help me solve it. Any help would be appreciated.  
 
Marshal
Posts: 62259
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please show us how you are creating the ID.
 
onagh grange
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi thank you . I think i have fixed that problem but now have run into another. If you could please help me fix this code. When i search through the arraylist after id is entered it is finally showing the right one. Although the code underneath it to update the fields of that specific object wont run.

 
onagh grange
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have took out the input.next() and it runs but does not update the fields.
 
Campbell Ritchie
Marshal
Posts: 62259
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please always use the code button; because you are new, I have edited your post with the code button, and you can see how much better it looks
Why is your method marked static?
Don';t create lots of Scanner objects to read from System.in. Create one per application and use it for everything and don't close it.
What does line 8 do? Check it very carefully. That is the first thing I noticed, so I shall stop and see what happens when you deal with line 8.
Come to think of it, isExist isn't a good name for a variable, and line 15 looks strange. Maybe found would be a better name.
 
Campbell Ritchie
Marshal
Posts: 62259
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

onagh grange wrote:i have took out the input.next() and it runs but does not update the fields.

Please explain why you had input.next() there in the first place. But have a look at this old post first.
 
onagh grange
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thank you for fixing it. line 8 (get(Count)) is the reference for the id number to return the object that has the same id number as get count, because the id number was initialised as count to increment automatically.
I read the post you suggested and made a few changes and added in the input.nextLine() although now its not returning the right object and will only let me enter a new customer name but not sure if it will update it. i do appreciate if you can help.
 
Master Rancher
Posts: 933
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah line 8 also seems weird to me, I think you confused in with input, so does line 13.
. So you say if isExist is true, then you comment game found, yet you print out Game does not exist or isn't found.
 
Campbell Ritchie
Marshal
Posts: 62259
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

onagh grange wrote:Hi,

Thank you for fixing it. line 8 (get(Count)) is the reference for the id number . . .

That is only half of line 8. You have overlooked half of it. Start by working out what the input.equals bit means.

I read the post you suggested and made a few changes and added in the input.nextLine() although now its not returning the right object and will only let me enter a new customer name but not sure if it will update it. i do appreciate if you can help.

Careful about these changes; you are making changes faster than I can read them and not telling me what the changes mean.

Go through the code and find every reference to your Scanner. Write down a typical set of inputs no a sheet of paper, and go through them showing where the Scanner has got to, what it is returning, and where you are using that result. Remember what I said in the old post. Also remember that many books don't give a good description of Scanner, which makes many people dislike it. I am however a Scanner‑o‑phile, but I understand that you need to know how it works before using it. Please copy what you found out about your Scanner, so we can see what is happening for ourselves.
 
onagh grange
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Would there be a better approach as this is not working. I am not sure what .equals means if i am going to be honest. i will check the scanner and post back.
 
Daniel Demesmaecker
Master Rancher
Posts: 933
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

onagh grange wrote:Hi,

Would there be a better approach as this is not working. I am not sure what .equals means if i am going to be honest. i will check the scanner and post back.



.equals compares the value of two string for equality. I would indeed look at taking a different approach, since a lot of what you're trying to do is not needed or could be simplified.
 
Campbell Ritchie
Marshal
Posts: 62259
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The good news is, you can read about Scanner here, and in the Java™ Tutorials. You will find the equals() method is unchanged from that in Object. Please have a look at that, and don't let the five bullets scare you. The important pat is just after the 5th bullet.
The bad news is, those sources don't give a complete tutorial about how to use Scanners. You won't find much explanation about what happens if you follow next() or nextInt() by nextLine().
 
Campbell Ritchie
Marshal
Posts: 62259
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:. . .equals compares the value of two string for equality.

There aren't any Strings in that line.

I would indeed look at taking a different approach, since a lot of what you're trying to do is not needed or could be simplified.

Agree that code can be simplified a lot, but let's see if we can't correct it first. OP will learn a lot more like that.
 
Campbell Ritchie
Marshal
Posts: 62259
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please call a method returning the ID getId() or similar.
 
onagh grange
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scanner input = new Scanner(System.in);
       System.out.println("Please enter ID number :");
       int in = input.nextInt();


       boolean found = false;
       for (VideoGame x : listOfRentals) {
           if (in == x.getGameID()) {
               //Your logic if it's there.
               found = true;

           }
           if (found) {
               //Found in the arrayList.
               System.out.println("Sorry Game does not exist or is wrong");
           }
           System.out.println(x);
           input.nextLine();

           //Your logic if it's there.
           System.out.println("Please enter customer Name:");
           String cusName = input.nextLine();
           x.setCusName(cusName);

           input.nextLine();
           System.out.println("Please enter customer Number:");
           int cusNum = input.nextInt();

           x.setCusNum(cusNum);
           input.nextLine();

           System.out.println("Please enter Date of Rental:");
           String date = input.nextLine();
           x.setDate(date);
       }

 
Daniel Demesmaecker
Master Rancher
Posts: 933
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

There aren't any Strings in that line.


Let me rephrase that, since it isn't fully true eighter, it compares two values/objects for equality. I simply always think of strings cause in general I only use it to compare Strings...
 
onagh grange
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Scanner input = new Scanner(System.in);
       System.out.println("Please enter ID number :");
       int in = input.nextInt();


       boolean found = false;
       for (VideoGame x : listOfRentals) {
           if (in == x.getGameID()) {
               //Your logic if it's there.
               found = true;

           }
           if (found) {
               //Found in the arrayList.
               System.out.println("Sorry Game does not exist or is wrong");
           }
           System.out.println(x);
           input.nextLine();

           //Your logic if it's there.
           System.out.println("Please enter customer Name:");
           String cusName = input.nextLine();
           x.setCusName(cusName);

           input.nextLine();
           System.out.println("Please enter customer Number:");
           int cusNum = input.nextInt();

           x.setCusNum(cusNum);
           input.nextLine();

           System.out.println("Please enter Date of Rental:");
           String date = input.nextLine();
           x.setDate(date);
       }

code=java][/code]

I tried this and it didnt work
 
Daniel Demesmaecker
Master Rancher
Posts: 933
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lol good that you use the code tags, but they don't do a lot if you don't post your code in between them
 
Campbell Ritchie
Marshal
Posts: 62259
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks like line 8 sorted out but line 15 still looks strange. And I haven't looked beyond line 15 yet.

Please have another look at the link I posted about the code button. The [code=java] part goes before the code and the [/code] part after it.
 
onagh grange
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying, looks like to no avail at the minute.
 
Campbell Ritchie
Marshal
Posts: 62259
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain what that means; I can only help if I know what is going wrong.
 
onagh grange
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[code=java boolean found = false;
       for (VideoGame x : listOfRentals) {
           if (in == x.getGameID()) {
               //Your logic if it's there.
               found = true;

           }
           if (!found) {

               System.out.println("Sorry Game does not exist or is wrong");
           }
           System.out.println(x);
][/code]

i changed it to this and still not working. Its not returning the matching id numbers and when prompt for customer details it is not updating them to input.
 
Daniel Demesmaecker
Master Rancher
Posts: 933
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you check if the game is found, if not you print that out, but the rest of your code is always being executed, even when the game is not found...
 
Campbell Ritchie
Marshal
Posts: 62259
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Highlight the entire code block and then push the code button.

What is that code supposed to do? Are you printing the ID number, or what?
Please tell us what is going wrong; I don't like having to guess.
 
Campbell Ritchie
Marshal
Posts: 62259
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you need to explain what you are trying to do, rather than what you are doing now, viz. trying to work out how to do it.
 
Daniel Demesmaecker
Master Rancher
Posts: 933
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you trying to do here?


That might be the prob
 
Daniel Demesmaecker
Master Rancher
Posts: 933
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I think you need to explain what you are trying to do, rather than what you are doing now, viz. trying to work out how to do it.


My guess is he's just trying to update an object in his list
 
Daniel Demesmaecker
Master Rancher
Posts: 933
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried you're code, as I thought, check your code again. You only need to consume the empty line when not using next or nextLine
 
Saloon Keeper
Posts: 2091
77
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I feel that your make rental code should be something like this :

searchGameById should do the job of searching and returning. Your current code of looping through all rentals is not right.
 
Daniel Demesmaecker
Master Rancher
Posts: 933
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I alrady pointed out, there a lot of ways this code could be simplified, the objective was to make it run first. He also dosn't need the boolean
 
salvin francis
Saloon Keeper
Posts: 2091
77
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:As I alrady pointed out, there a lot of ways this code could be simplified,

Agreed, I am trying to push OP in the right direction too

Daniel Demesmaecker wrote: the objective was to make it run first...


This is a matter of preference, some problems need a nudge in the right direction and some problems need to solved first and then optimized. In this example I feel that separating intent of the method into smaller manageable method calls with clear intent may help OP solve the problem better.
 
Daniel Demesmaecker
Master Rancher
Posts: 933
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lol, I agree, but I'm carefull now a days with writting to mutch code to help someone out especially in the beginning java forum. So If a mod tell me let him try and figure out it himself, I'm a good little boy and obey
 
onagh grange
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my main goal with this code is to iterate through the loop using a comparator ie the user input of id number. once the id number is found i would like to retrieve that object and update three fields of that object, the customer name, number and the date. I have worked with arrayLists and arrays before but not using user defined methods. I will take your advice and do more research. I appreciate your help.
 
Daniel Demesmaecker
Master Rancher
Posts: 933
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wel the comparator worked, it's not clean code, but it does the trick. The problem is the overuse input.nextLine(). I was able to update an object of the list using the code you posted
 
onagh grange
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
unfortunately it did not work for me. It did not retrieve the right object via the id number
 
Daniel Demesmaecker
Master Rancher
Posts: 933
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is that even possible? I used the same code, just clened it up a bit:
 
onagh grange
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
may i ask about line 5? i already have an Arraylist would i need to create another?
 
salvin francis
Saloon Keeper
Posts: 2091
77
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:Lol, I agree, but I'm carefull now a days with writting to mutch code to help someone out especially in the beginning java forum.


I am pretty sure the staff meant that you should not share a full baked solution. There are rules for that here.

onagh grange wrote:unfortunately it did not work for me. It did not retrieve the right object via the id number


That is unfortunate, can you share the searchGameById method implementation where you pass an id and it retrieves the wrong object ?
 
Daniel Demesmaecker
Master Rancher
Posts: 933
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

searchGameById should do the job of searching and returning. Your current code of looping through all rentals is not right.


You don't even need a searchGameById method, his id is auto increment so most likely equal to the index of the list (if he started at zero) or equal to index+1 or list.lenght.
so you could do list.get(id) or list.get(id-1)
 
You ridiculous clown, did you think you could get away with it? This is my favorite tiny ad!
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!