Forums Register Login

Unable to increment id

+Pie Number of slices to send: Send
Hi guys, I have create a Note class that has Id, Name and Description. I am unable to automatically increment the id whenever a new Note is being created. Please help me increment the id.




And the input is taken from the console.
+Pie Number of slices to send: Send
Write to the Console
1. Add Note
2. To View Note
3. Exit
2
Write to the Console
1. Add Note
2. To View Note
3. Exit
1
Enter the title
storybook
Enter the description
Asterix And Obelix

descriptionAsterix And Obelix
--------------------
Write to the Console
1. Add Note
2. To View Note
3. Exit

1
Enter the title
storybook
Enter the description
Harry Potter

descriptionHarry Potter
--------------------
Write to the Console
1. Add Note
2. To View Note
3. Exit
2
Id : 2
Title :storybook
Description :Harry Potter
Id : 2
Title :storybook
Description :Harry Potter
Write to the Console
1. Add Note
2. To View Note
3. Exit
Write to the Console
1. Add Note
2. To View Note
3. Exit
2
Write to the Console
1. Add Note
2. To View Note
3. Exit
1
Enter the title
storybook
Enter the description
Asterix And Obelix

descriptionAsterix And Obelix
--------------------
Write to the Console
1. Add Note
2. To View Note
3. Exit

1
Enter the title
storybook
Enter the description
Harry Potter

descriptionHarry Potter
--------------------
Write to the Console
1. Add Note
2. To View Note
3. Exit
2
Id : 2
Title :storybook
Description :Harry Potter
Id : 2
Title :storybook
Description :Harry Potter
Write to the Console
1. Add Note
2. To View Note
3. Exit
+Pie Number of slices to send: Send
Ignore the previous output


Output

Write to the Console
1. Add Note
2. To View Note
3. Exit
2
Write to the Console
1. Add Note
2. To View Note
3. Exit
1
Enter the title
storybook
Enter the description
Asterix And Obelix

descriptionAsterix And Obelix
--------------------
Write to the Console
1. Add Note
2. To View Note
3. Exit

1
Enter the title
storybook
Enter the description
Harry Potter

descriptionHarry Potter
--------------------
Write to the Console
1. Add Note
2. To View Note
3. Exit
2
Id : 2
Title :storybook
Description :Harry Potter
Id : 2
Title :storybook
Description :Harry Potter
Write to the Console
1. Add Note
2. To View Note
3. Exit
+Pie Number of slices to send: Send
It looks like you are creating just one note and then modifying it. Wouldn't you want to create multiple Notes and store them in a NoteBook (List<Note>) or something?
+Pie Number of slices to send: Send
Avoid coloured text, which is very difficult to read; I have changed it to black. Also only use that form of package name if you own techlabs.com or techlabs have authorised you to use that name.

