How can I fix no line found error when using scanner in my code?












0














I keep getting this exception when I run my code:



Exception in thread "main" java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Unknown Source)
at BattleshipCheck.generateGrid(BattleshipCheck.java:60)


here's my code:



public static int generateGrid(Scanner in, int gridSize) {
int grid = new int[gridSize][gridSize];
while(in.hasNextLine()) {
in.nextLine();
in.nextLine();
String currentLine = in.nextLine().split("\s+");
for (int row = 0; row < gridSize; row++) {
for (int column = 0; column < gridSize; column++) {
grid[row][column] = Integer.parseInt(currentLine[column]);
}
}
}
return grid;


}


The text file looks like this:



8
4 2 3 4 5
0 0 0 0 0 0 0 0
1 1 1 1 0 0 0 1
0 0 0 0 1 0 0 1
0 0 0 0 1 0 0 1
0 1 1 0 1 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0


I'm trying to start reading from the third row so I used .nextLine twice and I'm thinking that's where my problem lies but I'm not sure how to fix it










share|improve this question






















  • Only call nextLine() once per iteration? In your loop you check to see if theres another line, but then you call nextLine() three times (Two of them just throw away the results too)
    – GBlodgett
    Nov 21 '18 at 16:41












  • @GBlodgett when I do that it reads in the second line instead for the array
    – Rose
    Nov 21 '18 at 16:50






  • 1




    What do you mean? If you want to skip the first two lines, move the two calls to nextLine() outside of the loop
    – GBlodgett
    Nov 21 '18 at 16:52
















0














I keep getting this exception when I run my code:



Exception in thread "main" java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Unknown Source)
at BattleshipCheck.generateGrid(BattleshipCheck.java:60)


here's my code:



public static int generateGrid(Scanner in, int gridSize) {
int grid = new int[gridSize][gridSize];
while(in.hasNextLine()) {
in.nextLine();
in.nextLine();
String currentLine = in.nextLine().split("\s+");
for (int row = 0; row < gridSize; row++) {
for (int column = 0; column < gridSize; column++) {
grid[row][column] = Integer.parseInt(currentLine[column]);
}
}
}
return grid;


}


The text file looks like this:



8
4 2 3 4 5
0 0 0 0 0 0 0 0
1 1 1 1 0 0 0 1
0 0 0 0 1 0 0 1
0 0 0 0 1 0 0 1
0 1 1 0 1 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0


I'm trying to start reading from the third row so I used .nextLine twice and I'm thinking that's where my problem lies but I'm not sure how to fix it










share|improve this question






















  • Only call nextLine() once per iteration? In your loop you check to see if theres another line, but then you call nextLine() three times (Two of them just throw away the results too)
    – GBlodgett
    Nov 21 '18 at 16:41












  • @GBlodgett when I do that it reads in the second line instead for the array
    – Rose
    Nov 21 '18 at 16:50






  • 1




    What do you mean? If you want to skip the first two lines, move the two calls to nextLine() outside of the loop
    – GBlodgett
    Nov 21 '18 at 16:52














0












0








0







I keep getting this exception when I run my code:



Exception in thread "main" java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Unknown Source)
at BattleshipCheck.generateGrid(BattleshipCheck.java:60)


here's my code:



public static int generateGrid(Scanner in, int gridSize) {
int grid = new int[gridSize][gridSize];
while(in.hasNextLine()) {
in.nextLine();
in.nextLine();
String currentLine = in.nextLine().split("\s+");
for (int row = 0; row < gridSize; row++) {
for (int column = 0; column < gridSize; column++) {
grid[row][column] = Integer.parseInt(currentLine[column]);
}
}
}
return grid;


}


The text file looks like this:



8
4 2 3 4 5
0 0 0 0 0 0 0 0
1 1 1 1 0 0 0 1
0 0 0 0 1 0 0 1
0 0 0 0 1 0 0 1
0 1 1 0 1 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0


