Log in

View Full Version : Java Programming help needed.


Kujiro
July 26th, 2011, 01:19 AM
Any one is familiar with Java arrayList and array management ?

I have an object
Pet(String PetId, String ownerName, String description )
Which I need to be created in an arraylist.

pet[] createpet = new pet[3]
createpet[0] = new Pet(..attributes..)
Hence createpet itself will be an array.

But I need that array to be placed in an arrayList.
ArrayList<String> document = new ArrayList <String>();

How can it be done so which I may access the arraylist for the information?

Any form of help is apperciated

Cheers

TheMatrix
July 26th, 2011, 01:25 AM
Isn't an "arrayList" the same as an array?
I could write you something really quickly in Perl, but I don't think that'd be of any use to you ;)

Anyways, this may not work but I'll give it a shot:

ArrayList<String> document = new ArrayList <String>( createpet[] );

Will that work?

Kujiro
July 26th, 2011, 01:32 AM
In java nope, as arraylist are expandable on runtime, but arrays needs to be predefined during coding

Isn't an "arrayList" the same as an array?
I could write you something really quickly in Perl, but I don't think that'd be of any use to you ;)

Anyways, this may not work but I'll give it a shot:

ArrayList<String> document = new ArrayList <String>( createpet[] );

Will that work?

Nope it will hit syntax.

document.add(new pet(...attributes..)) would work but it places each attribute separately in the arrayList

Don't double post please, use the edit button - Kaius

TheMatrix
July 26th, 2011, 01:34 AM
Pffft! What a confusing language! Imma stick to Perl.

You would make a better Perl programmer. Why? Your class names aren't long enough to be a true Java programmer.

Nope it will hit syntax.

document.add(new pet(...attributes..)) would work but it places each attribute separately in the arrayList
Isn't that what you want?

Kujiro
July 26th, 2011, 01:37 AM
Yup, it's a school science project. Nothing at the moment, just wanting to fight for a prize this year :D

TheMatrix
July 26th, 2011, 01:38 AM
Yup, it's a school science project. Nothing at the moment, just wanting to fight for a prize this year :D
If you told me exactly what you wanted it to do, I would gladly write you a Perl script. Of course, that'd be cheating....

Kujiro
July 26th, 2011, 01:45 AM
Yea it has to be done in java. :(
We are to create a pet hotel system,
With check in functions and check out functions.
Services I.e grooming, bathing and deticking

Checkin and out to be done with system time ,
No GUI or persistence API allowed.
Except fileWrite, scan and bufferedreader

I have the full work flow if you are interested .. :)

TheMatrix
July 26th, 2011, 01:54 AM
Yea it has to be done in java. :(
We are to create a pet hotel system,
With check in functions and check out functions.
Services I.e grooming, bathing and deticking

Checkin and out to be done with system time ,
No GUI or persistence API allowed.
Except fileWrite, scan and bufferedreader

I have the full work flow if you are interested .. :)
They are being language-ists(you know, like racists, but then for languages), then. :P
Sounds mighty complicated to do in Java...

darkwoon
July 26th, 2011, 01:56 AM
Any one is familiar with Java arrayList and array management ?

I have an object
Pet(String PetId, String ownerName, String description )
Which I need to be created in an arraylist.

pet[] createpet = new pet[3]
createpet[0] = new Pet(..attributes..)
Hence createpet itself will be an array.

But I need that array to be placed in an arrayList.
ArrayList<String> document = new ArrayList <String>();

How can it be done so which I may access the arraylist for the information?

Any form of help is apperciated

Cheers

Copying the array into the List:

List<Pet> pets = Arrays.asList(createpet);

Note that the list is of type List<Pet>, not List<String> as you wrote - this is because createpet is an array of Pets, not an array of Strings.

Can I get the full workflow? Sounds like some fun to code ;)

They are being language-ists(you know, like racists, but then for languages), then. :P
Sounds mighty complicated to do in Java...

Nah, quite easy, actually, but let's wait for the workflow to see how to do it in a fun way. I already think about the case where the pet escapes and the hotel needs to call packs of hunters ;)

Don't double post please, use the edit button - Kaius

Kujiro
July 26th, 2011, 02:14 AM
Nah, quite easy, actually, but let's wait for the workflow to see how to do it in a fun way. I already think about the case where the pet escapes and the hotel needs to call packs of hunters ;)

I'm at school now. I'll post up the work flow when I'm at home in about 4 hours :)

I can supply the class for the pet and the menu as well , that's the furthest I've got thus far

Don't double post please, use the edit button - Kaius

Kujiro
July 26th, 2011, 09:07 AM
http://i57.photobucket.com/albums/g207/kainniarun/classdiagram.jpg

This is the class diagram, I am done with the Pet class,
Pretty much thats it.

darkwoon
July 26th, 2011, 10:02 AM
yeah, thanks! Smells like CODING TIME again! XD

CaptainObvious
July 26th, 2011, 10:09 AM
Copying the array into the List:

List<Pet> pets = Arrays.asList(createpet);

yes, this is definitely more correct. why create the intermediary array at all, though?:

ArrayList<Pet> createdPets = new ArrayList<Pet>();
createdPets.add(new Pet(...));
etc.

Kujiro
July 26th, 2011, 10:25 AM
I need the Pet objects to be written into a file, no persistent api allowed only .txt files.

I would use excel if i could, but it seems that its not possible, object pet is set into an array, and i intend to use arraylist to manipulate the files,

You may ignore keyboard.class,
As we have decided to use bufferedreader for the input stream.

