我正在尝试将存储在文本文件中的一组单词一个接一个地提取到文本字段中,即在我的表单中我有一个文本字段和一个按钮,当我单击按钮时,文本文件必须显示在文本字段中,当我再次单击该按钮时,文本文件中的第二个单词必须显示在文本字段中,依此类推。我能够编写一个程序来获取单个单词,但不能在循环中。请指导我这个问题。提前谢谢。
//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)
{}
}
}
答案 0 :(得分:0)
do{
name=scan.next();
} while(scan.hasNext());
System.out.println(name);
应该是
do{
name=scan.next();
System.out.println(name);
}while(scan.hasNext());
答案 1 :(得分:0)
你可以通过将所有单词存储到arraylist对象中来实现这一点,然后根据按钮单击将其移动为prev / next。在您阅读时,请继续进行小型实施
犹豫不决的方法
public List<String> Read(){
//initialize
List<String> words = new ArrayList<String>();
do{
name=scan.next();
// now store all the words into the words object
words.add(name);
} while(scan.hasNext());
return words;
}
这将读取文件中的所有单词并存储到words
对象中。现在点击按钮,只需通过递增值
int wordIndex = -1;
fetch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
if(words.size>0 && wordIndex<words.size()){
wordIndex++;
String word = words.get(wordIndex);
text1.setText(N);
}
}
});
答案 2 :(得分:0)
试试这个,它正在我的机器上工作.....
public void Open()
{
try
{
scan =new Scanner(new File("C:/temp.txt"));
flag=true;
}
catch(FileNotFoundException e)
{
flag=false;
System.out.println("It wont working");
}
}
public String Read()
{
if(flag)
name=scan.next();
else
return "End of the file";
System.out.println(name);
if(!scan.hasNext())
{
scan.close();
flag=false;
}
return name;
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==fetch)
{
text1.setText(Read());
}
}
<强> UPDATE1 强>
如果您想使用另一个类作为按钮,请尝试以下代码:
创建textview
类
textview tv = new textview();
调用Open()方法:
tv.Open();
并获得下一个文字:
text1.setText(tv.Read());
<强> UPDATE2 强>
public class textview extends JFrame implements ActionListener
{
Scanner scan;
static String name;
JButton fetch;
JTextField text1;
testview2 t2 = new testview2();
public static void main(String args[])
{
new textview();
}
public textview()
{
setLayout(new FlowLayout());
setSize(200, 200);
fetch = new JButton("FetchData");
text1 = new JTextField(20);
add(fetch);
fetch.addActionListener(this);
add(text1);
setVisible(true);
t2.Open();
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==fetch)
{
text1.setText(t2.Read());
}
}
}
class testview2 {
Scanner scan;
static String name;
boolean flag=false;
public void Open()
{
try
{
scan =new Scanner(new File("C:/temp.txt"));
flag=true;
}
catch(FileNotFoundException e)
{
flag=false;
System.out.println("It wont working");
}
}
public String Read()
{
if(flag)
name=scan.next();
else
return "End of the file";
System.out.println(name);
if(!scan.hasNext())
{
scan.close();
flag=false;
}
return name;
}
}