I need some help please in Java! 1. Given: int[][] values = new int[4][5] Write
ID: 3939207 • Letter: I
Question
I need some help please in Java!
1. Given: int[][] values = new int[4][5]
Write a nested loop to set values as follows:
[0] [1] [2] [3] [4]
[0] 1 2 3 4 5
[1] 1 2 3 4 5
[2] 1 2 3 4 5
[3] 1 2 3 4 5
Given the following array of 5 rows and 5 columns, which contains the
distances between cities:
[0] [1] [2] [3] [4]
Albany Boston Hartford NY Phila
[0] Albany 0 171 115 141 240
[1] Boston 171 0 103 194 333
[2] Hartford 115 103 0 120 235
[3] NY 141 194 120 0 104
[4] Phila 240 333 235 104 0
Note that the city names are not in the array; the array contains
the numeric entries only, which give the distance between the two
cities represented by the row and column.
2. Write the statements to initialize an array distance with the
mileage data given above.
3. Write the statements to print the following menu, read in two
city numbers, and print the distance between the two cities:
To determine the mileage between cities, enter the numbers of
two cities from the following list:
1: Albany 4: NY
2: Boston 5: Phila
3: Hartford
Enter your city numbers:
** Test Case: Distance between Albany and NY && Distance between Hartford and Phila
Explanation / Answer
import java.util.Scanner;
Here is the fixed code; Explanation below:
It looks like you're not printing out the mileage at the end. Try changing the last line of your code to:
Change the code like,
Basically, to read keyboard input from the user, you need a Scanner object:
The errors you were seeing were likely due to the fact you were trying to call the method nextInt() from a null object reference (i.e. pointer to nothing):
Finally, don't forget to add mileage to the string that you get at the end: