• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

jon bones

hot hot hanuman-on-man action
been really enjoying www.codeschool.com

trying to get my ruby on rails chops up, and the site's been great for doing so. i forgot what it's like to be passionate about learning... my skillset's been so stagnant for so long.

my "to learn" self-study list:
-ruby on rails
-sql
-agile / scrum
 

tokkun

Member
This is a crash bug waiting to happen. A more robust function would look like this:

Code:
void destroy_matrix(matrix m) {
  if(m && [B]m.element != NULL[/B]) // this check prevents a crash if you accidentally try to destroy the same matrix twice
  {
    free(m.element);
    m.element = NULL;
  }
}

free(NULL) is safe, so the "m.element != NULL" is unnecessary here.

" if(m) " also doesn't make sense since m is a struct being passed by value, not by pointer.
 
I love* the app I am on. Today, I come across bugs that are semi-caused by this conditional, which happens to be expressed in multiple places in the code base:

Code:
if (someObject.SomeProperty != null)
{
	// doin' thangs
}

The problem is that the getter for the property (C#) ensures that it never returns null.

Code:
private Foo someField;
public Foo SomeProperty 
{
	get { return (someField == null) ? someField = new Foo() : someField; }
	set { someField = value; }
}

And then the code blindly relies on further properties of .SomeProperty to be populated in some orderly fashion, which they might not be, and then hilarity** ensues.



*hate
**pain
 

usea

Member
What is the conditional's part in the bug? If it's always true, then it's meaningless right?

Oh, but I guess checking for null instantiates the field.
 
What is the conditional's part in the bug? If it's always true, then it's meaningless right?

Oh, but I guess checking for null instantiates the field.

Correct, checking it creates it. The code goes on to assume something was there, when nothing was until it checked. SomeProperty is supposed to contain values the code can rely upon, when clearly here, the code really shouldn't.

This situation, as with this entire application, is a comedy of errors and I won't go into specifics of what happens inside the braces, but at the top layer of the onion is this side-effect inducing, non-null observing conditional.
 

Godslay

Banned
Anyone using VS 12? I've been going back and forth between 10 and 12, and 10 feels better. Maybe it's just what I'm used to at this point.
 

usea

Member
Correct, checking it creates it. The code goes on to assume something was there, when nothing was until it checked. SomeProperty is supposed to contain values the code can rely upon, when clearly here, the code really shouldn't.

This situation, as with this entire application, is a comedy of errors and I won't go into specifics of what happens inside the braces, but at the top layer of the onion is this side-effect inducing, non-null observing conditional.
Yeah, that's pretty sneaky.

We had a bug where an IEnumerable collection passed to an uploader was being lazy-loaded. And when it finally came time to upload the items, the list was suddenly empty. Similar bug with the conditional check, but with .Any() instead. I guess if I had resharper installed it would have warned me.
 
Did anyone know that (-2147483648 > 0) == true in C++ even with 32-bit integers? I've been working with C++ for years and yet it still has weird corner cases to surprise me.

Assuming (without going into the stackoverflow thread) that the number is really 2146483648 but overflows to -2146483648 so it turns to -(-2146483648), which is therefore > 0;

[testing my knowledge here]

Also incidentally I have to write a fairly basic command line shell, and I feel like being dumb about the name. Definitely thinking of calling it the 'seashell'.
 

Slavik81

Member
Assuming (without going into the stackoverflow thread) that the number is really 2146483648 but overflows to -2146483648 so it turns to -(-2146483648), which is therefore > 0;

[testing my knowledge here]
Pretty much. Basically: C++ does not support negative integer literals. They are all positive integers, which are then negated.

The behaviour you're describing is exactly what's happening. Though, overflow for signed int is undefined behaviour. So, perhaps I should have said "(-2147483648> 0) == true for some compilers".
 

gblues

Banned
free(NULL) is safe, so the "m.element != NULL" is unnecessary here.

" if(m) " also doesn't make sense since m is a struct being passed by value, not by pointer.

Good catch. I was going to say that the point of checking for NULL was to avoid a crash (calling free() on a hunk that's already freed WILL cause a crash with certain implementations), but the code would only change the object on the stack, not the original struct element.

Version 2:
Code:
void destroy_matrix(matrix *m) {
  if(m && m->element != NULL) // this check prevents a crash if you accidentally try to destroy the same matrix twice
  {
    free(m->element);
    m->element = NULL;
  }
}

Of course you would also need to change the call to destroy_matrix() appropriately.
 
I have been meaning to vent about this a bit since someone recommended not to comment their code but I was banned so...

The code in question was just some function that calculated the area and sure it was clear what the function was doing but the point is just getting on the habit of actually commenting the code. I know that in some (most?) schools profs tell people to comment every line or something which is obviously excessive but it's still better than nothing. Because they thing is that most programmers are awful and all programming languages are awful. I should know, because I am the shittiest of them all working on the shittiest of them all. But at least I try to put some effort in someone other than me actually understanding what I have done. The guy I referred in the first sentence recommended writing clean and easily understandable code so comments become less of necessary which is ABSOLUTELY true. Only thing is that this will fail because of what I said previously: everything is awful.

It doesn't matter if you write the best and cleanest code in the world when you get to work on someone elses awful code full of hacks, exceptions and I-don't-know-why-it-works-but-it-sort-of-does and then the comments become kinda crucial. Nothing is worse than cruising around thousands of lines of codes and still not having any clue what the fuck it is actually doing.

tl;dr. No comments inside the code, comments describind the methods, modules and classes are ok.

Pretty much this, but please if you don't know why it works, please at least mention that what it is supposed to do


edit: for the record: My current job description is to bugfix, update and upgrade corporate projects done by students that might or might have any sort of programming experience (I am last year student myself too)
 
I have been meaning to vent about this a bit since someone recommended not to comment their code but I was banned so...

The code in question was just some function that calculated the area and sure it was clear what the function was doing but the point is just getting on the habit of actually commenting the code. I know that in some (most?) schools profs tell people to comment every line or something which is obviously excessive but it's still better than nothing. Because they thing is that most programmers are awful and all programming languages are awful. I should know, because I am the shittiest of them all working on the shittiest of them all. But at least I try to put some effort in someone other than me actually understanding what I have done. The guy I referred in the first sentence recommended writing clean and easily understandable code so comments become less of necessary which is ABSOLUTELY true. Only thing is that this will fail because of what I said previously: everything is awful.

It doesn't matter if you write the best and cleanest code in the world when you get to work on someone elses awful code full of hacks, exceptions and I-don't-know-why-it-works-but-it-sort-of-does and then the comments become kinda crucial. Nothing is worse than cruising around thousands of lines of codes and still not having any clue what the fuck it is actually doing.

In that case comments won't help either. Hell if can't trust the code how are you gonna trust the comments. The code could be doing X but the comments saying it does Z. Comments inside methods are useless, clean code is the only acceptable solution. Comments above each method as some sort of doc is fine though.

tl;dr. No comments inside the code, comments describind the methods, modules and classes are ok.
 

Korey

Member
I'm very familiar with HTML/CSS

I want to get into programming to make iPhone apps (and eventually also website apps in the future).

Do you think it'd be better for me to start with learning the basics of programming in Javascript?

Or should I just jump right into learning Objective-C or something like that? Or something else?
 

Jokab

Member
I'm very familiar with HTML/CSS

I want to get into programming to make iPhone apps (and eventually also website apps in the future).

Do you think it'd be better for me to start with learning the basics of programming in Javascript?

Or should I just jump right into learning Objective-C or something like that? Or something else?
Speaking from my own (very limited) experience, Java is a very good entry-level language to OOP. Javascript might suit your web background better though since it's way easier to test your creations that way.
 
Anyone using VS 12? I've been going back and forth between 10 and 12, and 10 feels better. Maybe it's just what I'm used to at this point.

I use 2012 exclusively now. Unless I need to use an earlier version because of a project/client.

The only thing that really pissed me off was the stupid default color theme. Luckily that is fixable.
 

usea

Member
In that case comments won't help either. Hell if can't trust the code how are you gonna trust the comments. The code could be doing X but the comments saying it does Z. Comments inside methods are useless, clean code is the only acceptable solution. Comments above each method as some sort of doc is fine though.

tl;dr. No comments inside the code, comments describind the methods, modules and classes are ok.
I agree with this. Commenting every line is not better than nothing. Comments don't tell you what code does; they tell you what the author thought it was doing. If I can't tell what the code does, I'm certainly not going to blindly trust the comments that the same person wrote.

Sometimes I will comment a line of code if I think that a reasonable person reading the code would wonder about some particular bit of it. But usually this is a sign that you need to change something anyway.
 

gcubed

Member
so i've gotten a basic understanding of PERL for my network scripting at work... my field is starting to rapidly move towards SDN, which is either Java or Python. I know very little C and no Java whatsoever... any good Java or Python primer? I need to look over the thread as i think its been asked already and i'm probably being lazy
 

Godslay

Banned
Alas, I've been developing on VS2008 since... well, 2008.

Yes, 2008 was great. 10 feels good though and the majority of my projects were in 10.

10 just feels better, but mostly because it doesn't have that metro shit going on with it.

I use 2012 exclusively now. Unless I need to use an earlier version because of a project/client.

The only thing that really pissed me off was the stupid default color theme. Luckily that is fixable.

I discovered the theme editor plugin, but it still feels very metroish. I'll pretty much being working in it exclusively, so I guess I better get used to it.

so i've gotten a basic understanding of PERL for my network scripting at work... my field is starting to rapidly move towards SDN, which is either Java or Python. I know very little C and no Java whatsoever... any good Java or Python primer? I need to look over the thread as i think its been asked already and i'm probably being lazy

This will get you up and running in Python fairly quickly. It's just the basics. Look for the Python track.

http://www.codecademy.com/

I can't find the post, but I did post a set of books for the beginning programming in Java in this thead.

There is also a youtube set of videos here that will get you going, there a little old but it goes over foundation type stuff so you should be okay. Good luck!

http://www.youtube.com/course?list=EC4BBB74C7D2A1049C
 

Septimius

Junior Member
Speaking from my own (very limited) experience, Java is a very good entry-level language to OOP. Javascript might suit your web background better though since it's way easier to test your creations that way.

Oh god. Stay away from javascript like it's the plague. It's the stupidest wide-spread language today.
 
Oh god. Stay away from javascript like it's the plague. It's the stupidest wide-spread language today.

JavaScript has issues yes but it's a criminally underrated language in terms of its actual capability. There's a really damaging copy paste culture that goes along with it and most programmers who use it don't know what the hell they're doing but there's a very solid core there.

PHP, now there's a shit language.
 

Septimius

Junior Member
It does some weird stuff, but it is useful, especially in webdev.

I'm a web developer, and I think javascript sucks donkey butts. It is just so far removed from any other syntax that no one is able to use it properly. Not only is it extremely hard to do properly yourself, because the thing combines strange features from low and high level languages, and the separation is lost on... everyone?

It's a nightmare to debug, it is incredibly hard to do structured, and doing structured OOP stuff requires education of anyone that already knows CSS, HTML and C#. So most people end up writing crappy code, and you know you'll just love debugging that later. If you do know what you're doing, chances are, no one else does.

Yes, I agree that it's underrated, but the amount of times it's been abused makes it quite the opposite. It's a shame, because it's the most common way to do client-side updates, and it always just leaves a trail of crap.
 

Godslay

Banned
I'm a web developer, and I think javascript sucks donkey butts. It is just so far removed from any other syntax that no one is able to use it properly. Not only is it extremely hard to do properly yourself, because the thing combines strange features from low and high level languages, and the separation is lost on... everyone?

It's a nightmare to debug, it is incredibly hard to do structured, and doing structured OOP stuff requires education of anyone that already knows CSS, HTML and C#. So most people end up writing crappy code, and you know you'll just love debugging that later. If you do know what you're doing, chances are, no one else does.

Yes, I agree that it's underrated, but the amount of times it's been abused makes it quite the opposite. It's a shame, because it's the most common way to do client-side updates, and it always just leaves a trail of crap.

Web dev here as well. Although I just recently moved from a more of a desktop application environment to web dev, so I'm still fresh. Anyways, bro fist.

Besides that I find that the syntax isn't too terrible. It certainly feels like something out of the C family to me anyways. I enjoy working with it, but the other people I work with seem to have a good handle on it, so it eases the transition. I guess YMMV?
 

gcubed

Member
This will get you up and running in Python fairly quickly. It's just the basics. Look for the Python track.

http://www.codecademy.com/

I can't find the post, but I did post a set of books for the beginning programming in Java in this thead.

There is also a youtube set of videos here that will get you going, there a little old but it goes over foundation type stuff so you should be okay. Good luck!

http://www.youtube.com/course?list=EC4BBB74C7D2A1049C

thanks, i saw that code academy link a bit earlier and have been browsing, will have to partition off some time to get up and running. I never like programming in college, but now that I have an actual use for it in my job its making it easier for me to learn
 

squidyj

Member
I just got thoroughly ruined. MIcrosoft was holding a contest at my school so I decided to show up even though i've been up for forever. Anyways I tanked it due in part to overthinking but as usually happens I realized the solutions on my way out the door. I thought I'd pose the questions to GAF to see what you guys come up with.

1. Swap the values of two integers without using a temporary variable at all.
2. Given a keyword and a list of words find the longest word in the list of words that can be made using only letters from the keyword (if your keyword is abcd you could make abc or abd, etc). You can use each letter only once for each time it appears in the keyword
3. Given a tic tac toe board in some state (could be the very beginning of the game or halfway through or whenever) with one player and one opponent determine whether the player will win, lose or tie. Furthermore you must define the data structure you use for the tic-tac-toe board / game state.
 
Web dev here as well. Although I just recently moved from a more of a desktop application environment to web dev, so I'm still fresh. Anyways, bro fist.

Besides that I find that the syntax isn't too terrible. It certainly feels like something out of the C family to me anyways. I enjoy working with it, but the other people I work with seem to have a good handle on it, so it eases the transition. I guess YMMV?
Complaining about Javascript as a language feels a little like complaining about C++ to me. It's very flexible, and if you want to use it poorly it will make no effort to stop you. Soon you will be missing both feet, and your instinct will be to blame the language, but that gun didn't pull its own trigger!

The most useful thing is to appreciate jQuery and the like, because while they might feel like unnecessary cruft at first, what they really do well is abstracting away the stupid browser hacks for you. This basically allows you to pretend IE < 9 doesn't exist, which is awesome.

Also, closures.

Also, unit test with qunit and phantomjs for the love of god.
 

Godslay

Banned
thanks, i saw that code academy link a bit earlier and have been browsing, will have to partition off some time to get up and running. I never like programming in college, but now that I have an actual use for it in my job its making it easier for me to learn

Np. The CodeAcademy stuff is easy to get into. Short little segments that introduce a little at a time. The Python track is one of the best imo.

Also, closures.

Also, unit test with qunit and phantomjs for the love of god.

I'm going to have to look into those. I've seen closures around, just never really spent the time to learn them.
 
1. Swap the values of two integers without using a temporary variable at all.

Something like this, I believe. Might be a better way.

Code:
int a = 100;
int b = 257;

a ^= b;
b ^= a;
a ^= b;

a.Dump();
b.Dump();

Questions 2 and 3 remind me why I need to brush up on my algorithm/data structure skills. And by brush up, I mean find some. Wasn't a CS major, so I'm sure I'd come up with code that gets the job done but not as efficiently as it could be.
 

leroidys

Member
I just got thoroughly ruined. MIcrosoft was holding a contest at my school so I decided to show up even though i've been up for forever. Anyways I tanked it due in part to overthinking but as usually happens I realized the solutions on my way out the door. I thought I'd pose the questions to GAF to see what you guys come up with.

1. Swap the values of two integers without using a temporary variable at all.
2. Given a keyword and a list of words find the longest word in the list of words that can be made using only letters from the keyword (if your keyword is abcd you could make abc or abd, etc). You can use each letter only once for each time it appears in the keyword
3. Given a tic tac toe board in some state (could be the very beginning of the game or halfway through or whenever) with one player and one opponent determine whether the player will win, lose or tie. Furthermore you must define the data structure you use for the tic-tac-toe board / game state.

1. I think this would work
Code:
int a = whatever
int b = whatever

a = a + b
b = a - b
a = a -b

2. Really inefficient but something like getting a word, checking if its = or < length of the keyword, and checking each letter to see if its in the keyword, removing the letter if its in the keyword. Have a variable that stores the current largest word, and replace it if the new one is larger.

3. Have a recursive function that tries every single move after the initial state. Just use a 3x3 matrix, fill it with 0 and 1 or something for x and o.

Maybe I don't understand the question in number 3 though, because it seems like you could arrive at a win lose or tie from the same state.
 

gblues

Banned
I just got thoroughly ruined. MIcrosoft was holding a contest at my school so I decided to show up even though i've been up for forever. Anyways I tanked it due in part to overthinking but as usually happens I realized the solutions on my way out the door. I thought I'd pose the questions to GAF to see what you guys come up with.

1. Swap the values of two integers without using a temporary variable at all.

Ah a classic Microsoft interview question. The solution involves fancy use of XOR:

http://en.wikipedia.org/wiki/XOR_swap_algorithm

Code:
void xorSwap (int *x, int *y) {
     if (x != y) {
         *x ^= *y;
         *y ^= *x;
         *x ^= *y;
     }
 }
 

mannerbot

Member
thanks, i saw that code academy link a bit earlier and have been browsing, will have to partition off some time to get up and running. I never like programming in college, but now that I have an actual use for it in my job its making it easier for me to learn

Codecademy is garbage. Check out edX's 6.00x or Udacity's CS101. The edX one started this week and has weekly assignments, whereas Udacity is self-paced (designed to run 7 weeks for a total beginner, can probably be done pretty quickly since you're comfortable another language).
 

Exuro

Member
Good catch. I was going to say that the point of checking for NULL was to avoid a crash (calling free() on a hunk that's already freed WILL cause a crash with certain implementations), but the code would only change the object on the stack, not the original struct element.

Version 2:
Code:
void destroy_matrix(matrix *m) {
  if(m && m->element != NULL) // this check prevents a crash if you accidentally try to destroy the same matrix twice
  {
    free(m->element);
    m->element = NULL;
  }
}

Of course you would also need to change the call to destroy_matrix() appropriately.
So the only case that this would happen is when calling the function twice for the same matrix?

Thanks for the help, I've fixed it up a bit and had a few more functions added. Still trying to understand pointers and references to the T so I dont have to question which to use in arguments.
 

SRG01

Member
I just got thoroughly ruined. MIcrosoft was holding a contest at my school so I decided to show up even though i've been up for forever. Anyways I tanked it due in part to overthinking but as usually happens I realized the solutions on my way out the door. I thought I'd pose the questions to GAF to see what you guys come up with.

1. Swap the values of two integers without using a temporary variable at all.
2. Given a keyword and a list of words find the longest word in the list of words that can be made using only letters from the keyword (if your keyword is abcd you could make abc or abd, etc). You can use each letter only once for each time it appears in the keyword
3. Given a tic tac toe board in some state (could be the very beginning of the game or halfway through or whenever) with one player and one opponent determine whether the player will win, lose or tie. Furthermore you must define the data structure you use for the tic-tac-toe board / game state.

You can use bitshift for #1 if assembly is allowed :)
 

hateradio

The Most Dangerous Yes Man
Good catch. I was going to say that the point of checking for NULL was to avoid a crash (calling free() on a hunk that's already freed WILL cause a crash with certain implementations), but the code would only change the object on the stack, not the original struct element.

Version 2:
Code:
void destroy_matrix(matrix *m) {
  if(m && m->element != NULL) // this check prevents a crash if you accidentally try to destroy the same matrix twice
  {
    free(m->element);
    m->element = NULL;
  }
}
Of course you would also need to change the call to destroy_matrix() appropriately.
This is why I don't like crotchety languages. :p

Is there a way you can wrap it in a try/catch block instead? Or is that impossible because you're trying to free it, which would cause the crash, without being able to catch the error?


Just for my own curiosity, why can't you set m->element = NULL, then free(m->element)?
 

Slavik81

Member
This is why I don't like crotchety languages. :p

Is there a way you can wrap it in a try/catch block instead? Or is that impossible because you're trying to free it, which would cause the crash, without being able to catch the error?


Just for my own curiosity, why can't you set m->element = NULL, then free(m->element)?
You cannot use try/catch because c does not have exceptions.

As to your second question,
Code:
m->element = NULL;
free(m->element);

is no different from:
Code:
m->element = NULL;
free(NULL);

Pointers are values, not objects with identity. One null pointer is indistinguishable from any other of the same type. Once you've assigned null to element, you're erased the only information that could be used to free the allocated memory.
 
I just got thoroughly ruined. MIcrosoft was holding a contest at my school so I decided to show up even though i've been up for forever. Anyways I tanked it due in part to overthinking but as usually happens I realized the solutions on my way out the door. I thought I'd pose the questions to GAF to see what you guys come up with.

1. Swap the values of two integers without using a temporary variable at all.
2. Given a keyword and a list of words find the longest word in the list of words that can be made using only letters from the keyword (if your keyword is abcd you could make abc or abd, etc). You can use each letter only once for each time it appears in the keyword
3. Given a tic tac toe board in some state (could be the very beginning of the game or halfway through or whenever) with one player and one opponent determine whether the player will win, lose or tie. Furthermore you must define the data structure you use for the tic-tac-toe board / game state.
The first question appeared on my introductory computer science midterm haha (yeah, XORing is the way to go).
 
I just got thoroughly ruined. MIcrosoft was holding a contest at my school so I decided to show up even though i've been up for forever. Anyways I tanked it due in part to overthinking but as usually happens I realized the solutions on my way out the door. I thought I'd pose the questions to GAF to see what you guys come up with.

1. Swap the values of two integers without using a temporary variable at all.
2. Given a keyword and a list of words find the longest word in the list of words that can be made using only letters from the keyword (if your keyword is abcd you could make abc or abd, etc). You can use each letter only once for each time it appears in the keyword
3. Given a tic tac toe board in some state (could be the very beginning of the game or halfway through or whenever) with one player and one opponent determine whether the player will win, lose or tie. Furthermore you must define the data structure you use for the tic-tac-toe board / game state.

My auto reactions after 2 mins of thinking:

1) Like others have said, xor or use a b + - swap.
2) Sort the keyword lexicographically, sort each element of the list lexicographically. For each word in the list, start at the beginning of the keyword. +1 index in the keyword if the letter does not match, +1 in both if it does. If you get to the end of the keyword before the word you can throw it out. Store the largest.
3) I would probably just use an array of some kind for board state and minimax to the end. There's probably a nicer way to handle it but I just love trees. Trees trees trees.

Fake edit: I've been learning Clojure recently so I'm working on these random problems just for fun, here's no. 2 solved with clojure (there is probably a much better way to do it)

Code:
(def kword "abcd")
(def words ["abc" "dog" "cat" "ddbca" "dbca"] )
(defn word-match
  [k word]
  (let [[k & kx] (sort k) [w & wx :as word] (sort word)]
  (cond 
    (nil? w) true
    (nil? k) false 
    (= w k) (word-match kx wx) 
    :else (word-match kx word))))

(reduce #(if (< (count %1) (count %2)) %2 %1) (filter #(word-match kword %) words))

Output:

Code:
"dbca"
 

usea

Member
3) I would probably just use an array of some kind for board state and minimax to the end. There's probably a nicer way to handle it but I just love trees. Trees trees trees.
I'm sure there is a more specialized solution too, but in my experience minimax works really well for tic-tac-toe, although if the state is a blank board it will take a while (a few seconds) to reach a solution. Just 1 move in though, and the time drops to well under a second.

Also the question as posed is incomplete because it doesn't say the opponent plays optimally. Without information about how the opponent plays, there needs to be a fourth possible answer which is "cannot determine" for all those states where there are multiple ways the game can end.
 
I'm sure there is a more specialized solution too, but in my experience minimax works really well for tic-tac-toe, although if the state is a blank board it will take a while (a few seconds) to reach a solution. Just 1 move in though, and the time drops to well under a second.

Also the question as posed is incomplete because it doesn't say the opponent plays optimally. Without information about how the opponent plays, there needs to be a fourth possible answer which is "cannot determine" for all those states where there are multiple ways the game can end.

Yeah I would have assumed optimal play from the opponent but that is something that needs to be considered, especially if it's meant to be a gotcha question in that sense. Tic tac toe in particular definitely only has a small amount of rules you can apply to determine a winner from a given state, especially since you can cut out 75% of the state space by mirroring everything to one corner. (And even if you were to do MiniMax or something there's tons of shortcuts, like a blank board is always a draw etc.)
 

smiggyRT

Member
Java question. If I have a class, say 'Person' with variables like name, age, postcode etc and i store multiple Persons in an array what would be the best way of storing that information in a text file then being able to take the text file and pass the Persons back into the array? Sorry if that wasn't entirely clear.
 

Naka

Member
Java question. If I have a class, say 'Person' with variables like name, age, postcode etc and i store multiple Persons in an array what would be the best way of storing that information in a text file then being able to take the text file and pass the Persons back into the array? Sorry if that wasn't entirely clear.

If you are doing a flat text file then use some sort of deliminator like comma or tab and have each person be represented on each line. Or you could get more complex and use XML or JSON.
 

usea

Member
Java question. If I have a class, say 'Person' with variables like name, age, postcode etc and i store multiple Persons in an array what would be the best way of storing that information in a text file then being able to take the text file and pass the Persons back into the array? Sorry if that wasn't entirely clear.
Serialization. I'm not sure what the most common way of doing this in Java is. I would just google Java serialization.

In C# I usually use Json for this. It appears there's a popular library from google for this in Java: http://code.google.com/p/google-gson/
 
Java question. If I have a class, say 'Person' with variables like name, age, postcode etc and i store multiple Persons in an array what would be the best way of storing that information in a text file then being able to take the text file and pass the Persons back into the array? Sorry if that wasn't entirely clear.

Use a serializer like YAML, JSON, etc..
 

smiggyRT

Member
If you are doing a flat text file then use some sort of deliminator like comma or tab and have each person be represented on each line. Or you could get more complex and use XML or JSON.

Serialization. I'm not sure what the most common way of doing this in Java is. I would just google Java serialization.

In C# I usually use Json for this. It appears there's a popular library from google for this in Java: http://code.google.com/p/google-gson/

Use a serializer like YAML, JSON, etc..

Thanks guys!
 
Top Bottom