Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can take out this part of the code in the loop:</p> <pre><code>if(line.trim().length() ==1) { //set value of first line in file to be numOfCities numOfCities = Integer.parseInt(line); System.out.println("There are"+ " " + numOfCities+ " " + "Number of cities"); } else { </code></pre> <p>because what you can do is above the while loop put this:</p> <pre><code>numOfCities = Integer.parseInt(br.readLine()); System.out.println("There are"+ " " + numOfCities+ " " + "Number of cities"); </code></pre> <p>and what that will do is read the first line, set it to the number of cities, then go into the while loop and start at the SECOND line instead of the first. So your whole code will be:</p> <pre><code>import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class TSM { private static int numOfCities; private static int[][] arr = new int[5][5]; public static void main(String[] args) throws NumberFormatException, IOException{ numOfCities = 0; BufferedReader br = new BufferedReader(new FileReader("/Users/sly/Desktop/tsp.txt")); String line = " "; String [] temp; numOfCities = Integer.parseInt(br.readLine()); //Reads first line then starts at while loop in second System.out.println("There are"+ " " + numOfCities+ " " + "Number of cities"); while ((line = br.readLine())!= null){ temp = line.split(" "); //split spaces for(int i = 0; i&lt;arr.length; i++) { for (int j = 0; j&lt;arr.length; j++) { arr[i][j] = Integer.parseInt(temp[i]); } } } printArray(); } public static void printArray () { for (int i =0; i &lt;arr.length; i++) { for (int j = 0; j &lt; arr.length; j++) { System.out.print(arr[i][j]); } System.out.println(""); } } } </code></pre> <p>Hope it helps!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload