graphical_force
09-17-2008, 11:59 PM
Hello, I am pretty new to Java so any help would be appreciated. I am trying to display a random string that is saved into a .dat file. The file is read but I am not sure how to go about this.
Here is what I have:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class SecretePhraseUsingFile extends JFrame
{
final int WIDTH = 420;
final int HEIGHT = 200;
private JLabel companyName =
new JLabel("Phrase Game");
private Font bigFont =
new Font("Helvetica", Font.ITALIC, 24);
private JLabel prompt =
new JLabel("Secrete phrase");
private JTextField phrase = new JTextField(30);
String newPhrase;
DataInputStream istream;
public SecretePhraseUsingFile()
{
super("Create Phrase");
try
{
istream = new
DataInputStream(new FileInputStream
("Phrases.dat"));
String phraseString;
ArrayList phraseArray = new ArrayList();
phraseString = istream.readUTF();
Scanner scanner = new Scanner(istream);
while(scanner.hasNext())
{
phraseArray.add(scanner.nextLine());
}
Collections.shuffle(phraseArray);
phrase.setText(String.valueOf(phraseArray.get(0)));
System.out.println(phraseString);
scanner.close();
}
catch (IOException e)
{
e.printStackTrace();
}
setSize(WIDTH, HEIGHT);
setLayout(new FlowLayout());
companyName.setFont(bigFont);
add(companyName);
add(prompt);
add(phrase);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
SecretePhraseUsingFile cef = new SecretePhraseUsingFile();
}
}
Any suggestions?
Thanks in advance!
Here is what I have:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class SecretePhraseUsingFile extends JFrame
{
final int WIDTH = 420;
final int HEIGHT = 200;
private JLabel companyName =
new JLabel("Phrase Game");
private Font bigFont =
new Font("Helvetica", Font.ITALIC, 24);
private JLabel prompt =
new JLabel("Secrete phrase");
private JTextField phrase = new JTextField(30);
String newPhrase;
DataInputStream istream;
public SecretePhraseUsingFile()
{
super("Create Phrase");
try
{
istream = new
DataInputStream(new FileInputStream
("Phrases.dat"));
String phraseString;
ArrayList phraseArray = new ArrayList();
phraseString = istream.readUTF();
Scanner scanner = new Scanner(istream);
while(scanner.hasNext())
{
phraseArray.add(scanner.nextLine());
}
Collections.shuffle(phraseArray);
phrase.setText(String.valueOf(phraseArray.get(0)));
System.out.println(phraseString);
scanner.close();
}
catch (IOException e)
{
e.printStackTrace();
}
setSize(WIDTH, HEIGHT);
setLayout(new FlowLayout());
companyName.setFont(bigFont);
add(companyName);
add(prompt);
add(phrase);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
SecretePhraseUsingFile cef = new SecretePhraseUsingFile();
}
}
Any suggestions?
Thanks in advance!