Fractal Softworks Forum

Please login or register.

Login with username, password and session length

Author Topic: Need some java help! ( again :P ) (again :P :P)  (Read 1632 times)

sdmike1

  • Admiral
  • *****
  • Posts: 820
  • Dyslexics of the world, untie!
    • View Profile
Need some java help! ( again :P ) (again :P :P)
« on: December 06, 2012, 07:41:01 AM »

My friends and i are going to make a basic messing server and client for a semester project in our AP computer science class, we believe we know how we are going to handle sending the information but we have absolutely now idea how to receive the information. :-[  Any help would be nice, even if it is just pointing me towards the right class in the comm package :)
« Last Edit: January 14, 2013, 07:40:43 AM by sdmike1 »
Logged

Thaago

  • Global Moderator
  • Admiral
  • *****
  • Posts: 7209
  • Harpoon Affectionado
    • View Profile
Re: Need some java help! ( again :P )
« Reply #1 on: December 06, 2012, 10:00:02 AM »

[Edit] Whoops - I'm responding to your previous post!

I'm a little confused - are you writing to file or are you having a user input a new set of Strings? Or both (user inputs until done, and then it writes)? I think both...

I've never done Java file io before, but going by this documentation: http://docs.oracle.com/javase/1.4.2/docs/api/java/io/ObjectOutputStream.html

Code
ObjectOutputStream ostream = new ObjectOutputStream(new FileOutputStream("test.dat"));
ostream.writeObject(object);
ostream.flush();
ostream.close();

Should be all you need to write an object to a file.

I don't think you should be using arrays (things with []) for input - they have a constant length that must be set at declaration. See: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html  If your user wants to input more objects than that, you either get an out of bounds problem or you have to reinitialize a larger array then copy the old contents into it (slow and ugly). Use Lists instead - they are variable length. They can be converted to arrays at the end if you really want that. I think this should work for your NAME_ENTRY case:

Code
List<String> entryList = new LinkedList(); // to initialize

name = input.nextLine();
while (! name.equals(DONE)){
    entryList.add(name);
    name = input.nextLine();
}

ObjectOutputStream ostream = new ObjectOutputStream(new FileOutputStream("test.dat"));
ostream.writeObject(entryList.toArray());
ostream.flush();
ostream.close();



I suggest reading through the Java tutorials like the ones I linked... they really helped me to understand what was going on.
Logged

sdmike1

  • Admiral
  • *****
  • Posts: 820
  • Dyslexics of the world, untie!
    • View Profile
Re: Need some java help! ( again :P )
« Reply #2 on: December 06, 2012, 06:23:56 PM »

Ah, i will have to try that when i get to school tomorrow, don't have JDK on the laptop at home and the admomistrator wont install it...

And yes the user enter's until done and then it writes the object of the array to the file, i before i knew that you could write objects to files i was trying to make this very messy, very glitchy, ad hoc array system, it didn't work very well at all... lol
« Last Edit: December 07, 2012, 07:14:54 AM by sdmike1 »
Logged