8 Replies Latest reply: Dec 12, 2006 6:35 PM by 807599 RSS

    scanner doesnt seem to work if number of lines aren't even..why?

    807599
      im making a simple program (fooling around with java) and i tried uploading a file that have an uneven amount of lines. There's a error at the end of the program.. I post code + my txt file..any comments would be appreciated..
      import javax.swing.*;
      import java.util.*;
      import java.io.*;
      
      public class TestOnThursday {
        public static void main (String [] args) throws IOException {
          JFileChooser jfc = new JFileChooser();
          jfc.showOpenDialog(null); // opens the chooser
          String file = jfc.getSelectedFile().getAbsolutePath();
          Scanner sc = new Scanner(new File(file));
          String eggs = sc.nextLine();
          System.out.println(eggs);
          while (sc.hasNext()) {
            String cheese = sc.nextLine();
            String ham = sc.nextLine();
            System.out.println(cheese);
            System.out.println(ham);
            
          }
          System.out.println("end of file");
        }
      }
      txt file(dont ask why I put such randomness..i have a test in about 2 days and i need to get ansome grade)

      handsinmypockethandsinmypockethandsinmypocket
      iam
      thesmartest
      kid
      in
      the
      world
      yay!!!!!!