So far, this is what I have. The problem I'm having isn't getting one line, it's getting several lines and putting them into an array.
import java.io.*;
public class Confused
{
// assign variables used throughout the program
private static BufferedWriter BW, BW_File;
public static String[] name1 = new String[7];
public static String[] name2 = new String[7];
public static String[] name3 = new String[7];
public static String[] name4 = new String[7];
public static String[] name5 = new String[7];
public static String[] name6 = new String[7];
public static String[] name7 = new String[7];
public static String[] name8 = new String[7];
public static String[] name9 = new String[7];
public static String[] name10 = new String[7];
public static int count;
public static String tester;
// begin execution of the program
public static void input()
{
try
{
File f = new File("Students.txt");
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
}
catch(Exception e)
{
System.out.println("No input reader");
}
createWriter();
printer();
}
public static void createWriter()
{
try
{
BW=new BufferedWriter(new OutputStreamWriter(System.out));
BW_File=new BufferedWriter(new FileWriter("Pets.txt"));
}
catch(Exception e)
{
System.out.println("Couldn't create the output writer");
}
}
public static void printer()
{
///////////////////////////////////////////////////////////////////////
// Prints a heading on another .txt file
///////////////////////////////////////////////////////////////////////
try
{
BW_File.write("\t\tHeader for new file" );
BW_File.write("\n");
}
catch(Exception e)
{
System.out.println("Can't print headings");
}
}
public static void grabAndAssign()
{
count=0;
// Stream to read file.
FileInputStream fin;
try
{
// Open an input stream.
fin= new FileInputStream("Students.txt");
// Read a line of text
tester=new DataInputStream(fin).readLine();
System.out.println(tester);
// Close the input stream.
fin.close();
}
catch(Exception e)
{
System.out.println("HELP!!!");
}
}
public static void closeBuffer()
{
try
{
BW.close();
BW_File.close();
}
catch(Exception e)
{
System.out.println("Can't close reader");
}
}
}
> Is there any particular logic to where the words go?
>
> Happy
> Birthday
> To
> You
> Mr
> President
>
> What if this was the text file, where do Mr and
> President go?
Mr and President would go into another array.
1. Did you follow the link and read that example from the Java Almanac? To read several lines, write a loop.
2. When catching an exception,. do the following so you can at least see what went wrong:
catch (Exception e) {
e.printStackTrace();
}
3. What is the format of the input file? Is there a limit on the number of lines? You 10 arrays of 7 strings is a bad idea: when it gets to that much data, you should probably have a list, like List<Something>.