I'm trying to start reading from the third row so I used .nextLine twice and I'm thinking that's where my problem lies but I'm not sure how to fix it










share|improve this question













I keep getting this exception when I run my code:



Exception in thread "main" java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Unknown Source)
at BattleshipCheck.generateGrid(BattleshipCheck.java:60)


here's my code:



public static int generateGrid(Scanner in, int gridSize) {
int grid = new int[gridSize][gridSize];
while(in.hasNextLine()) {
in.nextLine();
in.nextLine();
String currentLine = in.nextLine().split("\s+");
for (int row = 0; row < gridSize; row++) {
for (int column = 0; column < gridSize; column++) {
grid[row][column] = Integer.parseInt(currentLine[column]);
}
}
}
return grid;


}


The text file looks like this:



8
4 2 3 4 5
0 0 0 0 0 0 0 0
1 1 1 1 0 0 0 1
0 0 0 0 1 0 0 1
0 0 0 0 1 0 0 1
0 1 1 0 1 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0


I'm trying to start reading from the third row so I used .nextLine twice and I'm thinking that's where my problem lies but I'm not sure how to fix it







java arrays java.util.scanner






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 16:39









Rose

31




31












  • Only call nextLine() once per iteration? In your loop you check to see if theres another line, but then you call nextLine() three times (Two of them just throw away the results too)
    – GBlodgett
    Nov 21 '18 at 16:41












  • @GBlodgett when I do that it reads in the second line instead for the array
    – Rose
    Nov 21 '18 at 16:50






  • 1




    What do you mean? If you want to skip the first two lines, move the two calls to nextLine() outside of the loop
    – GBlodgett
    Nov 21 '18 at 16:52


















  • Only call nextLine() once per iteration? In your loop you check to see if theres another line, but then you call nextLine() three times (Two of them just throw away the results too)
    – GBlodgett
    Nov 21 '18 at 16:41












  • @GBlodgett when I do that it reads in the second line instead for the array
    – Rose
    Nov 21 '18 at 16:50






  • 1




    What do you mean? If you want to skip the first two lines, move the two calls to nextLine() outside of the loop
    – GBlodgett
    Nov 21 '18 at 16:52
















Only call nextLine() once per iteration? In your loop you check to see if theres another line, but then you call nextLine() three times (Two of them just throw away the results too)
– GBlodgett
Nov 21 '18 at 16:41






Only call nextLine() once per iteration? In your loop you check to see if theres another line, but then you call nextLine() three times (Two of them just throw away the results too)
– GBlodgett
Nov 21 '18 at 16:41














@GBlodgett when I do that it reads in the second line instead for the array
– Rose
Nov 21 '18 at 16:50




@GBlodgett when I do that it reads in the second line instead for the array
– Rose
Nov 21 '18 at 16:50




1




1




What do you mean? If you want to skip the first two lines, move the two calls to nextLine() outside of the loop
– GBlodgett
Nov 21 '18 at 16:52




What do you mean? If you want to skip the first two lines, move the two calls to nextLine() outside of the loop
– GBlodgett
Nov 21 '18 at 16:52












3 Answers
3






active

oldest

votes


















0














If you want to start at the third row, call nextLine() before the while-Loop, not inside of it. Once you're at the last line, you try skipping two more lines, which dont exist. You'll also have to check whether the file actually has three lines before skipping two lines.



public static int generateGrid(Scanner in, int gridSize) {
int grid = new int[gridSize][gridSize];

if(in.hasNextLine())in.nextLine();
if(in.hasNextLine())in.nextLine();

while(in.hasNextLine()) {
String currentLine = in.nextLine().split("\s+");
for (int row = 0; row < gridSize; row++) {
for (int column = 0; column < gridSize; column++) {
grid[row][column] = Integer.parseInt(currentLine[column]);
}
}
}
return grid;


}






