Results 1 to 3 of 3
  1. #1
    explosion242 is offline Member
    Join Date
    Sep 2008
    Posts
    1
    Rep Power
    0

    Default Cannot display a random string from .dat file

    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:

    Java Code:
    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!

  2. #2
    dhnsekaran is offline Member
    Join Date
    Sep 2008
    Posts
    9
    Rep Power
    0

    Default

    for me also it need please any one help me ?

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Can you describe what the problem is? Compile error or runtime error or wrong output or ?

Similar Threads

  1. Update a record in Random access file
    By Rgfirefly24 in forum New To Java
    Replies: 2
    Last Post: 04-24-2008, 11:07 PM
  2. [SOLVED] Get a random String
    By LeoCoderIV in forum New To Java
    Replies: 6
    Last Post: 04-07-2008, 03:58 PM
  3. random string are duplicate
    By googgoo in forum New To Java
    Replies: 3
    Last Post: 04-03-2008, 11:01 AM
  4. Display random cards
    By carl in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 08:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •