Jon Kho

Ranch Hand
+ Follow
since Jun 03, 2009
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jon Kho

Dear admins..

It seems that I am unable to edit the topic's title which I have created. I remembered clearly that I can do it... I would add a "[Resolved]" in front of it..

Is it the normal?

Thanks

Regards,
Jon
14 years ago
Dear all,

Thanks for your comments..

Now I learned something new again on exceptions ...

Regards,
Jon
14 years ago
Hi Ritchie,

You got it right.. it's ArrayIndexOutOfBoundsException.. I would need this error if there is no "ArrayList's elements".. The reason why I used is because of a situation where there is no elements in the arraylist and I want to get the "ID" but unable because there is no single element to get the ID. So what I did is that I will "catch" this exception and perform the following simple codes..

Which will force it to have a value of 1 and save it into the text file.

Just curious.. Why would you want to recommend to avoid using ArrayIndexOutOfBoundsException? Would it affect the future coding?

Thanks

Regards,
Jon
14 years ago
perhaps you want to try this?

Regards,
Jon
14 years ago
Hi,

maybe can you try change this

to this?


it's a wild guess though..

Regards,
Jon
14 years ago
Hi all and to Ritchie too.

I have used the coding shown below to save the ID that belongs to the last element of the arraylist.. So the coding looks like this..


I am just glad that I am almost done with this function.. i also noticed there is a logic error.. for example..
1
3
4
5// I removed this last ID
next... I would get this..
1
3
4
5// this is the new generated ID..
So in theory, it is not right because every transaction is suppose have a different ID.

I was thinking to save the whole arraylist to a data file. adding another attribute call genID using boolean.. so it would show out only the ones whose ID was already given and avoiding generating the same ID created before.

anyway, later I will read up more on shutdown hooks and IllegalArgumentException ...

Any comments on this topic would be great!

Thanks.

Regards,
Jon
14 years ago
Dear all,

I am here to update my working code for reading the integer from the text file.. thanks Ritchie for the clues you have given!! Here is the coding...


I am just wondering... this line " int number = Integer.parseInt(custID);" would show me this message "local variable hides a field". I am not sure whether it would affect any other codes in the future but it still work as per normal for now..

Now I have two questions in my head.
1st, how do I increase the value by one.. using this? "getAndIncrement();" EDITED: I have just added this feature to the code and it seems working well.
2nd one is How do i save the last integer to the text file?

As for now, I need to get a shuteye for awhile due to lack of sleep and think about these two questions in dreamland...

any comments on this topic would be great!!

Regards,
Jon
14 years ago
Hi Ritchie,

Thanks for the reply, really appreciate it!

The main reason I would want to use Object Stream because it more easier to store data on individual elements(Object's Information) which belongs to the arraylist. The problem with using BufferReader and FileReader is that I have to put in more codes(Even though it not much different but I just want to be more "code" efficient and reducing the chances of code errors) but I like the part is that you can edit the existing information using notepad while Object Stream method can't.

However, in this example where one Integer value is needed and where "BufferReader and FileReader" (in fact i think scanner and FileReader also works) is alot better where you just need to read one line of value which I didn't thought of that until you pointed that out.

As for the shut down hooks.. I try to explain in simple terms.. You have to look what kind of application you are creating. It seems that "shut down hooks" is suitable for server type applications due to the allocation of resources for each connection to the external database. What "shut down hooks" does is to prevent shutdown of the server.

Note: I got the information from hereand here.

So I am creating a simple client application so there is no need for "shut down hooks".

As for the getting the value of Integer from the text file. I have not got any chance to try it out.. I have to restricted my time on java programming as I have another different assignment due this Friday morning. Probably I will look into this Saturday evening..

Any comments on this topic would be great!!

Thanks

Regards,
Jon

14 years ago
it works on with those variables that uses with arraylist...

Regards,
Jon
14 years ago
Hi,

Actually you need to do something like this..



but i dun see any variables declared.. maybe this might help and using google.com? I did not have any experience using hashmap so can't help much..

Regards,
Jon
14 years ago
Hi Ritchie,

Thanks for the comments. In fact, I found an example using BufferReaders and File Reader from here

Dear all,
I have modified the code to read the value from the text file first. Once clearing this, I doubt there is no problem writing the new value to the same txt file. As I would like to do things step by step..

The example code..


I am quite puzzled with one thing. Why is the private static method is above the declaration of variables..

i thought the declaration of Variables only would be at the End or start of the class, The first or the last codes of lines to execute..

for example...


Thanks for this post, any comments would be great!

Regards,
Jon
14 years ago
Thanks for the tip Ritichie,

Using and modifying the coding that i used for other function..
I was thinking whether can I not use arraylist to extract out the one and only integer in the file?



Thanks for any comments on this topic!

Regards,
Jon
14 years ago
opps.. sorry, I guess I didn't make it clear.. He did explain to me in concept. anyway, here it goes..

Problem
Firstly, I am facing the 2 problems for generating ID when after saving the data file..
1) Using the "static int ID = 0; and ID++" method... It is Volatile, The ID will start from 0 once the program is stopped and launched again. It does not start from the last ID generated.

2) Using ArrayList's .size or .ofLastIndex to get the 'starting Int" to generate the ID but the problem comes when the arraylist is empty and there might be duplicates or reuse of the IDs.

Solution
He recommended me to take the last ID that is generated and save it to a file using another class that is just created for this function and every time the program launches. The class would automatically take the last ID that is "saved" to file(Note: the file used is a .dat file that can be opened using notepad).

I am wondering whether is it the same as the normal way to save and retrieving the "ID" aka number to the file.. I think the real problem lies in creating the codes to allow the number from the file into the class and telling the program that is the starting number to generate from.

any comments on this topic would be great!!

Regards,
Jon
14 years ago
Dear all,

I have spoken to my Java lecturer about this case. He told me the of concept to use another "data file" to save the last ID that is last created and using another class to extract it. As the technical details, I have to a way to do it but I am not sure where to start from.

By any chance, if someone knows of this concept do post it there while I am searching for it. It will be very helpful.

Thanks,

Regards,
Jon
14 years ago
Dear all, I have found this article on managing volatility which is good.

Now currently I am still unable to find the suitable solution on what I hope to get. But I learned about Interface Lock and Synchronized from that article.

any comments or help on this topic would be great!!

Thanks.

Regards,
Jon
14 years ago