4 Replies - 4242 Views - Last Post: 26 October 2014 - 07:45 PM Rate Topic: ***-- 2 Votes

#1 ric1989   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 120
  • Joined: 28-February 12

Java Mad Lib program

Posted 23 October 2014 - 04:55 PM

My mad lib program was able to store my three notepad text: noun, verb, and story. I would
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


Is This A Good Question/Topic? 0
  • +

Replies To: Java Mad Lib program

#2 ric1989   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 120
  • Joined: 28-February 12

Re: Java Mad Lib program

Posted 24 October 2014 - 05:12 AM

I wanted to figure out what i am missing?
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Java Mad Lib program

Posted 24 October 2014 - 06:34 AM

Please avoid needlessly bumping your thread.
Was This Post Helpful? 0
  • +
  • -

#4 ric1989   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 120
  • Joined: 28-February 12

Re: Java Mad Lib program

Posted 26 October 2014 - 07:40 PM

Am i using scanner and fileinput stream correctly to read three text?
I wanted to change my % and # into different word.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Java Mad Lib program

Posted 26 October 2014 - 07:45 PM

You can check this by seeing if the contents of these files have been successfully read in. Though I will note that you are comparing Strings using the == operator, which compares their locations in memory. Use the equals() method, which checks their literal values.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1