Question

Im going to write a hotel reservation program for my final year at school. I am going to use a text file to have all the guests whose attributes include: Name, Surname, contact details etc. and I want to read these attributes back to the text fields they were entered in to but they will be the same fields and labels and everything but in a different page. I will be using a StringTokenizer when writing to the file so there will be e.g. a '#' separating each attribute.

Can someone please help with reading each attribute separately into separate text fields so e.g. the text file will look something like this:

Sam#Haden#samhaden@gmail.com#7562234#

and I want each attribute to be read to a separate text field.

Was it helpful?

Solution 3

You could use String.indexOf("#") to find a hash tag, then use that index to create a substring with just the first element, and a substring of the remaining elements. Then repeat the process until everything is split into the separate text fields.

OTHER TIPS

Use

split('#')

it would return an array... read from the different indexes to populate the fields.

you could consider to use Splitter from google-guava library like this:

import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;

Iterable<String> fieldset = Splitter.on("#").split("Sam#Haden#samhaden@gmail.com#7562234#");

for(final String s : fieldset) {
    System.out.println(s);
}

access fields by index:

Iterables.get(fieldset, 2); // returns "samhaden"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top