我正在尝试将存储在文本文件中的一组单词一个接一个地提取到一个文本字段中,即在我的表单中我有一个文本字段和一个按钮,当我单击按钮时,文本文件中存在的单词必须显示在文本字段中,当我再次单击按钮时,文本文件中的第二个单词必须显示在文本字段中,依此类推。我能够编写一个程序来获取一个单词,但不能在循环中。请指导我解决这个问题。提前致谢。
//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)
{}
}
}