从文本文件读取到JTextfield

我遵循了关于如何从文本文件读取到JTextField的教程。 我在运行程序时经历了几个小时的错误:**

在线程中出现异常“main”java.util.NoSuchElementException在java.util.Scanner.throwFor(Scanner.java:907)at java.util.Scanner.next(Scanner.java:1416)at 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);
     } } }

如果条件为你的姓氏,请添加。

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);
        }
    }
}

看来,你读的文件有一个不适当的格式。 至少看起来,扫描器无法正确读取单个令牌。 要说明原因,人们必须看看你想要阅读的文件。

如果不存在更多标记,Scanner.next()将引发java.util.NoSuchElementException。 既然你通过scan.hasNext()检查了这一点,这必定发生在你的do-while构造的初始循环中。 如果您最初检查scan.hasNext(),则不会出现此异常,但是您仍然无法得到期望的结果,因为循环不会运行一次。

    while(scan.hasNext())
    {
        Name = scan.next();
        Surname = scan.next();
    }
链接地址: http://www.djcxy.com/p/96079.html

上一篇: Reading from text file to a JTextfield

下一篇: I'm receiving an error on run