by creating list.add(new pet(...)),
The return value will be weird and gibberish, this is where im lost :(

darkwoon
July 26th, 2011, 11:17 AM
Ok, I have a decent classes structure now, but I'd need to know the work flow to be able to finish the application - in short, I'd need to know how the program works, and the menus I should display.

Kujiro
July 26th, 2011, 11:27 AM
I will send it to you via your email if its ok, the context is too long to actually be placed here.

darkwoon
July 26th, 2011, 11:31 AM
I will send it to you via your email if its ok, the context is too long to actually be placed here.

no problemo!

Kujiro
July 26th, 2011, 11:59 AM
I have dropped it to your gmail account :)

russjr08
July 26th, 2011, 02:33 PM
Hmm... the pet hotel thing reminds me of a movie... can't remember what it was called.

Anyways, I'm trying to learn Java, it can be very confusing. I wish you luck on your project and hope you win!

Kujiro
July 27th, 2011, 08:53 AM
I read a little in regards on object to file , requires serialization.
Of the object before passing the value into the file, any thoughts?

darkwoon
July 27th, 2011, 01:04 PM
I read a little in regards on object to file , requires serialization.
Of the object before passing the value into the file, any thoughts?

Yes, but you said earlier that no persistence api was allowed. And Serialization mechanisms are precisely that ;)

Anyway, just so you know how to perform Serialization in Java, you need to:

1. Make any class to serialize "implement Serializable":

public class stuff implements Serializable

2. You can store objects using a DataOutputStream:

DataOutputStream dout = new DataOutputStream(new FileOutputStream("output.dat"));
dout.writeObject(myobject);
dout.close();

(Or something like that, I'm typing from memory)

3. To retrieve a serialized object, use a DataInputStream:

DataInputStream din = new DataInputStream(new FileInputStream("output.dat"));
Object ob = din.readObject();
din.close();

If you want to learn more about it, I suggest you to have a look in Thinking in Java (http://www.mindview.net/Books/TIJ/) (a very good - and free - book about Java) at the chapter related to Serialization.

Now, what you're probably supposed to use to store your pet is manually storing each field of it. There are many different ways to do it - you can use the various InputStream/OutputStream subclasses, or the Reader/Writer ones, depending on the format you want to store your data. Here (http://www.javapractices.com/topic/TopicAction.do?Id=42) is a good explanation on how to read and write text files in Java - and again, Thinking in Java contains more extensive explanations and examples.

Hope this helps!

Edit: Another good information source are the Oracle tutorials - the one about files input/output is here (http://download.oracle.com/javase/tutorial/essential/io/). They contain a lot of commented small examples, so it is easier to understand what's going on.

Kujiro
July 27th, 2011, 01:11 PM
Thanks dark, working on them immediately! :)
You recieved the display diagrams?

darkwoon
July 27th, 2011, 01:15 PM
Thanks dark, working on them immediately! :)
You recieved the display diagrams?
Yeah, nearly finished coding my own version of the app, but I didn't work on it today (only Yesterday): today was about Perl-studying ;)

Kujiro
July 27th, 2011, 01:35 PM
Im alittle confused, what is considered a persistent API ?
Would writing the data into an excel spreadsheet be considered the use of persistent api?

darkwoon
July 27th, 2011, 01:43 PM
Im alittle confused, what is considered a persistent API ?
Would writing the data into an excel spreadsheet be considered the use of persistent api?
Nope, it wouldn't, but to do that, you'd need to use the Apache POI library (http://poi.apache.org/), which allows accessing and modifying excel documents from Java.

What is usually considered "persistence API" are APIs allowing transparent storage and retrieval of complete object instances. You'd want to ask your teacher what *he* actually meant to be sure, though.

Kujiro
July 27th, 2011, 01:45 PM
Ah i see, we dont have any official teacher for this sadly. Its a competition
But im at no where :(
The reason i took part is pretty complicated..

Solvez18
August 2nd, 2011, 05:28 AM
java is a bitch to programme, but good luck

Kujiro
August 5th, 2011, 12:09 AM
After studying the string object, I tried using delimiters and I guess using seralizable seems the only option out to add it to non gibberish text that can be reinvoked in 2nd runtime.
Unless we add SQL into it but I suppose it would just complicate the problem

TheMatrix
August 5th, 2011, 12:17 AM
Unless we add SQL into it but I suppose it would just complicate the problem
Are you sure you want to bring in any sort of database? That would certainly complicate stuff.

Kujiro
August 5th, 2011, 12:25 AM
Are you sure you want to bring in any sort of database? That would certainly complicate stuff.


I agree, so I guess serializing seems the best portable option .
1 more week left, yet so much on my mind.
*stoned*

TheMatrix
August 5th, 2011, 12:47 AM
Must it be completely in java?
If not, you just make a java program that writes a Perl script. And then run that.

Kujiro
August 5th, 2011, 01:16 AM
Must it be completely in java?
If not, you just make a java program that writes a Perl script. And then run that.

Yup, it has to be fully in java. :(
The syntax is blasting my brains dry.

darkwoon
August 5th, 2011, 10:50 AM
I'll finish tonight my version, so you can have a look at how it may work.

Edit: Ok, here is a simplistic version. It is not absolutely complete feature-wise (it lacks many input value checks and details in the checkout sums), but it should give you a decent idea on how you can solve various problems.

Note that it is only one of the many possible solutions - you can find lots of alternative ways to do each part (and it isn't a code of huge quality TBH, but I admit I hadn't more than few hours to give to it...).

Oh, and I asked around - looks like I was wrong, Serialization is usually not considered part of the Java Persistence API (that term actually relates to database-related stuff, etc). so OF COURSE you should use Serialization - it is MUCH easier to store your data that way ;).

Must it be completely in java?
If not, you just make a java program that writes a Perl script. And then run that.
I believe the project goal is to understand some of the Java programming language basics, not to get headaches just for the fun of it ;)