like to find out what is the best way to make it display my result randomize my story from notepad?
once everything is stored?
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.util.*;
public class StoryProject extends JFrame implements ActionListener
{
JButton mytextButton = new JButton("Choose your TEXT");
JButton allverbButton = new JButton("Choose your Verb");
JButton anounButton = new JButton("Choose your Noun");
JFileChooser fileChooser = new JFileChooser();
JTextField output = new JTextField(20);
public static void main()
{
StoryProject frame = new StoryProject();
frame.setBackground(Color.YELLOW);
frame.setSize(900, 300);
frame.setTitle("randomize story");
frame.createGUI();
frame.setVisible(true);
}
public void createGUI()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout());
JPanel panel = new JPanel();
panel.setSize(300,300);
panel.setBackground(Color.YELLOW);
window.add(panel);
mytextButton.addActionListener(this);
allverbButton.addActionListener(this);
anounButton.addActionListener(this);
JLabel outputLabel = new JLabel("The mad lib will be: ");
JButton button = new JButton("START");
panel.add(mytextButton);
panel.add(allverbButton);
panel.add(anounButton);
panel.add(outputLabel);
panel.add(output);
}
public void actionPerformed(ActionEvent e)
{
Random selectRandomVerb = new Random();
Random selectRandomNoun = new Random();
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
System.out.println(selectedFile.getName());
}
if (e.getSource().equals(allverbButton))
{
try
{
File verbsFile = fileChooser.getSelectedFile();
if (verbsFile.exists())
{
Scanner verbsScanner = new Scanner(new FileInputStream(verbsFile));
ArrayList<String> verbLines = new ArrayList<String>();
String verbLine;
while ((verbLine = verbsScanner.nextLine()) != null) {
verbLines.add(verbLine);
}
int v = selectRandomVerb.nextInt(verbLines.size());
String verb = verbLines.get(v);
}
else
{
JOptionPane.showMessageDialog(null, "There is no such file, Try Again");
}
}
catch (FileNotFoundException ev)
{
ev.printStackTrace();
}
}
if (e.getSource().equals(anounButton))
{
try
{
File nouns = fileChooser.getSelectedFile();
if (nouns.exists())
{
Scanner nounsScanner = new Scanner(new FileInputStream(nouns));
ArrayList<String> nounLines = new ArrayList<String>();
String nounLine;
while ((nounLine = nounsScanner.nextLine()) != null) {
nounLines.add(nounLine);
}
int n = selectRandomNoun.nextInt(nounLines.size());
String noun = nounLines.get(n);
}
else
{
JOptionPane.showMessageDialog(null, "There is no such file, Try Again");
}
}
catch(FileNotFoundException ev)
{
ev.printStackTrace();
}
}
if (e.getSource().equals(mytextButton))
{
File storyFile = fileChooser.getSelectedFile();
try{
if (storyFile.exists())
{
Scanner storyScanner = new Scanner(new FileInputStream(storyFile));
ArrayList<String> storyArrayList = new ArrayList<String>();
for (String element : storyArrayList) {
if (storyScanner.nextLine() == "#")
{
}
}
for (String element : storyArrayList) {
if (storyScanner.nextLine() == "%")
{
}
}
// output.setText();
}
else
{
JOptionPane.showMessageDialog(null, "there is no such file, Try again!");
}
}
catch(FileNotFoundException ev)
{
ev.printStackTrace();
}
}
}
}
This post has been edited by macosxnerd101: 23 October 2014 - 04:58 PM
Reason for edit:: Fixed code tags

New Topic/Question
Reply



MultiQuote





|