Quote:
|
while ((line = read.readLine()) != null)
|
I think you ment to write:
while ((line == read.readLine()) != null)
don't declare something with an = in a while, for, if, or do parameters.
also I would suggest using an Array List (import java.util.ArrayList) so you don't have an IndexOutOfBounds exception. Array Lists are great because you can keep adding to them by using the .add method, and because plain arrays are very primative.
I'm not completely sure what you are trying to use this for, but this is a very simple program I made that reads a text file and makes a copy of the same one. It might be of some use to you.You need to specify the name of the input text file, and name the output text file whatever you want.
Code:
/**
* Write a description of class LineNumberer here.
*
* @author (Mike Perugini)
* @version (11.3)
*/
import java.io.*;
import java.util.Scanner;
public class CopyFile
{
public static void main(String[] args) throws FileNotFoundException
{
FileReader reader = new FileReader("input.txt");
PrintWriter out = new PrintWriter("output.txt");
Scanner in = new Scanner(reader);
while (in.hasNextLine())
{
String line = in.nextLine();
out.println(line);
}
out.close();
}
}
This is an enhanced version of that program that simply reads a text file and adds line numbers to it. You need to specify the name of the input text file, and name the output text file whatever you want.
Code:
/**
* Write a description of class LineNumberer here.
*
* @author (Mike Perugini)
* @version (11_tutorial_1)
*/
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class LineNumberer
{
public static void main(String[] args) throws FileNotFoundException
{
Scanner console = new Scanner(System.in);
System.out.print("Input file: ");
String inputFileName = console.next();
System.out.print("Output file: ");
String outputFileName = console.next();
FileReader reader = new FileReader(inputFileName);
Scanner in = new Scanner(reader);
PrintWriter out = new PrintWriter(outputFileName);
int lineNumber = 1;
while (in.hasNextLine())
{
String line = in.nextLine();
out.println("/* " + lineNumber + "*/" + line);
lineNumber++;
}
out.close();
}
}