"Java.util.NosuchelementException" 我正在获取并且 n 值也是错误的
"Java.util.NosuchelementException" I am getting and also n value is fault
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();
}
}
}
我遇到了以下异常
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)
尝试将每个数字换行。这个例子对我有用:
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
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();
}
}
}
我遇到了以下异常
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)
尝试将每个数字换行。这个例子对我有用:
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