0

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

//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)
   {}
 }
}
4

3 回答 3

0
do{  
   name=scan.next();  

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

应该

do{  
   name=scan.next();  
   System.out.println(name); 
 }while(scan.hasNext());  
于 2013-04-01T06:05:14.613 回答
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);  
            }
       }  
  });
于 2013-04-01T06:15:25.820 回答
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());  
    }
}

更新1

如果您想为按钮使用另一个类,请尝试以下代码:

textview类的创建和对象

textview tv = new textview();

调用 Open() 方法:

tv.Open();

并获得下一个文本使用:

text1.setText(tv.Read());

更新2

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;
}
}
于 2013-04-01T06:25:51.070 回答