0

I have this assigment where I have to make a simple shopping interface, in which the users can register, log in and buy. As it's a simple one I'm not bothering to even think of security issues or anything, so when a new user registers I just take the data of each textbox(name,email,country, etc)add it to an arraylist, and then store that into a txt file, with one user per line. When you open up the txt file each line looks like this:

[Name,email,password,Country,State,adress,birthdate]

now I'm trying to do the log in method, in wich I just need to compare the inputs on the textboxes to the defined indexes on each the arraylist of each user(user[1] for the email and user[2] for the password), but when I read the file line from line, it just makes the whole arraylist into a big string like this:

"[Name,email,password,Country,State,adress,birthdate]"

is there a way to read each line as an arraylist?

btw, I'm using the BufferReader and FileReader libraries

(english is not my native tongue so, if anything is not clear, please ask so I can try to clarify it)

azurefrog
  • 10,785
  • 7
  • 42
  • 56
Twhite1195
  • 351
  • 6
  • 17
  • Might I suggest that an `ArrayList` might not be the best idea. It would be better to create a custom class, write `toString()` and `parseString()` functions and use that to do your data storage and retrieval? Shouldn't really add any complexity to your code, really. – Tripp Kinetics Oct 13 '15 at 21:09
  • First use String methods to remove the end and beginning brackets, then call String's `split(...)` method to split the String up. – Hovercraft Full Of Eels Oct 13 '15 at 21:09

1 Answers1

0

So far so good!

Suppose that this is the string:

String myLine = "[Name,email,password,Country,State,adress,birthdate]";

The first thing you want to do is take out the brackets [ and ]:

myLine = myLine.substring(1, myLine.length() - 2);

Now we got this:

Name,email,password,Country,State,adress,birthdate

So we gotta split the data. Observe how the separator here are commas ,. You can split the string this way:

Strings[] fragments = myLine.split(",");

The array fragments contains each piece of data. For example, the name would be in fragments[0], and the address in fragments[5].

That's the basic principle here. However, there are some obvious problems. For example, I think it's quite possible that an user's password contains a comma. This would break our above method.

There are many ways to fight this, but the simplest is probably using a separator that is not a comma.

JVon
  • 684
  • 4
  • 10
  • Wow, thanks this really fixed the issue I've had now. Very well explained, this is why this community is so awesome! – Twhite1195 Oct 13 '15 at 21:31