【问题标题】:Reading multiple integers in multiple lines在多行中读取多个整数
【发布时间】:2016-12-30 10:03:28
【问题描述】:

我目前正在写我的图论学士学位论文,并使用 java 扫描仪将带有边的 txt 文件转换为我的 Java 类图。我的 txt 文件如下所示:

1 2 72 3
2 3 15 98
4 7 66 49
5 6 39 48
6 9 87 97
8 13 31 5

整数的顺序为:源顶点、汇顶点、成本、容量。

我的代码如下:

Graph graph = new Graph(false);
File f = new File("Filepath");
Scanner in = new Scanner(f);
while (in.hasNextLine())
{
    for (int i =1; i<= numberEdges; i++)
    {
        String s = in.nextLine();
        try (Scanner inscan = new Scanner(s)) {
            while (inscan.hasNext())
            {
                int source = inscan.nextInt();
                int sink = inscan.nextInt();
                double cost =inscan.nextDouble();
                double capacity = inscan.nextDouble();
                Vertex Source = new Vertex(source);
                Vertex Sink = new Vertex(sink);
                Edge edge = new Edge(Source,Sink, cost, capacity);
                graph.addEdge(edge);
            }
        }
    }    
}
in.close();

我尝试扫描字符串中的每一行,然后将字符串扫描到我的变量中。 它总是在 for 循环的第一行抛出“NoLineFound”异常,如果我尝试输出这些行,我什么也得不到。但是当我禁用第二个扫描仪并重试时,我得到了输出中的所有行,但最后仍然是“NoLineFound”异常。

我检查了我的 txt 文件,最后一行没有 UTF8 行结尾,但我不知道如何给它一个。

【问题讨论】:

  • 您能否添加您对File f = new File("Filepath");的确切代码
  • 查看这个问题stackoverflow.com/questions/7209110/… 的接受答案。你需要先检查in.hasNextLine(),然后再做nextLine()
  • 我认为 for 循环没有意义。不用它试试看
  • 谢谢!问题是for循环。我昨天刚开始使用扫描仪,并没有意识到 while(in.hasnextline()) 和我的 for 循环一样

标签: java java.util.scanner


【解决方案1】:

我认为您的问题来自于此:

while (in.hasNextLine()){
    for (int i =1; i<= numberEdges; i++)
        {

首先,迭代是多余的(whilefor 足够单一,可以读取每一行。您必须在它们之间进行选择)。
此外,如果您的文件的行数少于numberEdges,则会引发java.util.NoSuchElementException

如果文件中的行数是固定的,使用for:

for (int i =1; i<= numberEdges; i++)

删除封闭的while (in.hasNextLine())。这不是必需的。迭代控制已经由for 完成。

如果文件中的行数可能不同,请仅使用 while

    while (in.hasNextLine()){

但无论如何,不​​要同时使用。

【讨论】:

    【解决方案2】:

    使用 Java 8 流:

    Files
        .lines(f.toPath())
        .map(l ->Arrays.stream(l.split(" ")).mapToDouble(Double::parseDouble).toArray())
        .map(a->new Edge(new Vertex((int)a[0]), new Vertex((int)a[1]), a[2], a[3]))
        .forEach(graph::addEdge);
    

    【讨论】:

      【解决方案3】:

      在对hasNextLine() 进行一次检查后,您正在循环中读取nextLine()。您需要在每次读取后执行检查以避免“NoLineFound”异常。

      看起来嵌套循环是完全没有必要的。您可以逐行读取文件,忽略空行,并在不事先了解其拥有的边数的情况下构建图形:

      Graph graph = new Graph(false);
      File f = new File("Filepath");
      Scanner in = new Scanner(f);
      while (in.hasNextLine()) {
          String s = in.nextLine();
          try (Scanner inscan = new Scanner(s)) {
              if (!inscan.hasNext()) {
                  continue; // Ignore empty lines
              }
              int source = inscan.nextInt();
              int sink = inscan.nextInt();
              double cost =inscan.nextDouble();
              double capacity = inscan.nextDouble();
              Vertex Source = new Vertex(source);
              Vertex Sink = new Vertex(sink);
              Edge edge = new Edge(Source,Sink, cost, capacity);
              graph.addEdge(edge);
          }    
      }
      in.close();
      

      【讨论】:

        【解决方案4】:

        只需在读取后执行检查以避免“NoLineFound”异常。

        您可以使用以下代码扫描文件:

        import java.io.File;
        import java.util.Scanner;
        
        public class ReadFile {
        
            public static void main(String[] args) {
        
                try {
                    System.out.print("Enter the file name with extension : ");
                    Scanner input = new Scanner(System.in);
                    File file = new File(input.nextLine());
                    input = new Scanner(file);
        
                    while (input.hasNextLine()) {
                        String line = input.nextLine();
                        System.out.println(line);
                    }
                    input.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-09-22
          • 2021-02-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-04-21
          相关资源
          最近更新 更多