
October 2nd, 2012, 08:18 AM
|
|
Registered User
|
|
Join Date: Apr 2012
Posts: 3
Time spent in forums: 29 m 29 sec
Reputation Power: 0
|
|
|
Having trouble with delimiters in scanner
hi
i wanted to use the scanner to read a text file containing some source code and output it one statement at a time but am having trouble since sometimes there might be more than one statement on a line.
i wanted to use ; as a delimiter but i doesn't seem to work properly
the sample text am using is just :
cat;
dog;
fish;salmon;beef;
pork;boom;
i wanted to have each on a separate line ie
cat;
dog;
fish;
salmon;
beef;
pork;
boom;
Code:
public static void main(String[] args) {
try{
Scanner input = new Scanner(new File("code.txt")).useDelimiter("/;/g");
while(input.hasNext())
{
String line = input.next();
System.out.println(line);
}
}
catch(Exception ex){
System.out.println(ex);
}
}
but its just returning the file as written
can anybody help ?
|