scanner and while

According to http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.htmlA Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespacehow can i change that delimiter to tab or new linethanks
[267 byte] By [TheNewLeadera] at [2007-11-27 7:11:26]
# 1
Take a look at the useDelimiter() function. [url= http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html#useDelimiter(java.util.regex.Pattern)]Link[/url]Thanks.
DarumAa at 2007-7-12 19:03:04 > top of Java-index,Java Essentials,Java Programming...
# 2
> ...> Thanks.Err, thanks for what?
prometheuzza at 2007-7-12 19:03:04 > top of Java-index,Java Essentials,Java Programming...
# 3
thanks for the link ;-)but i want to use it with hasNext() how can i apply it
TheNewLeadera at 2007-7-12 19:03:04 > top of Java-index,Java Essentials,Java Programming...
# 4
> > ...> > Thanks.> > Err, thanks for what?Thanks for taking the time to read my post.This world must be in a terrible shape if I have to explain why I am being nice :-)
DarumAa at 2007-7-12 19:03:04 > top of Java-index,Java Essentials,Java Programming...
# 5

> > Err, thanks for what?

>

> Thanks for taking the time to read my post.

>

> This world must be in a terrible shape if I have to

> explain why I am being nice :-)

You don't have to explain yourself. I was just curious as to why you said it.

I just find it strange when you help someone out, you thank that person for letting him/her be helped.

Thanks.

; )

prometheuzza at 2007-7-12 19:03:04 > top of Java-index,Java Essentials,Java Programming...
# 6

> thanks for the link ;-)

> but i want to use it with

>

> hasNext() how can i apply it

hasNext() also takes a Pattern or a String based on which it returns true or false.

[url=http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html#hasNext(java.lang.String)]Link[/url]

DarumAa at 2007-7-12 19:03:04 > top of Java-index,Java Essentials,Java Programming...
# 7

Hi all

it is ok i find this

private static void readFile(String fileName) {

try {

Scanner scanner = new Scanner(new File(fileName));

scanner.useDelimiter

(System.getProperty("line.separator"));

while (scanner.hasNext()) {

System.out.println(scanner.next());

scanner.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

Thanks

TheNewLeadera at 2007-7-12 19:03:04 > top of Java-index,Java Essentials,Java Programming...
# 8

> > > Err, thanks for what?

> >

> > Thanks for taking the time to read my post.

> >

> > This world must be in a terrible shape if I have

> to

> > explain why I am being nice :-)

>

> You don't have to explain yourself. I was just

> curious as to why you said it.

> I just find it strange when you help someone out, you

> thank that person for letting him/her be helped.

>

> Thanks.

> ; )

You can just file it under "English is not my first or second or even third language" folder :-)

Thanks ;-)

DarumAa at 2007-7-12 19:03:04 > top of Java-index,Java Essentials,Java Programming...
# 9

> Hi all

> it is ok i find this

>

> ...

If it's lines you're interested in, then you can use the hasNextLine() and nextLine() methods:

private static void readFile(String fileName) {

try {

Scanner scanner = new Scanner(new File(fileName));

while (scanner.hasNextLine()) {

System.out.println(scanner.nextLine());

}

scanner.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

prometheuzza at 2007-7-12 19:03:04 > top of Java-index,Java Essentials,Java Programming...
# 10

> If it's lines you're interested in, then you can use

> the hasNextLine() and nextLine() methods:

In fact, you should definitely use this technique. The "line.separator" property just tells you what the underlying OS thinks a line separator should consist of. It tells you absolutely nothing about the file you're reading right now, which could use "\n", "\r\n", "\r", or all three in random sequence. hasNextLine() and nextLine() can identify line boundaries correctly in any case, as can other line-based API's like BufferedReader.readLine(). There's virtually never any reason to consult the "line.separator" system property.

uncle_alicea at 2007-7-12 19:03:04 > top of Java-index,Java Essentials,Java Programming...
# 11
Thanks prometheuzz and uncle_alice I'll try it and see the result
TheNewLeadera at 2007-7-12 19:03:04 > top of Java-index,Java Essentials,Java Programming...
# 12

Hi all

guess what\\when i try the method that i found

sc.useDelimiter

(System.getProperty("line.separator"))

it doesnot show the lines

when i use the while (sc.hasNextLine())

it write null in the file

and when i tried while (sc.nextLine() )

it gaves me incomptatible error

here the code

Scanner sc = null;

try {

sc = new Scanner(new BufferedReader(new FileReader("xanadu.txt")));

int number=0 ;

String str ="";

sc.useDelimiter

(System.getProperty("line.separator")) ;

while (sc.hasNext() )

//while (sc.hasNextLine())

//while (sc.nextLine() )

{

if (sc.hasNextInt())

{

number = sc.nextInt();

target+=number+"";

}

// .... and so............

else

{

str = sc.next();

target += convert(str) +" ";

}

}

}finally {

if (sc != null) {

sc.close();

}

TheNewLeadera at 2007-7-12 19:03:04 > top of Java-index,Java Essentials,Java Programming...
# 13

Hi all

guess what\\when i try the method that i found

sc.useDelimiter

(System.getProperty("line.separator"))

it doesnot show the lines

when i use the while (sc.hasNextLine())

it write null in the file

and when i tried while (sc.nextLine() )

it gaves me incomptatible error

here the code

Scanner sc = null;

try {

sc = new Scanner(new BufferedReader(new FileReader("xanadu.txt")));

int number=0 ;

String str ="";

sc.useDelimiter

(System.getProperty("line.separator")) ;

while (sc.hasNext() )

//while (sc.hasNextLine())

//while (sc.nextLine() )

{

if (sc.hasNextInt())

{

number = sc.nextInt();

target+=number+"";

}

// .... and so............

else

{

str = sc.next();

target += convert(str) +" ";

}

}

}finally {

if (sc != null) {

sc.close();

}

TheNewLeadera at 2007-7-12 19:03:04 > top of Java-index,Java Essentials,Java Programming...
# 14
> Hi all> guess what\\when i try the method that i found > > ...Did you try the code I posted in reply #9?
prometheuzza at 2007-7-12 19:03:04 > top of Java-index,Java Essentials,Java Programming...
# 15

You're using one Scanner for two different purposes: reading lines, and reading tokens within each line. Try using two Scanners instead: Scanner sc0 = new Scanner(new File("xanadu.txt"));

while (sc0.hasNextLine())

{

String line = sc0.nextLine();

Scanner sc1 = new Scanner(line);

while (sc1.hasNext())

{

// read and process the token

}

}

uncle_alicea at 2007-7-21 22:14:22 > top of Java-index,Java Essentials,Java Programming...
# 16
prometheuzz your suggestion was to use hasNextLine which i have commented in my repvoius post and said that it does not work
TheNewLeadera at 2007-7-21 22:14:22 > top of Java-index,Java Essentials,Java Programming...
# 17
> prometheuzz > > your suggestion was to use hasNextLine which i have> commented in my repvoius post and said that it does> not workYes it does; you're just using it wrong. Its correct use has been demonstrated twice now.
uncle_alicea at 2007-7-21 22:14:22 > top of Java-index,Java Essentials,Java Programming...