【问题标题】:Reading from text file to a JTextfield从文本文件读取到 JTextfield
【发布时间】:2014-07-31 10:57:08
【问题描述】:

我遵循了一个关于如何从文本文件读取到 JTextField 的教程。运行程序时,我一直收到相同的错误已经有几个小时了: **

线程“main”中的异常 java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:907) 在 java.util.Scanner.next(Scanner.java:1416) 在 demo.Demo.Read(Demo.java:26) 在 demo.DemoSwing.main(DemoSwing.java:42)

**

这是我目前使用的代码:

 package demo;
import java.io.*; 
import java.util.Scanner;

 public class Demo {

 Scanner scan; 
static String Name, Surname;

 public void open() {

     try {
     scan = new Scanner(new File("C:/cygwin/home/James/Demo/src/team1.txt"));
     System.out.println("it is working"); }catch (FileNotFoundException e){
     System.out.println("it is not working"); } }

 public void Read() {   
      do
     {
         Name = scan.next();
         Surname = scan.next();
     } while(scan.hasNext()); System.out.println(Name + Surname);

 scan.close();
      } }




 package demo; 
import javax.swing.*; 
import java.awt.*;
importjava.awt.event.ActionEvent; 
import java.awt.event.ActionListener;

 public class DemoSwing implements ActionListener {
     private JTextField T = new JTextField(30);
     private JTextField T1 = new JTextField(30);
     private JFrame f = new JFrame("Demo");
     private JButton B = new JButton("View");
      // Static variable
     static String N, S;

     public DemoSwing(){
            f.add(new JLabel("Name"));    
T.setEditable(false);    
f.add(T);    
f.add(new JLabel("Surname"));   
 T1.setEditable(false);  
 f.add(T1);
B.addActionListener(this);    
f.add(B);

 // JFrame    f.setLayout(new FlowLayout());    f.setSize(300,100);    f.setVisible(true);

     }
      public static void main (String[] args){
     new DemoSwing();

     Demo f = new Demo();
     f.open();
     f.Read(); } public void actionPerformed(ActionEvent e){
     if(e.getSource()== B)
     {
         T.setText(N);
         T1.setText(S);
     } } }

【问题讨论】:

  • 也许通过使用JTextArea.read()来使用JTextArea
  • 您的代码中的所有> 是怎么回事?
  • 您的文件中似乎只有一行。再添加一行,它应该可以解决错误。

标签: java swing user-interface java.util.scanner


【解决方案1】:

为您的姓氏添加 if 条件。

if(scan.hasNext())
         Surname = scan.next();

将面板添加到框架并在设置文本字段时使用类名访问静态值。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

class Demo {

    Scanner scan;
    static String Name, Surname;

    public void open() {

        try {
            scan = new Scanner(new File("C:/team1.txt"));
            System.out.println("it is working");
        } catch (FileNotFoundException e) {
            System.out.println("it is not working");
        }
    }

    public void Read() {
        do {
            Name = scan.next();

            if (scan.hasNext())
                Surname = scan.next();

        } while (scan.hasNext());
        System.out.println(Name + Surname);

        scan.close();
    }
}

public class DemoSwing implements ActionListener {
    private JTextField T = new JTextField(30);
    private JTextField T1 = new JTextField(30);
    private JFrame f = new JFrame("Demo");
    private JPanel p = new JPanel();
    private JButton B = new JButton("View");
    // Static variable
    static String N, S;

    public DemoSwing() {

        f.setVisible(true);
        f.setSize(500, 500);

        p.add(new JLabel("Name"));
        T.setEditable(false);
        p.add(T);

        p.add(new JLabel("Surname"));
        T1.setEditable(false);
        p.add(T1);

        B.addActionListener(this);
        p.add(B);

        f.add(p);
        // JFrame f.setLayout(new FlowLayout()); f.setSize(300,100);
        // f.setVisible(true);

    }

    public static void main(String[] args) {
        new DemoSwing();

        Demo f = new Demo();
        f.open();
        f.Read();
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == B) {
            T.setText(Demo.Name);
            T1.setText(Demo.Surname);
        }
    }
}

【讨论】:

  • 我已将条件添加到姓氏,结果出现在控制台中,谢谢。不幸的是,当我按下查看按钮时,GUI 没有显示任何内容
  • 我已经完成了您所做的工作,并且效果很好。我真的很感谢你的帮助:)
  • 另见Initial Threads
【解决方案2】:

看来,您阅读的文件格式不合适。至少看起来,扫描仪无法正确读取单个令牌。要说明原因,必须查看您要读取的文件。

如果不再存在令牌,则 Scanner.next() 会抛出 java.util.NoSuchElementException。由于您通过 scan.hasNext() 检查此内容,因此这必须在您的 do-while 构造的初始循环中发生。如果你最初检查 scan.hasNext() ,不会出现这个异常,但是你仍然不会得到想要的结果,因为循环不会运行一次。

    while(scan.hasNext())
    {
        Name = scan.next();
        Surname = scan.next();
    }

【讨论】:

    猜你喜欢
    • 2013-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多