2016-02-10 102 views
0

我的输入是这样的。我必须从一个文件中读取超过5000个值,包括负值和浮点值以及值,这些值也会输入到我的jarvis 3月份算法中。从文件读取整数值并直接输入到程序

1 
2 
6 
-5 
8 
7.32 
2 
3 
8 
-3.32 
9 
1.25 
7 
3 

代码:

public static void main(String[] args) { 
     Scanner scan; 

     try 
     { 
      FileReader fr=new FileReader("nani.txt"); 
      int[] integers = new int [50]; 
      int i=0; 
      scan=new Scanner(fr); 

      while(scan.hasNextInt()) 
      { 
          integers[i] = scan.nextInt(); 
          i++; 
          for(int item: integers) { 
          System.out.println(item); 
      } 

      System.out.println("Jarvis Algorithm Test\n"); 

      int n=scan.nextInt(); 
      System.out.println(n); 
      scan.useDelimiter(",|\\s*"); 



      /** Make an object of Jarvis class **/ 

      Point[] points = new Point[n]; 

      System.out.println("Reading X,Y Values From File"); 


      for (i = 0; i < n && scan.hasNext(); i++) 
      { 
       points[i] = new Point(); 
       points[i].x = scan.nextInt(); 
       points[i].y = scan.nextInt(); 

       System.out.println("(x,y) values are:"+ points[i].x + "\t" +points[i].y); 
      } 

      Jarvis j = new Jarvis(); 
      Jarvis.convexHull(points); 
      } 
      scan.close(); 
     } 


     catch (FileNotFoundException e) 
     { 
      e.printStackTrace(); 
     } 
    } 

如何从文件中获得的n值?

+0

请阅读 “[如何创建一个最小的,完整的,并且可验证的示例](http://stackoverflow.com/help/mcve)”。你展示了很多与你的问题无关的代码。 – Andreas

回答

0

这是建议。您可以将if语句添加到搜索测试文件的循环中,并且条件评估为true,然后在文本文件中打印该行。然后,跳出循环。

为(I = 0;我<Ñ& & scan.hasNext();我++)

{

 if(scan.hasNext() == x) 

{

的System.out.println(“(X, y)值是:“+ points [i] .x +”\ t“+ points [i] .y);

break;

}

}

+0

x将是您搜索文件的值。 –

相关问题