Fetching each word from a text file into text field in java
<p>I'm trying to fetch a set of words stored in a text file into a text field one after the other i.e in my form i have a text field and a button, when i click on the button the word present in the text file must be displayed in the text field and when i click the button again, the second word in the text file must be displayed in the text field and so on. I was able to write a program to fetch a single word but not in a loop. Please guide me with this issue. Thanks in advance.</p>

<pre><code>//textview.java  

import java.util.Scanner;  
import java.io.*;  
public class textview   
{  
Scanner scan;  
static String name;   
public void Open()  
{  
try  
{  
scan =new Scanner(new File("F:/MajorProject-NLP/Databases/DataFiles/split.txt")); 
}  
catch(FileNotFoundException e)  
{  
    System.out.println("It wont working");  
}  
}  
public void Read()  
{  
    do  
    {  
       name=scan.next();  

     }  
       while(scan.hasNext());  
       System.out.println(name);  
       scan.close();  
 }  
}  

  // this is my another file where the fetch button is placed.

 import javax.swing.*;
 import java.awt.*;
 import java.io.BufferedWriter;
 import java.io.FileWriter;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;

 class ReadTextFile extends JFrame
 {
  public static String N;
  private static final long serialVersionUID = 1L;
  JButton save,fetch;
  JPanel panel;
  JLabel label1,label2,label3,label4,label5;
  final JTextField text1,text2,text3,text4,text5;

  ReadTextFile(){
  label1 = new JLabel();
  label1.setText("English Word:");
  text1 = new JTextField(20);

  label2 = new JLabel();
  label2.setText("English Synonym:");
  text2 = new JTextField(20);

  label3 = new JLabel();
  label3.setText("Kannada Word:");
  text3 = new JTextField(20);

  label4 = new JLabel();
  label4.setText("Kannada Synonym:");
  text4 = new JTextField(20);

  label5 = new JLabel();
  label5.setText("Parts of Speech:");
  text5 = new JTextField(20);

  save=new JButton("SAVE");
  fetch=new JButton("NEXT");

  panel=new JPanel(new GridLayout(6,2));
  panel.add(label1);
  panel.add(text1);
  panel.add(label2);
  panel.add(text2);
  panel.add(label3);
  panel.add(text3);
  panel.add(label4);
  panel.add(text4);
  panel.add(label5);
  panel.add(text5);
  panel.add(save);
  panel.add(fetch);
  add(panel,BorderLayout.CENTER);
  setTitle("Word Base");

  save.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent ae){
  String value1=text1.getText();
  String value2=text2.getText();
  String value3=text3.getText();
  String value4=text4.getText();
  String value5=text5.getText();
  try
  {
    FileWriter fstream = new FileWriter("F:/MajorProject-NLP/Databases/DataFiles/pos.txt",true);
        BufferedWriter out = new BufferedWriter(fstream);
    out.write(" ");
    out.write(value1);
        out.write(" ");
    out.write(value2);
    out.write(" ");
    out.write(value3);
    out.write(" ");
    out.write(value4);
    out.write(" ");
    out.write(value5);
    out.write(" ");
    out.write("\n");
    out.close();
  }
  catch(Exception e)
  {
    System.err.println("Error"+e.getMessage());
  }
  text1.setText("");
  text2.setText("");
  text3.setText("");
  text4.setText("");
  text5.setText("");
 }

 });

 fetch.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent ae){
          if(ae.getSource()==fetch)
          { 
            text1.setText(N);
              }
  });
  }
  public static void main(String arg[]) 
  {
   try
  {
     ReadTextFile frame=new ReadTextFile();
     frame.setSize(300,200);
     frame.setVisible(true);
     textview tv=new textview();
     tv.Open();
     tv.Read();
     N=textview.name;
  }
   catch(Exception e)
   {}
 }
}
</code></pre>
