【问题标题】:read integer values from file and directly input to program [duplicate]从文件中读取整数值并直接输入到程序[重复]
【发布时间】:2016-05-20 07:57:36
【问题描述】:

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

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

【问题讨论】:

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


【解决方案1】:

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

for (i = 0; i

{

      if(scan.hasNext() == x)

{

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

休息;

}

}

【讨论】:

  • x 将是您在文件中搜索的值。
猜你喜欢
  • 2015-08-04
  • 2019-04-24
  • 2019-07-03
  • 2018-04-17
  • 1970-01-01
  • 1970-01-01
  • 2018-07-23
  • 2011-05-05
  • 2021-10-25
相关资源
最近更新 更多