-1

nani.txt contains values such as

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

I got the following Exceptions

Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:907) at java.util.Scanner.next(Scanner.java:1530) at java.util.Scanner.nextInt(Scanner.java:2160) at java.util.Scanner.nextInt(Scanner.java:2119)

Dimitar
  • 4,402
  • 4
  • 31
  • 47

1 Answers1

0

Try putting each number in a new line. This example worked for me:

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

This is the content of test.txt:

12

11

10

9

Tim
  • 14
  • 1
  • Data is reading ok.. but j.convexHull(points); constructor is not returning any values..and also Convexhull method is not giving any output – Challagulla Muralikrishna Jan 06 '16 at 12:06
  • the convexHull method has a void return type. It is not supposed to return anything. – Tim Jan 06 '16 at 12:16
  • Also the main method is in the Jarvis class. Either Jarvis should be a separate class in a separate file or all methods should be static and you wold not need to create a Jarvis object. I don't know which constructor you mean. You have no constructor. – Tim Jan 06 '16 at 12:19