Need help with reading files
For example, i have this in a file call junk and i want to read a the first part of a line and store it to line1 and then the numbers store on line2. this is what i have so far but it give me aException in thread "main" java.util.NoSuchElementException: No line found
when i try to run it. Some one give me some hints? Thanks
Hello,Java23453456456
while(sc.hasNext())
{
line1 = sc.nextLine().substring(0,19);
line2 = sc.nextLine().substring(19);
pw.print(line1+line2);
}
[623 byte] By [
bail_wa] at [2007-11-27 9:57:11]

> For example, i have this in a file call junk and i
> want to read a the first part of a line and store it
> to line1 and then the numbers store on line2. this is
> what i have so far but it give me aException in
> thread "main" java.util.NoSuchElementException: No
> line found
> when i try to run it. Some one give me some
> hints? Thanks
>
> Hello,Java23453456456
>
> while(sc.hasNext())
> {
> line1 = sc.nextLine().substring(0,19);
> line2 = sc.nextLine().substring(19);
> pw.print(line1+line2);
> }
You'll have to read one line at a time.
while(sc.hasNextLine()) {
String line = sc.nextLine();
String firstPart = ...
String secondPart = ...
}
Thanks for your hint, so i read the line then store the line into seperate parts
One more question, How do i skip line to read. For example if i have thisName John Davidhow do i start reading at John?
> Thanks for your hint, so i read the line
You read it one line at a time, that's the important thing. For example, if your text file has only one line in it (or it reaches the last line), then sc.hasNextLine()
will return true. But if you then try to read a line twice by doing String a = sc.nextLine();
String a = sc.nextLine();
an exception will be thrown, because there was only one line left.
Understand?
> then store the line into seperate parts
Yes.
> One more question, How do i skip line to read. For
> example if i have this
>
> Name
>
> John David
>
>
> how do i start reading at John?
Skip the first two lines by calling nextLine() twice (you need to check with the hasNextLine() if there are two lines, of course!).
i dont think there is a nextLine() method.
> i dont think there is a nextLine() method.Before you start claiming to know more than one of our regular posters, you might want to look at the API first.
> > i dont think there is a nextLine() method.
>
> Before you start claiming to know more than one of
> our regular posters, you might want to look at the
> API first.
Well, he wouldn't necessarily know that prometheuzz is a regular poster, and even if he did, everybody's fallible. Regardless of somebody else's experience or authority, however, it's ALWAYS wise to make sure you know what you're talking about before you post.
while(sc.hasNext())
{
sc.nextLine();
sc.nextLine();
line = sc.nextLine();
pw.println(line);
}
pw.close();
It gave me NoSuchElementException error ? may i know why?
methinks you have an error in your programming, not in java's implementation of Scanner.nextLine!
You are calling nextLine 3 times in your while loop, but only check for the presense of the line once at the top of the loop. This code is then eventually doomed to fail.
Well, actually it has a one in three chance of not failing. It only works if the number of lines in your textfile %3 == 0. Those odds aren't good.
Bottom line:
1) Prometheuzz was right.
2) Your code is buggy.
Message was edited by:
petes1234
> It gave me NoSuchElementException error ? may i know
> why?
Well, if you'd look at the name and docs for that exception, and if you'd read promethuzz's post carefully, and if you'd do a bit of thinking, you'd come to the conclusion that you're trying to read a line that doesn't exist, and that before you call nextLine, you should, as was suggested, check to see if there is in fact another line.
Really, it shouldn't be that hard to come up with a possible cause for that problem. Rather than throw your hands in the air and give up when you see an error, engage your brain and try to figure out what the message could possibly mean.
>
> Well, if you'd look at the name and docs for that
> exception, and if you'd read promethuzz's post
> carefully, and if you'd do a bit of thinking, you'd
> come to the conclusion that you're trying to read a
> line that doesn't exist, and that before you call
> nextLine, you should, as was suggested, check to see
> if there is in fact another line.
>
> Really, it shouldn't be that hard to come up with a
> possible cause for that problem. Rather than throw
> your hands in the air and give up when you see an
> error, engage your brain and try to figure out what
> the message could possibly mean.
maybe i am in the wrong section of the forum, but i remember i post this in the "New To Java" section.
> maybe i am in the wrong section of the forum, but i> remember i post this in the "New To Java" section.New to Java != new to thinking, reading, investigating.
> > maybe i am in the wrong section of the forum, but
> i
> > remember i post this in the "New To Java" section.
>
> New to Java != new to thinking, reading,
> investigating.
which mean i learn from what i am doing wrong. honestly if you not trying to help a newbie out why you even bother to come to this section.
> which mean i learn from what i am doing wrong.
> honestly if you not trying to help a newbie out why
> you even bother to come to this section.
All true. Now show us what you are made of. Think this through, and come up with a solution. You know you can do it. Then show it to us.
Good luck.
> > > maybe i am in the wrong section of the forum,
> but
> > i
> > > remember i post this in the "New To Java"
> section.
> >
> > New to Java != new to thinking, reading,
> > investigating.
>
> which mean i learn from what i am doing wrong.
Which I helpfully pointed out to you:
* You didn't read promethuzz's answer closely enough.
* You didn't read the docs.
* You didn't stop and think and puzzle it through hard enough.
> honestly if you not trying to help a newbie out why
> you even bother to come to this section.
If you think I'm not trying to help, you're mistaken. Just because you don't like what I say or how I say it doesn't mean I'm not being helpful.
jverda at 2007-7-21 23:12:36 >

> It gave me NoSuchElementException error ? may i know why?
The API documentation says exactly why you get this exception.
One thing you can do to get even more information is to catch the exception and print it out:try
{
while(sc.hasNext())
{
sc.nextLine();
sc.nextLine();
line = sc.nextLine();
pw.println(line);
}
pw.close();
}
catch(NoSuchElementException nsee)
{
nsee.printStackTrace();
}
It will print the "stack trace" which you read from the top down. The stack trace tells you exactly which line caused the problem. (As noted before, the problem being what the API documentation tells you the problem was.)
If you have thought about but can't understand the stack trace, post it. Since the stack trace refers to line numbers (which we can't see) say which line of your code is being refered to.
if(sc.hasNextLine())
{
sc.nextLine();
sc.nextLine();
while(sc.hasNext())
{
String line = sc.nextLine();
firstPart = line.substring(0,19);
secondPart = line.substring(19);
//fixName(firstPart);
pw.println(firstPart);
}
}
i got it. thanks guys.