title: Reading from text file to a JTextfield
body: <p>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: 
  **</p>
  
  <blockquote>
    <p>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)</p>
  </blockquote>
  
  <p>**  </p>
  
  <p>Here is the code that I am currently using:</p>
  
  <pre><code> 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);
       } } }
  </code></pre>