Reading from text file to a JTextfield
I followed a tutorial on how to read from a text file to a JTextField. It has been a few hours that I keep getting the same error when I run the program: **
Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:907) at java.util.Scanner.next(Scanner.java:1416) at demo.Demo.Read(Demo.java:26) at demo.DemoSwing.main(DemoSwing.java:42)
**
Here is the code that I am currently using:
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);
} } }
Add if condition for your surname.
if(scan.hasNext())
Surname = scan.next();
Add panel to a frame and access static values using class name while setting to text fields.
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);
}
}
}
It seems, the file you read has an unappropriate format. At least it seems, the scanner is not able to read a single token properly. To tell the reason for this, one would have to look at the file you want to read in.
A java.util.NoSuchElementException is thrown by Scanner.next(), if no more token exists. Since you check for this via scan.hasNext(), this must be happening in the initial loop of your do-while construction. If you check scan.hasNext() initially, there won't be this exception, but however you still won't get the desired result, because the loop would not run even a single time.
while(scan.hasNext())
{
Name = scan.next();
Surname = scan.next();
}
链接地址: http://www.djcxy.com/p/96080.html
上一篇: java.util.Scanner中
下一篇: 从文本文件读取到JTextfield