Whether it is a good idea to use a nextId field or not is a different question, but your problem is caused by not having a nextId field. What you are doing is creating the id field anew every time you create an instance, so it will start again from 0. The customary way to do this is to have an id field and a nextId field; you only want one copy of nextId and you want to increment it whenever a new instance is created, so you make it static.Note:-
  • 1: The way I wrote it, the first id will be 0.
  • 2: The way I wrote it, you cannot write a setId() method.
  • I can see some serious problems with your code, I am afraid.
  • 1: Why are you incrementing id in lines 17 and 35?
  • 2: Why are you throwing an Exception and catching the same Exception in the same method?
  • 3: Why didn't you call that exception class XXXException?
  • 4: What is going to happen to the static nextId field when you serialise and deserialise your objects?
  • Your line 25 is poor style:-
  • 1: It is long enough to be difficult to read.
  • 2: After you carefully wrote number literals to use in the switch, you shou‍ld have used those literals to form the message String:- ... EXIT + " to exit..."
  • +Pie Number of slices to send: Send
    The note i create is getting save in  serialized file . Line 45 - 46 do it.

    notes.add(note);
    service1.noteSerialize(notes);

    (is this the answer to  what you talking about ?)
    +Pie Number of slices to send: Send
     

    Sucheta Shrivastavas wrote:. . . (is this the answer to  what you talking about ?)

    No.

    I meant, what is going to happen to the static field, particularly if you close the JVM and then later open a new JVM, or run two terminals with two JVMs simultaneously and pass serialised note objects from one JVM to the other?
    +Pie Number of slices to send: Send
    @ Campbell Ritchie

    1. I am sorry to repeat the "id ++" twice. sorry my mistake.

    2. I am throwing an Exception. That is a user-defined exception and i have created a separate clas for it. My sir taught it like that to define user defined exception.



    3. I made changes in the Note class as :

    private String title;
       private static int nextId=0;
    private String description;
    private int id;

    public Note(){
    id = nextId++;
    }


    4. Now i am getting InvalidClassException . How to resolve that

    java.io.InvalidClassException: com.techlabs.notes.businesslayer.Note; local class incompatible: stream classdesc serialVersionUID = -332854762966116870, local class serialVersionUID = 1
    Write to the Console
    1. Add Note
    2. To View Note
    3. Exit
    +Pie Number of slices to send: Send
    @ Campbell Ritchie
    1. i corrected the error i was getting as InvalidClassException.

    2.The solution you provided to increment the id as -



    is giving an errneous output - id s remaining the same for all outputs.

    Output
    Write to the Console
    1. Add Note
    2. To View Note
    3. Exit
    1
    Enter the title
    storybook
    Enter the description
    Lost Symbol

    description :Lost Symbol
    --------------------
    Write to the Console
    1. Add Note
    2. To View Note
    3. Exit
    2
    Id : 2
    Title :storybook
    Description :Harry Potter
    Id : 2
    Title :storybook
    Description :Harry Potter
    Id : 1
    Title :storybook
    Description :Lost Symbol
    Write to the Console
    1. Add Note
    2. To View Note
    3. Exit
    1
    Enter the title
    poem
    Enter the description
    humpty dumpty
    2
    description :humpty dumpty
    --------------------
    Write to the Console
    1. Add Note
    2. To View Note
    3. Exit
    2
    Id : 2
    Title :storybook
    Description :Harry Potter
    Id : 2
    Title :storybook
    Description :Harry Potter
    Id : 2
    Title :poem
    Description :humpty dumpty
    Id : 2
    Title :poem
    Description :humpty dumpty
    Write to the Console
    1. Add Note
    2. To View Note
    3. Exit
    +Pie Number of slices to send: Send
     

    Sucheta Shrivastava wrote:. . . InvalidClassException.

    Have you looked up the details of that Exception? The documentation gives you all the details you need to know. If you don't understand them, please ask again.

    2.The solution you provided to increment the id as . . . is giving an errneous output - id s remaining the same for all outputs. . . .

    No, I gave a solution which will supply a different ID for each object. As Tim has already told you, you are not using multiple objects (at least you weren't in the first version.
    +Pie Number of slices to send: Send
     

    Sucheta Shrivastava wrote:. . . 1. I am sorry to repeat the "id ++" twice. sorry my mistake.

    The mistake wasn't repeating the ++ call. Having it there at all is the real mistake.

    2. I am throwing an Exception. That is a user-defined exception . . .

    I know that, but it shou‍ld be called SomethingException.

    . . .

    That is not the correct way to create an Exception class. Look in the Java™ Tutorials for a bit of advice, but in 99% of cases all you need is constructors; the code shou‍ld look like this:-You may wish to implement the 5th constructor, too.
    +Pie Number of slices to send: Send
    This is my Serialization code







    DeSerialization code :






    But what is wrong with my code. Is there a problem with my serialization & deserialization code , because  my output is not proper.  Output is repetitive and some of it doesn't appear
    Output :

    Write to the Console
    1. Add Note
    2. To View Note
    3. Exit
    1
    Enter the title
    storybook
    Enter the description
    gulliver travels

    --------------------
    Write to the Console
    1. Add Note
    2. To View Note
    3. Exit
    2
    Id : 1
    Title :storybook
    Description :red ridinghood
    Id : 1
    Title :storybook
    Description :red ridinghood
    Id : 1
    Title :poems
    Description :null
    Id : 1
    Title :poems
    Description :null
    Id : 0
    Title :School
    Description :vidya vikasini
    Id : 0
    Title :storybook
    Description :gulliver travels
    Write to the Console
    1. Add Note
    2. To View Note
    3. Exit
    1
    Enter the title
    Poems
    Enter the description
    Johnny Johnny

    --------------------
    Write to the Console
    1. Add Note
    2. To View Note
    3. Exit
    2
    Id : 1
    Title :storybook
    Description :red ridinghood
    Id : 1
    Title :storybook
    Description :red ridinghood
    Id : 1
    Title :poems
    Description :null
    Id : 1
    Title :poems
    Description :null
    Id : 0
    Title :School
    Description :vidya vikasini
    Id : 1
    Title :Poems
    Description :Johnny Johnny
    Id : 1
    Title :Poems
    Description :Johnny Johnny
    Write to the Console
    1. Add Note
    2. To View Note
    3. Exit



    +Pie Number of slices to send: Send
    @ Campbell

    Thanks for the advice on Exception. Can you please sort out  the above problem.
    +Pie Number of slices to send: Send
    Did you see what it said about that kind of exception in the link I showed you?
    +Pie Number of slices to send: Send
    @ Campbell

    public class PQRException extends XYZException
    {
      public PQRException()
      {
         super(); // what will super() here come from??
      }

      public PQRException(String message)
      {
         super(message);//what will super(message) run and how.because there is no parent exception class here
      }

      public PQRException(Throwable cause)
      {
         super(cause);  // what will be the cause?
      }

      public PQRException(String message, Throwable cause)
      {
         super(message, cause);
      }
    }


    everywhere in the meassage you have written super. so there need to be a parent class to validate the super. where will it come frm. Most importantly, where and what would i write in the parent class . My class i created BeyondTheSpecifiedLength exception is working.
    +Pie Number of slices to send: Send
     

    Sucheta Shrivastava wrote:. . . you have written super. so there need to be a parent class to validate the super. where will it come frm. . . .

    It says extends XYZException in the code. You create the XYZException class similarly.
    Don't say parent class and child class because the analogy with genetic inheritance is misleading. Say superclass and subclass. Maybe the terms they use in C# (base class and derived class) are better still.

    My class i created BeyondTheSpecifiedLength exception is working.

    The fact that you can get something to run doesn't necessarily make it correct.

    What did you learn when you read the details for the invalid class exception? Did you understand what you read? Please tell us if there was something you didn't understand.
    +Pie Number of slices to send: Send
    I merged your stuff with the following thread. I hope that is okay by you.
    +Pie Number of slices to send: Send
    Hi, here i attach my code. Its a Notes application wherein i create a note / view notes/ exit.
    The output is very unstable. Sometimes the correct output comes. Sometimes the previous note gets overridden.I am serializing the output. i.e. string it in a file and deserializing it i.e. retreiving from the file. I have shown the output . please check.

    Class Note Console-



    Note Class.




    Serialized class -



    Exception class




    Output -

    Write to the Console
    1. Add Note
    2. To View Note
    3. Exit
    2
    The number of notes are:2
    Id : 1
    Title :country
    Description :israel
    Id : 1
    Title :country
    Description :israel
    Write to the Console
    1. Add Note
    2. To View Note
    3. Exit
    1
    Enter : 1. Id already present or  2. To create a new id
    1
    Enter the id

    1
    Id : 1
    Title :country
    Description :israel
    Id : 1
    Title :country
    Description :israel
    Write to the Console
    1. Add Note
    2. To View Note
    3. Exit
    1
    Enter : 1. Id already present or  2. To create a new id
    2
    Enter a new id
    2
    Enter the title
    developer
    Enter the description
    wipro

    --------------------
    Write to the Console
    1. Add Note
    2. To View Note
    3. Exit
    2
    The number of notes are:3
    Id : 1
    Title :country
    Description :israel
    Id : 1
    Title :country
    Description :israel
    Id : 2
    Title :developer
    Description :wipro
    Write to the Console
    1. Add Note
    2. To View Note
    3. Exit
    1
    Enter : 1. Id already present or  2. To create a new id
    2
    Enter a new id
    3
    Enter the title
    trainee
    Enter the description
    Tech Mahindra

    --------------------
    Write to the Console
    1. Add Note
    2. To View Note
    3. Exit
    2
    The number of notes are:4
    Id : 1
    Title :country
    Description :israel
    Id : 1
    Title :country
    Description :israel
    Id : 3
    Title :trainee
    Description :Tech Mahindra
    Id : 3
    Title :trainee
    Description :Tech Mahindra
    Write to the Console
    1. Add Note
    2. To View Note
    3. Exit
    +Pie Number of slices to send: Send
    Output -

    Write to the Console
    1. Add Note
    2. To View Note
    3. Exit
    2
    The number of notes are:4
    Id : 1
    Title :country
    Description :israel
    Id : 1
    Title :country
    Description :israel
    Id : 3
    Title :trainee
    Description :Tech Mahindra
    Id : 3
    Title :trainee
    Description :Tech Mahindra
    Write to the Console
    1. Add Note
    2. To View Note
    3. Exit
    2
    The number of notes are:4
    Id : 1
    Title :country
    Description :israel
    Id : 1
    Title :country
    Description :israel
    Id : 3
    Title :trainee
    Description :Tech Mahindra
    Id : 3
    Title :trainee
    Description :Tech Mahindra
    Write to the Console
    1. Add Note
    2. To View Note
    3. Exit
    1
    Enter : 1. Id already present or  2. To create a new id
    1
    Enter the id
    3
    Id : 3
    Title :trainee
    Description :Tech Mahindra
    Id : 3
    Title :trainee
    Description :Tech Mahindra
    Write to the Console
    1. Add Note
    2. To View Note
    3. Exit
    2
    The number of notes are:4
    Id : 1
    Title :country
    Description :israel
    Id : 1
    Title :country
    Description :israel
    Id : 3
    Title :trainee
    Description :Tech Mahindra
    Id : 3
    Title :trainee
    Description :Tech Mahindra
    Write to the Console
    1. Add Note
    2. To View Note
    3. Exit
    1
    Enter : 1. Id already present or  2. To create a new id
    2
    Enter a new id
    4
    Enter the title
    mumbai
    Enter the description
    trains and heavy rains

    --------------------
    Write to the Console
    1. Add Note
    2. To View Note
    3. Exit
    2
    The number of notes are:5
    Id : 1
    Title :country
    Description :israel
    Id : 1
    Title :country
    Description :israel
    Id : 3
    Title :trainee
    Description :Tech Mahindra
    Id : 3
    Title :trainee
    Description :Tech Mahindra
    Id : 4
    Title :mumbai
    Description :trains and heavy rains
    Write to the Console
    1. Add Note
    2. To View Note
    3. Exit
    1
    Enter : 1. Id already present or  2. To create a new id
    2
    Enter a new id
    5
    Enter the title
    festival
    Enter the description
    ganesh chaturthi

    --------------------
    Write to the Console
    1. Add Note
    2. To View Note
    3. Exit
    2
    The number of notes are:6
    Id : 1
    Title :country
    Description :israel
    Id : 1
    Title :country
    Description :israel
    Id : 3
    Title :trainee
    Description :Tech Mahindra
    Id : 3
    Title :trainee
    Description :Tech Mahindra
    Id : 5
    Title :festival
    Description :ganesh chaturthi
    Id : 5
    Title :festival
    Description :ganesh chaturthi
    Write to the Console
    1. Add Note
    2. To View Note
    3. Exit
    +Pie Number of slices to send: Send
    Forget about serialisation until you have the note class sorted out. At present it is still full of errors. Why are you not setting the ID in the constructor? Why is the ID not fixed for ever? Why are you catching the Exception in the same place you are throwing it? How are you going to ensure that different notes have different IDs?
    You haven't taken these comments into consideration, though you were told them several days ago.

    I do not believe you have a new question, so I am merging this discussion with your previous one.
    +Pie Number of slices to send: Send
    @campbell
    I have used the setId method to set the id. Is that not proper  notation. You mean to say i should create a constructor like

    public Note{
    id = nextId++;
    }
    I did that but today i had class , my sir refused to make such a notation. Instead he asked to check if the id is already present if not then ask the user to enter the id. Even when i had created that constructor to increment the id, still it was not working properly .

    About exception,how do i make it in my id . Can you please make the changes and show. Because i am unable to understand how would the syntax be. what changes to make in the syntax.

    what other errors in my program?
    +Pie Number of slices to send: Send
    @ Campbell .
    The Invalid Class Exception that i was getting got resolved by i line of code .
    private static final long serialVersionUID = -332854762966116870L;

    It is no longer  throwing such an exception. I surfed for such an exception.
    +Pie Number of slices to send: Send
    The reason you were getting that Exception is that the old serialised classes had a different SUID calculated “on the fly”. The JVM calculated the SUID as it serialised the objects, and then calculated the SUID on the new Class<> object, which was different. That means the classes were different and the old object couldn't be deserialised. By supplying an SUID, you are asserting that this Class<> object is the same as the previous version, so no Exception.
    +Pie Number of slices to send: Send
    @ campbell

    Thank you.

    Can you figure  out why am i getting repetitive ouputs. Like if i make an entry say
    id :3
    title : mumbai
    description : trains and heavy rains

    and another entry
    id :4
    title :storybook
    description : red ridinghood

    then when i view the note i would get the output as :

    id:3
    title:storybook
    description : red ridinghood

    id:4
    title : storybook
    description:red ridinghood

    it is overwriting the id 3 contents
    +Pie Number of slices to send: Send
    @ campbell

    please read posts 21,22,23 all are for you.
    +Pie Number of slices to send: Send
    You have a lot of code, and therefore I haven't read it in any detail, but the description of your problem suggests to me that you're using a single Note object and modifying it each time you input a title, instead of creating a new Note object for each title.
    +Pie Number of slices to send: Send
    @ Paul Clapham

    Can you please show me how to create a new object everytime. I don't understand how to make a new object everytime.
    +Pie Number of slices to send: Send
    @ Paul

    As i understand everytime a new title is entered or a new id or description  it is stored in a new memory object. But as you say maybe i might be overriding on the same memory location.

    How to create a new object everytime?
    +Pie Number of slices to send: Send
    Here's how to create a new Note object:



    But I suspect you probably know that already. Next, look around your code. Do you just see this creation being done at the beginning of the processing? If so, then change it so that the creation is done every time you read a title from the console.

    Otherwise you just have one Note object and when you finally go to write out the list, it's a list which contains only references to that one Note object. And that one Note object contains only data from the last title at that point.
    +Pie Number of slices to send: Send
    Is this the way you create a new note object? My output is still errored and repetitive

    +Pie Number of slices to send: Send
    Putting all the code into one block is very error‑prone. That amount of code ought to represent several separate methods. Looking for a note with a particular ID shouldn't involve iterating the entire List. You shou‍ld have some way to index it from the ID. If you have consecutive IDs, you might use them as an index in a List or similar. Otherwise you could find out about Maps.
    +Pie Number of slices to send: Send
    @Campbell Ritchie

    Thanks. I made a separate method and now its working fine
    +Pie Number of slices to send: Send
    VIEW _NOTE :



    displayNote -

    +Pie Number of slices to send: Send
    The display method doesn't follow OO principles, I am afraid. You shou‍ld simply write System.out.println(myNote); and get a display, using the note class' toString method. Why is that method static? It is also named incorrectly; it shou‍ld be called displayNotes. Why are you declaring that method with throws Exception? I can't see anything likely to throw exceptions.
    Arthur, where are your pants? Check under this tiny ad.
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 701 times.
    Similar Threads
    Why is the entire text not getting printed
    non-static method cannot be referenced from a static context
    Two scanner FileReader
    Menu Code repeating itself
    Endless loop !
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Mar 28, 2024 04:37:12.