Results 1 to 9 of 9
  1. #1
    medic642 is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default Reading from a text file, then writing back to Text Area in Reverse

    Hello,

    I am working on a school assignment (GUI based) that will read an existing file consisting of single words on six different lines, then write these words in reverse order to a JTextArea. I am having trouble locating any guides to help me through the creation of an array and then reading from the array in reverse order. I have looked at creating a buffer and reading from it at specific points to get the characters but it seems there should be a better way with an array. Does anyone have any suggestions for sites to look at? The API pages are cryptic and give no examples. I have already created my GUI and have a fileinputstream working.

    Thanks!

    Jared

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What have you tried? Have you read the file into an array?(or the easier to work with ArrayList). Once you do that a simple loop can be used to build the string from the array or list.

  3. #3
    medic642 is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default re:

    Actually, I am trying to work through this myself and I am looking for good documentation of array or arraylist with examples. Once I get a good grip on the concept I think I can work through it myself. I just can't find documentation with examples!

    Thank you.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,604
    Rep Power
    23

    Default

    Go to this site and Search for array:
    The Really Big Index

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Here is a link to the tutorials on arrays: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

    You can find a similar one for collections on the same site.

    Edit: too slow :(

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    This can be achieved with recursion but I guess you haven't covered that yet.

  7. #7
    medic642 is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default

    Ok, Here's the code I have so far. Won't compile and I need a few pointers, please. I am getting errors about the textcopyarea and I need to now how to tweak the code to get my file "input.txt" as the input file Thanks!

    Java Code:
    // Import Classes
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.JCheckBox;
    import javax.swing.JTextArea;
    import java.io.*;
    import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;
    
    
    public class FileToArray extends JFrame
    {
    
    
    public static void main(String[] args) {
    
    // Create Window
    
    JFrame frame = new JFrame();
    
    frame.setSize(400, 250);
    frame.setTitle("File Reader");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    // Create Text Area
    
    final JTextArea textcopyarea = new JTextArea(10, 20);
    textcopyarea.setText("");
    
    // Create Button
    
    JButton button = new JButton("Process");
    
    // Create Display Panel
    
    JPanel panel = new JPanel();
    
    panel.add(button);
    panel.add(textcopyarea);
    frame.add(panel);
    
    // Listener Class
    
    class CalculateListener implements ActionListener
    {
    
    public void actionPerformed(ActionEvent event) {
    
    
    // Get Input From File
    
    try {
    String brline="";
    String filebuffer[]=new String[100000];
    int i=0; int j=0;
    RandomAccessFile file=new RandomAccessFile(filenametext.getText(),"r");
    while((brline = file.readLine()) != null)
    {
    filebuffer[i]=brline;
    i++;
    }
    int len = i;
    file.close();
    
    txtcopyarea.append("\r\n");
    for(j = len; j>=0 ;j--)
    {
    txtcopyarea.append(filebuffer[j]);
    txtcopyarea.append("\r\n");
    txtcopyarea.setLineWrap(true);
    txtcopyarea.setWrapStyleWord(false);
    txtcopyarea.setCaretPosition(txtcopyarea.getText() .length() - 1);
    }
    
    }catch(Exception e) { System.out.println(e); }
    
    
    }
    }
    
    ActionListener listener = new CalculateListener();
    button.addActionListener(listener);
    
    frame.setVisible(true);
    
        }
    
    
    }
    Last edited by medic642; 07-17-2011 at 05:20 AM. Reason: spelling errors!

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    First thing I see is that you are importing nio classes, this can all be done with regular io(and probably should), nio is probably too advanced for you.

    I would also suggest you not clump all code into the main method and instead have the main method only contain a line or two of code.

    Why are you using random access file? Do you need to access into the file randomly? If not, use File.

    All you need for io: http://download.oracle.com/javase/tu.../essential/io/

    And for swing: http://download.oracle.com/javase/tu...ing/index.html
    Last edited by sunde887; 07-17-2011 at 06:13 AM.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,604
    Rep Power
    23

    Default

    I am getting errors about the textcopyarea
    When you get errors, please copy and paste the full text of the message.

    For reading the file, the Scanner class is much simpler.
    Last edited by Norm; 07-17-2011 at 02:40 PM.

Similar Threads

  1. Replies: 0
    Last Post: 12-09-2010, 05:56 PM
  2. Replies: 0
    Last Post: 09-28-2010, 09:52 AM
  3. writing to text Area through Class
    By dilpreet28 in forum New To Java
    Replies: 3
    Last Post: 07-06-2010, 12:11 PM
  4. Replies: 6
    Last Post: 12-12-2008, 03:35 PM
  5. Reading and Writing Text Files
    By kandt in forum New To Java
    Replies: 1
    Last Post: 11-12-2008, 03:15 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
  •