This is a school project. I need to read jokes from multiple files and show them in separate threads. It complies but I keep getting IndexOutOfBoundsExceptions. Any tips or help you can give would be greatly appreciated.
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.*;
import java.io.FileNotFoundException;
import java.awt.event.*;
import java.util.*;
public class JokeOutput{
public static void main (String args[])
{
while(true)
{
String tempString = (String)JOptionPane.showInputDialog("Enter File Name");
Thread temp = new Thread(new FileWatcher(tempString));
temp.run();
}
}
}
class FileWatcher extends Thread{
private String fileName;
private File tempfile;
public FileWatcher(String s){
fileName = s;
tempfile = new File(fileName);
}
public void run(){
try{
System.out.println("Checking for file \"" + fileName + "\"");
while(!tempfile.exists()){
System.out.println("File \"" + fileName + "\" not found");
this.sleep(10000);
}
System.out.println("File \"" + fileName + "\" found");
if(fileName.substring((fileName.length() - 3)).equals("obj"))
{
Thread newThread = new FileWatcherObj(tempfile);
newThread.run();
}
if(fileName.substring((fileName.length() - 3)).equals("txt"))
{
Thread newThread = new FileWatcherTxt(tempfile);
newThread.run();
}
if(fileName.substring((fileName.length() - 3)).equals("dat"))
{
Thread newThread = new FileWatcherDat(tempfile);
newThread.run();
}
}catch(InterruptedException e){}
}
}
class FileWatcherObj extends Thread{
private ObjectInputStream in;
private ArrayList<Joke> jokeList = new ArrayList(10);
private JokeFrame frame;
public FileWatcherObj(File f){
try{
in = new ObjectInputStream(new FileInputStream(f));
frame = new JokeFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
for(int i = 0; i < 10; i++)
{
Joke tempJoke = new Joke();
tempJoke = (Joke)in.readObject();
jokeList.add(tempJoke);
}
}catch(FileNotFoundException e){}catch(IOException g){}catch(ClassNotFoundException e){}
}
public void run(){
try{
while(true)
{
int random = (int)((Math.random() * jokeList.size())+1);
frame.showJoke(jokeList.get(1).toString());
this.sleep(10000);
}
}catch(InterruptedException f){}
}
}
class FileWatcherDat extends Thread{
private DataInputStream in;
private ArrayList<Joke> jokeList = new ArrayList(10);
private JokeFrame frame;
public FileWatcherDat(File f){
try{
in = new DataInputStream(new FileInputStream(f));
frame = new JokeFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
for(int i = 0; i < 10; i++)
{
Joke tempJoke = new Joke(in.readLine());
jokeList.add(tempJoke);
}
}catch(FileNotFoundException e){}catch(IOException g){}
}
public void run(){
try{
while(true)
{
int random = (int)((Math.random() * jokeList.size())+1);
frame.showJoke(jokeList.get(random).toString());
this.sleep(10000);
}
}catch(InterruptedException f){}
}
}
class FileWatcherTxt extends Thread{
private BufferedReader in;
private ArrayList<Joke> jokeList = new ArrayList(10);
private JokeFrame frame;
public FileWatcherTxt(File f){
try{
in = new BufferedReader(new FileReader(f));
frame = new JokeFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
for(int i = 0; i < 10; i++)
{
Joke tempJoke = new Joke(in.readLine());
jokeList.add(tempJoke);
}
}catch(FileNotFoundException e){}catch(IOException g){}
}
public void run(){
try{
while(true)
{
int random = (int)((Math.random() * jokeList.size())+1);
frame.showJoke(jokeList.get(random).toString());
this.sleep(10000);
}
}catch(InterruptedException f){}
}
}
class Joke implements Serializable{
private String joke;
public Joke(){}
public Joke(String j){
joke = j;
}
public String toString(){
return joke;
}
}
class JokeFrame extends JFrame{
private JTextArea text = new JTextArea();
public JokeFrame(){
this.setTitle("Jokes");
this.setSize(600, 400);
this.add(text);
text.setVisible(true);
}
public void showJoke(String s)
{
text.append(s);
}
}