> > 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.
; )
> 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]
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
> > > 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 ;-)
> 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();
}
}
> 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.
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();
}
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();
}
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
}
}