从文件中读取整数值并直接输入到程序中

时间:2016-02-10 05:02:39

标签: java file java.util.scanner convex-hull

我的输入是这样的。我必须从一个文件中读取超过5000个值,包括负值,浮点数和双精度值,这些值也输入到我的jarvis march agorithm。

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值?

1 个答案:

答案 0 :(得分:0)

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

for(i = 0; i&lt; n&amp;&amp; scan.hasNext(); i ++)

{

      if(scan.hasNext() == x)

{

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

中断;

}

}

相关问题