Read from a .txt file, then...

How do you read a line from a .txt file, then assign it as an element in an array? Example:The text file reads:HappyBirthdayToYouBut, you want "Happy" and "Birthday" to be in one array, and "To" and "You" to be in another array.
[277 byte] By [oxkeithscowgirlxoa] at [2007-11-26 18:14:32]
# 1
Anyone?
oxkeithscowgirlxoa at 2007-7-9 5:47:57 > top of Java-index,Java Essentials,New To Java...
# 2
Here's a typical example of how to read a text file, line by line: http://www.exampledepot.com/egs/java.io/ReadLinesFromFile.htmlAs for putting strings into arrays, why not try to do it and come back if you can present us with code and a specific question?
DrLaszloJamfa at 2007-7-9 5:47:57 > top of Java-index,Java Essentials,New To Java...
# 3
Is there any particular logic to where the words go?HappyBirthdayToYouMrPresidentWhat if this was the text file, where do Mr and President go?
floundera at 2007-7-9 5:47:57 > top of Java-index,Java Essentials,New To Java...
# 4

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");

}

}

}

oxkeithscowgirlxoa at 2007-7-9 5:47:58 > top of Java-index,Java Essentials,New To Java...
# 5

> 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.

oxkeithscowgirlxoa at 2007-7-9 5:47:58 > top of Java-index,Java Essentials,New To Java...
# 6

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>.

DrLaszloJamfa at 2007-7-9 5:47:58 > top of Java-index,Java Essentials,New To Java...
# 7
I've got it... THANKS BUNCHES!!
oxkeithscowgirlxoa at 2007-7-9 5:47:58 > top of Java-index,Java Essentials,New To Java...
# 8
> Mr and President would go into another array.So everytime you encounter two more words you place them in a new array. Smells like a bad design to me.
floundera at 2007-7-9 5:47:58 > top of Java-index,Java Essentials,New To Java...
# 9
I suspect the Op actually means new element.So maybe... array[0] = "happy birthday"array[1] = "to you"?But I could be wrong.
TimRyanNZa at 2007-7-9 5:47:58 > top of Java-index,Java Essentials,New To Java...