Hi, if i print out System.out.println("<"+s.trim()+">");
i get an output
<NameAttrib>
<YearAttrib>
<Year2Attrib>
then the next lines
<NameAttrib>
<YearAttrib>
<Year2Attrib>
which is how i want it (all 3 parts are printing seperetly so have been split correctly)
But now i need each of the 3 parts that are in s.trim() to be put into seperate strings like String name, String year, String year2;
I tried:
which outputswhile (scanner.hasNextLine()) { String theNextLine = scanner.nextLine(); // set the next line to a string String regex = "(\\(|\\)|\\/)"; String fields[] = theNextLine.split(regex); for(String s: fields){ String name = s.trim(); String year = s.trim(); String year2 = s.trim(); System.out.println("TITLE IS" + name); System.out.println("\nYEAR IS" + year); System.out.println("YEAR2 IS" + year2); }
TITLE IS NameAttrib
YEAR IS NameAttrib
YEAR2 IS NameAttrib
then goes onto:
TITLE IS YearAttrib
YEAR IS YearAttrib
YEAR2 is YearAttrib
then
TITLE IS Year2Attrib
YEAR IS Year2Attrib
YEAR2 is Year2Attrib
It is assigning the same data part to each String and then next time it goes round it assigns the next data part to each string...
I also tried another way
this is a little better and assigns the correct data part to each stringwhile (scanner.hasNextLine()) { String theNextLine = scanner.nextLine(); // set the next line to a string String regex = "(\\(|\\)|\\/)"; String fields[] = theNextLine.split(regex); for(String s: fields){ String name = fields[0]; String year = fields[1]; String year2 = fields[2]; System.out.println("TITLE IS" + name); System.out.println("\nYEAR IS" + year); System.out.println("YEAR2 IS" + year2); }
but for example if the datafile has example data:
aname (2012) 2012
it will print out aname, (2012) 2012 twice, so assiging the same data to the Strings twice.
I need it to simply do like the example directly above what i am saying now, but do each data only once as it is just repeated data.
Thanks