Seeing that things came out "simple" I decided to complicate things a little more and make a program that asks the user for 3 countries and then 3 cities from them, to store them in a table and then show the table. The code I have come up with is the following:
public class Ejer3 {
public static void main(String[] args) {
Scanner teclado = new Scanner(System.in);
System.out.println("---------------------------------------------");
System.out.println("\t Aprendiendo Geografia");
System.out.println("---------------------------------------------");
String tabla[][] = new String[3][4];
String p1, p2, p3, ciudad;
System.out.print("Introduce el primer pais: ");
p1 = teclado.nextLine();
tabla[0][0] = p1;
System.out.print("Introduce el segundo pais: ");
p2 = teclado.nextLine();
tabla[1][0] = p2;
System.out.print("Introduce el tercer pais: ");
p3 = teclado.nextLine();
tabla[2][0] = p3;
System.out.println("\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 1; j++) {
System.out.print("Introduce la ciudad de " + p1 + ": ");
ciudad = teclado.nextLine();
tabla[i][j] = ciudad;
}
}
System.out.println("\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 1; j++) {
System.out.print("Introduce la ciudad de " + p2 + ": ");
ciudad = teclado.nextLine();
tabla[i][j] = ciudad;
}
}
System.out.println("\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 1; j++) {
System.out.print("Introduce la ciudad de " + p3 + ": ");
ciudad = teclado.nextLine();
tabla[i][j] = ciudad;
}
}
for (int i = 0; i < tabla.length; i++) {
for (int j = 0; j < tabla[i].length; j++) {
System.out.println(tabla[j][j]);
}
}
}
}
The fact is that it lets me enter everything by keyboard correctly as you can see:
The doubt or the error that I want to correct is, correctly store the countries and cities in a table, since with the code above, it allows me to enter everything correctly, countries and cities, but when displaying the table it gives an error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at arraysN.Ejer3.main(Ejer3.java:67)
C:\Users\jorge\AppData\Local\NetBeans\Cache\8.2rc\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 33 seconds)
You have a few things you could change in your code. I leave you the code first and then proceed to the explanation:
From what I understand, you plan to store the country in the first column, and then, in each remaining column, store its cities:
The first loop to store the countries you have them well.
Then, instead of doing 3 loops for each country, just one is enough, printing the country name with
tabla[i][0]
. You know that as i progresses from 0 to 3 you will access each row for each country, and that at position 0, you will always have the name of the country you are storing.Then, to save the data, you make j go from 1 ( since 0 is the country name ) to 3, storing the 3 cities. At this point, it would be better to go from
j=1
toj<tabla[i].lenght
, so that if you decide to expand the number of cities to store, you don't have to manually modify the code, keep that in mind .Then, at print time, you printed
tabla[j][j]
instead oftabla [i][j]
.Any questions let me know, greetings.
No se por qué motivo no me deja editar la respuesta anterior (me tira error de sangrado de código todo el rato por mas que esté correcto, si alguien me la modifica para agregar este código al final de la respuesta anterior, borraré esta, gracias).
I leave you the code with the suggestions I make in the comments:
That way, no matter what dimensions you declare for the table, your code will still work.