“Java.util.NosuchelementException”我得到了,n值也是错误

时间:2016-01-06 11:36:12

标签: java java.util.scanner nosuchelementexception

nani.txt包含

等值
  

12 6
   6 3
   1 5
   10 8
   9 2

import java.io.*;
import java.io.File;
import java.util.*;

import java.util.Scanner;
import java.util.Arrays;           
class Point
{
  int x, y;
}

/** Class Jarvis **/
public class Jarvis
{    
   private boolean CCW(Point p, Point q, Point r) // counter clockwise method
   {
     int val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);

     if (val >= 0)
        return false;
      return true;
    }
    public void convexHull(Point[] points) // convexhull method
    {
       int n = points.length;

       if (n < 3) 
          return;     
       int[] next = new int[n];
       Arrays.fill(next, -1);

       int leftMost = 0;
       for (int i = 1; i < n; i++)
         if (points[i].x < points[leftMost].x)
            leftMost = i;
            int p = leftMost, q;
        /** iterate till p becomes leftMost **/
        do
        {                         
           q = (p + 1) % n;
           for (int i = 0; i < n; i++)
             if (CCW(points[p], points[i], points[q])) 
               q = i;

               next[p] = q;
               p = q; 
         } while (p != leftMost);               

        display(points, next);
     }
     public void display(Point[] points, int[] next)
     {
        System.out.println("\nConvex Hull points : ");
        for (int i = 0; i < next.length; i++)
          if (next[i] != -1)
            System.out.println("("+ points[i].x +", "+ points[i].y +")");
     }                     

     public static void main (String[] args) 
     {

       File file  = new File("nani.txt");                       

       try
       {
          Scanner scan = new Scanner(file);

          while(scan.hasNextInt())
          {                 
             System.out.println("Jarvis Algorithm Test\n");
             int n=scan.nextInt(); // n value should be from nani.txt file. that contains no of (x,y) coordinates
             System.out.println(n);  

             Point[] points = new Point[n];

             System.out.println("Reading Values");
             for (int i = 0; i < n; i++) //reading values
             {                                                             
               System.out.println("Reading for loop"); //Reading 

               points[i] = new Point();
               points[i].x = scan.nextInt();
               points[i].y = scan.nextInt();
               System.out.println("Reading x,y");
             }                                                                  
             System.out.println("Reading points");
             Jarvis j = new Jarvis();
             j.convexHull(points);  
              System.out.println("points");
            }
           scan.close(); //scan close
          }
          catch (FileNotFoundException e) // catch block
          {
            e.printStackTrace();
          }
        }
      }

我收到了以下例外

  

线程“main”java.util.NoSuchElementException中的异常   在java.util.Scanner.throwFor(Scanner.java:907)   在java.util.Scanner.next(Scanner.java:1530)   在java.util.Scanner.nextInt(Scanner.java:2160)   在java.util.Scanner.nextInt(Scanner.java:2119)

1 个答案:

答案 0 :(得分:0)

尝试将每个数字放在一个新行中。这个例子对我有用:

public static void main(String[] args) {
    Scanner s;
    try {
        s = new Scanner(new File("test.txt"));

        while(s.hasNextInt())
            System.out.println(s.nextInt());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

这是test.txt的内容:

12

11

10

9