Si MPS said:Hi, i've got a textfile named words.txt i want to read into an array, have you any idea how i can do this, because the notes i was given are totally unclear and its driving me insane.
package testbufferedreader;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
class TestBufferedReader extends JFrame{
private JTextArea j = new JTextArea();
private Container c;
public TestBufferedReader() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
//
c = getContentPane();
// add the only control to the content pane
c.add(j);
}
public void readFile() throws IOException {
// Open the file
File inFile = new File("c:\\limerick.txt");
// Create a file stream, wrapping the file
FileReader fileReader = new FileReader(inFile);
// Create a second wrapper,
BufferedReader bufReader = new BufferedReader(fileReader);
// Read in the first line...
String s;
// Tries to read forever
while (true) {
s = bufReader.readLine();
// a null reference is returned if the end of file
// has been reached.
if(s == null) throw new EOFException("Hello");
// Add the new string to the text area
j.append(s);
j.append("\n"); // Append a new line to make it look nice
}
}
public static void main(String args[]) {
System.out.println("Starting TestBufferedReader...");
TestBufferedReader mainFrame = new TestBufferedReader();
mainFrame.setSize(400, 400);
mainFrame.setTitle("TestBufferedReader");
mainFrame.setVisible(true);
// Lets add error detection
try {
mainFrame.readFile();
}
catch (FileNotFoundException e) {
System.out.println("Warning, FileNotFound exception raised"+e);
}
catch (IOException e) {
System.out.println("Warning, I/O exception "+e);
}
}
}
Si MPS said:This is my code for reading it into a string, to modify this for an array should i put a 'for loop' in and increment the index until ive filled it with the words?
drak3 said:j.append(s); // What this does is to take the string s and put it in the Object j (being the text area here).
so instead of this, you can put it in the array.
static String[] myarray = new array[10]; //declare this as global
and then you put the strings into it by myarray; were i is incremented in a loop.
drak3 said:You dont, 10 there is an example. (my bad not excplicitly saying it is example)
You use dynamic data structures in this cases, but before we move into that is best to understand by keeping the rest as simple as possible I belive.
//Buffered Reader to read from the specified file (fileName)
BufferedReader reader = new BufferedReader(new FileReader(fileName));
//String to temporarily hold each line of the file
String in = reader.readLine();
//Output String
String out = "";
//Stop reading if the line is blank
while (in != null) {
//Append output string with next input line (or you could add it to an array or whatever)
out += in + "\n";
//Read next line of file
in = reader.readLine();
}
Si MPS said:Beaut, got it working, reading into an ArrayList
One other thing, does anyone know an easy way to limit the input of a JTextField, i.e. only put 9 characters in
JTextField field = new JTextField();
field.setColumns(9);
if(evt.getSource()==CreateRandomLetters) {
RandomLetterField = ""; //set initial word to blank
for (int i=1; i<10; i++ ){ // run code 9 times to generate each letter
number = Math.random(); // random number
numericvalue = (number * 26 + 'a'); //get numeric representation of character
randomChar = (char) numericvalue; //convert that number into the character
RandomLetterField = RandomLetterField + randomChar; //add previous character to word
}
RandomLetters.setText(RandomLetterField);