So here we go, hope this is an alright place to post this.
It's very much a personal project, just something I've set myself to try and further my knowledge of java. I only started studying it this year so I'm pretty useless so far.
Basically, the end aim is to develop a piece of code that will go through a file, copy all the lines that begin with a number and then insert them into a new file.
I've started off with a piece of code that will read through a file and display the letter "Y" when the line begins with an 'A' and display the letter "N" when it begins with anything else. The only way I know how to go through an entire file is to use:
while(MyFile.readLine()!=null)
However, I want it to be able to process lines that have nothing on them.
The code I've got so far is:
body.txt is simplyQuote:
import java.io.*;
class test1
{
public static void main(String[] args) throws IOException
{
FileReader file = new FileReader("body.txt");
BufferedReader MyFile = new BufferedReader(file);
String text = MyFile.readLine();
while(text!=null)
{
if(text.charAt(0)=='A')
{
System.out.println("Y");
}
else
{
System.out.println("N");
}
text = MyFile.readLine();
}
MyFile.close();
}
}
However, if there is an empty line in the middle of body.txt, it obviously won't work. Any help would be greatly appreciated!Quote:
A
B
B
A
B
B
A
A
A
Many thanks.