share|improve this answer





















  • @Rose Note that this will produce incorrect results. It will assign the last row to every row in the grid.
    – Johnny Mopp
    Nov 21 '18 at 17:14





















1














Looks like you are ignoring the data in the file. It tells you how many lines there are. Also, there is no need for a while loop.



public static int generateGrid(Scanner in) {
// Get gridSize from file
int gridSize = Integer.parseInt(in.nextLine());
int grid = new int[gridSize][gridSize];

// Not sure what 2nd line is for
// I'm guessing it is the number of ships followed
// by the size of each ship
in.nextLine();

for (int row = 0; row < gridSize; row++) {
// Move this here
String currentLine = in.nextLine().split("\s+");
for (int column = 0; column < gridSize; column++) {
grid[row][column] = Integer.parseInt(currentLine[column]);
}
}

return grid;
}


You still need to add error checking in case the file is malformed. Although I would leave as-is an let it throw an exception if there is a problem. Let the calling function deal with it.






share|improve this answer































    -1














    int i=0;
    public static int generateGrid(Scanner in, int gridSize) {
    int grid = new int[gridSize][gridSize];
    while(in.hasNextLine()) {
    if(++i < 2) {
    in.nextLine();
    continue;
    }
    String currentLine = in.nextLine().split("\s+");
    for (int row = 0; row < gridSize; row++) {
    for (int column = 0; column < gridSize; column++) {
    grid[row][column] = Integer.parseInt(currentLine[column]);
    }
    }
    }
    return grid;


    }





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416736%2fhow-can-i-fix-no-line-found-error-when-using-scanner-in-my-code%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      If you want to start at the third row, call nextLine() before the while-Loop, not inside of it. Once you're at the last line, you try skipping two more lines, which dont exist. You'll also have to check whether the file actually has three lines before skipping two lines.



      public static int generateGrid(Scanner in, int gridSize) {
      int grid = new int[gridSize][gridSize];

      if(in.hasNextLine())in.nextLine();
      if(in.hasNextLine())in.nextLine();

      while(in.hasNextLine()) {
      String currentLine = in.nextLine().split("\s+");
      for (int row = 0; row < gridSize; row++) {
      for (int column = 0; column < gridSize; column++) {
      grid[row][column] = Integer.parseInt(currentLine[column]);
      }
      }
      }
      return grid;


      }






      share|improve this answer





















      • @Rose Note that this will produce incorrect results. It will assign the last row to every row in the grid.
        – Johnny Mopp
        Nov 21 '18 at 17:14


















      0














      If you want to start at the third row, call nextLine() before the while-Loop, not inside of it. Once you're at the last line, you try skipping two more lines, which dont exist. You'll also have to check whether the file actually has three lines before skipping two lines.



      public static int generateGrid(Scanner in, int gridSize) {
      int grid = new int[gridSize][gridSize];

      if(in.hasNextLine())in.nextLine();
      if(in.hasNextLine())in.nextLine();

      while(in.hasNextLine()) {
      String currentLine = in.nextLine().split("\s+");
      for (int row = 0; row < gridSize; row++) {
      for (int column = 0; column < gridSize; column++) {
      grid[row][column] = Integer.parseInt(currentLine[column]);
      }
      }
      }
      return grid;


      }






      share|improve this answer





















      • @Rose Note that this will produce incorrect results. It will assign the last row to every row in the grid.
        – Johnny Mopp
        Nov 21 '18 at 17:14
















      0












      0








      0






      If you want to start at the third row, call nextLine() before the while-Loop, not inside of it. Once you're at the last line, you try skipping two more lines, which dont exist. You'll also have to check whether the file actually has three lines before skipping two lines.



      public static int generateGrid(Scanner in, int gridSize) {
      int grid = new int[gridSize][gridSize];

      if(in.hasNextLine())in.nextLine();
      if(in.hasNextLine())in.nextLine();

      while(in.hasNextLine()) {
      String currentLine = in.nextLine().split("\s+");
      for (int row = 0; row < gridSize; row++) {
      for (int column = 0; column < gridSize; column++) {
      grid[row][column] = Integer.parseInt(currentLine[column]);
      }
      }
      }
      return grid;


      }






      share|improve this answer












      If you want to start at the third row, call nextLine() before the while-Loop, not inside of it. Once you're at the last line, you try skipping two more lines, which dont exist. You'll also have to check whether the file actually has three lines before skipping two lines.



      public static int generateGrid(Scanner in, int gridSize) {
      int grid = new int[gridSize][gridSize];

      if(in.hasNextLine())in.nextLine();
      if(in.hasNextLine())in.nextLine();

      while(in.hasNextLine()) {
      String currentLine = in.nextLine().split("\s+");
      for (int row = 0; row < gridSize; row++) {
      for (int column = 0; column < gridSize; column++) {
      grid[row][column] = Integer.parseInt(currentLine[column]);
      }
      }
      }
      return grid;


      }







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 21 '18 at 16:45









      mejasper

      245




      245












      • @Rose Note that this will produce incorrect results. It will assign the last row to every row in the grid.
        – Johnny Mopp
        Nov 21 '18 at 17:14




















      • @Rose Note that this will produce incorrect results. It will assign the last row to every row in the grid.
        – Johnny Mopp
        Nov 21 '18 at 17:14


















      @Rose Note that this will produce incorrect results. It will assign the last row to every row in the grid.
      – Johnny Mopp
      Nov 21 '18 at 17:14






      @Rose Note that this will produce incorrect results. It will assign the last row to every row in the grid.
      – Johnny Mopp
      Nov 21 '18 at 17:14















      1














      Looks like you are ignoring the data in the file. It tells you how many lines there are. Also, there is no need for a while loop.



      public static int generateGrid(Scanner in) {
      // Get gridSize from file
      int gridSize = Integer.parseInt(in.nextLine());
      int grid = new int[gridSize][gridSize];

      // Not sure what 2nd line is for
      // I'm guessing it is the number of ships followed
      // by the size of each ship
      in.nextLine();

      for (int row = 0; row < gridSize; row++) {
      // Move this here
      String currentLine = in.nextLine().split("\s+");
      for (int column = 0; column < gridSize; column++) {
      grid[row][column] = Integer.parseInt(currentLine[column]);
      }
      }

      return grid;
      }


      You still need to add error checking in case the file is malformed. Although I would leave as-is an let it throw an exception if there is a problem. Let the calling function deal with it.






      share|improve this answer




























        1














        Looks like you are ignoring the data in the file. It tells you how many lines there are. Also, there is no need for a while loop.



        public static int generateGrid(Scanner in) {
        // Get gridSize from file
        int gridSize = Integer.parseInt(in.nextLine());
        int grid = new int[gridSize][gridSize];

        // Not sure what 2nd line is for
        // I'm guessing it is the number of ships followed
        // by the size of each ship
        in.nextLine();

        for (int row = 0; row < gridSize; row++) {
        // Move this here
        String currentLine = in.nextLine().split("\s+");
        for (int column = 0; column < gridSize; column++) {
        grid[row][column] = Integer.parseInt(currentLine[column]);
        }
        }

        return grid;
        }


        You still need to add error checking in case the file is malformed. Although I would leave as-is an let it throw an exception if there is a problem. Let the calling function deal with it.






        share|improve this answer


























          1












          1








          1






          Looks like you are ignoring the data in the file. It tells you how many lines there are. Also, there is no need for a while loop.



          public static int generateGrid(Scanner in) {
          // Get gridSize from file
          int gridSize = Integer.parseInt(in.nextLine());
          int grid = new int[gridSize][gridSize];

          // Not sure what 2nd line is for
          // I'm guessing it is the number of ships followed
          // by the size of each ship
          in.nextLine();

          for (int row = 0; row < gridSize; row++) {
          // Move this here
          String currentLine = in.nextLine().split("\s+");
          for (int column = 0; column < gridSize; column++) {
          grid[row][column] = Integer.parseInt(currentLine[column]);
          }
          }

          return grid;
          }


          You still need to add error checking in case the file is malformed. Although I would leave as-is an let it throw an exception if there is a problem. Let the calling function deal with it.






          share|improve this answer














          Looks like you are ignoring the data in the file. It tells you how many lines there are. Also, there is no need for a while loop.



          public static int generateGrid(Scanner in) {
          // Get gridSize from file
          int gridSize = Integer.parseInt(in.nextLine());
          int grid = new int[gridSize][gridSize];

          // Not sure what 2nd line is for
          // I'm guessing it is the number of ships followed
          // by the size of each ship
          in.nextLine();

          for (int row = 0; row < gridSize; row++) {
          // Move this here
          String currentLine = in.nextLine().split("\s+");
          for (int column = 0; column < gridSize; column++) {
          grid[row][column] = Integer.parseInt(currentLine[column]);
          }
          }

          return grid;
          }


          You still need to add error checking in case the file is malformed. Although I would leave as-is an let it throw an exception if there is a problem. Let the calling function deal with it.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 '18 at 17:17

























          answered Nov 21 '18 at 16:51









          Johnny Mopp

          6,77722344




          6,77722344























              -1














              int i=0;
              public static int generateGrid(Scanner in, int gridSize) {
              int grid = new int[gridSize][gridSize];
              while(in.hasNextLine()) {
              if(++i < 2) {
              in.nextLine();
              continue;
              }
              String currentLine = in.nextLine().split("\s+");
              for (int row = 0; row < gridSize; row++) {
              for (int column = 0; column < gridSize; column++) {
              grid[row][column] = Integer.parseInt(currentLine[column]);
              }
              }
              }
              return grid;


              }





              share|improve this answer


























                -1














                int i=0;
                public static int generateGrid(Scanner in, int gridSize) {
                int grid = new int[gridSize][gridSize];
                while(in.hasNextLine()) {
                if(++i < 2) {
                in.nextLine();
                continue;
                }
                String currentLine = in.nextLine().split("\s+");
                for (int row = 0; row < gridSize; row++) {
                for (int column = 0; column < gridSize; column++) {
                grid[row][column] = Integer.parseInt(currentLine[column]);
                }
                }
                }
                return grid;


                }





                share|improve this answer
























                  -1












                  -1








                  -1






                  int i=0;
                  public static int generateGrid(Scanner in, int gridSize) {
                  int grid = new int[gridSize][gridSize];
                  while(in.hasNextLine()) {
                  if(++i < 2) {
                  in.nextLine();
                  continue;
                  }
                  String currentLine = in.nextLine().split("\s+");
                  for (int row = 0; row < gridSize; row++) {
                  for (int column = 0; column < gridSize; column++) {
                  grid[row][column] = Integer.parseInt(currentLine[column]);
                  }
                  }
                  }
                  return grid;


                  }





                  share|improve this answer












                  int i=0;
                  public static int generateGrid(Scanner in, int gridSize) {
                  int grid = new int[gridSize][gridSize];
                  while(in.hasNextLine()) {
                  if(++i < 2) {
                  in.nextLine();
                  continue;
                  }
                  String currentLine = in.nextLine().split("\s+");
                  for (int row = 0; row < gridSize; row++) {
                  for (int column = 0; column < gridSize; column++) {
                  grid[row][column] = Integer.parseInt(currentLine[column]);
                  }
                  }
                  }
                  return grid;


                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 '18 at 16:43









                  Jignesh M. Khatri

                  501411




                  501411






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416736%2fhow-can-i-fix-no-line-found-error-when-using-scanner-in-my-code%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      404 Error Contact Form 7 ajax form submitting

                      How to know if a Active Directory user can login interactively

                      Refactoring coordinates for Minecraft Pi buildings written in Python