The issue is that I want to enter a String that can contain space (eg: United States) and for that reason I use the nextLine, but also in that same method I enter data type Int. The problem is that being inside a for in the second income is screwed by the issue of the nextLine.
Enter country name: United States Enter number of inhabitants: 300
And then this happens:
Enter country name: Enter number of inhabitants:
I leave you the code, I had thought of a possible solution (to do the loads separately but it definitely doesn't look very good to ask 2 things separately). PS: Suppose the vector is N quantity instead of 5.
import java.util.Scanner;
public class Problema {
private Scanner teclado;
private String[] paises;
private int[] habitantes;
public void cargar() {
teclado = new Scanner(System.in);
paises = new String[5];
habitantes = new int[5];
System.out.println("Carga de paises y cant de habitantes!");
for(int f = 0; f < paises.length; f++) {
System.out.print("Ingrese nombre de país: ");
paises[f] = teclado.nextLine();
//teclado.nextLine();
//paises[f] = teclado.nextLine();
System.out.print("Ingrese la cantidad de habitantes: ");
habitantes[f] = teclado.nextInt();
}
}
public void ordenarAlfabeticamente() {
for(int k = 0; k < paises.length-1; k++) {
for(int f = 0; f < paises.length-1-k; f++) {
if(paises[f].compareTo(paises[f+1]) > 0) {
String auxpaises;
auxpaises = paises[f];
paises[f] = paises[f+1];
paises[f+1] = auxpaises;
int auxhabitantes;
auxhabitantes = habitantes[f];
habitantes[f] = habitantes[f+1];
habitantes[f+1] = auxhabitantes;
}
}
}
}
public void ordenarHabitantes() {
for(int k = 0; k < habitantes.length-1; k++) {
for(int f = 0; f < habitantes.length-1-k; f++) {
if(habitantes[f] < habitantes[f+1]) {
int auxhabitantes2;
auxhabitantes2 = habitantes[f];
habitantes[f] = habitantes[f+1];
habitantes[f+1] = auxhabitantes2;
String auxpaises2;
auxpaises2 = paises[f];
paises[f] = paises[f+1];
paises[f+1] = auxpaises2;
}
}
}
}
public void imprimirA() {
System.out.println("Nombres de paises en orden alfabeto y sus habitantes: ");
for(int f = 0; f < paises.length; f++) {
System.out.println(paises[f] + " - " + habitantes[f]);
}
}
public void imprimirB() {
System.out.println("Nombres de paises y habitantes ordenado de mayor a menor: ");
for(int f = 0; f < habitantes.length; f++) {
System.out.println(paises[f] + " - " + habitantes[f]);
}
}
public static void main(String[] ar) {
Problema prob = new Problema();
prob.cargar();
prob.ordenarAlfabeticamente();
prob.imprimirA();
prob.ordenarHabitantes();
prob.imprimirB();
}
}
The problem is that:
It doesn't buffer
teclado
the line feed so the call to:Find as first character the line break and take it as an empty line
There are two ways to fix it:
Consuming the line feed with an
teclado.nextLine()
afterteclado.nextInt();
Reading the number as
String
and then parsing it toInteger
It should be noted that in both cases it is probably better to include the reading of the number inside a
try catch
since both will fail if a string is entered that does not represent a number.What is failing is the reading of integers since you use the same scanner, what you can do is use the same nextLine(), and transform it to an integer.
change this line
for this line
Well I solved the problem for you. What happened is that when entering values in the inhabitants variable you did not clear the buffer and the next field, which was text, recognized it as an invalid field. Then I leave you the code and the results of the console, I hope it will help you, you only have to order the data to make it look more aesthetic, greetings.
Console results: