Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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();
- }
- }
- habitantes[f] = teclado.nextInt();
- paises[f] = teclado.nextLine();
- 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();
- teclado.nextLine() // Esto quitará del buffer el cambio de línea
- }
- 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] = Integer.parseInt(teclado.nextLine()); //Esto extrae el número como String pero luego lo parsea como `Integer`
- }
- habitantes[f] = teclado.nextInt();
- habitantes[f] = Integer.parseInt(teclado.nextLine());
- package ayuda;
- import java.util.Scanner;
- public class Paises_habitantes {
- public static void main(String[] args) {
- Scanner c = new Scanner(System.in);
- String [] paises = new String[5];
- int [] habitantes = new int[5];
- System.out.println("carga de paises y cantidad de habitantes");
- for (int f=0;f<5;f++){
- System.out.print("Ingrese nombre del pais: ");
- paises[f]=c.nextLine();
- System.out.print("Ingrese la cantidad de haitantes: ");
- habitantes[f]=c.nextInt();
- c.nextLine(); //se utiliza para limpiar el buffer del teclado por variables int aqui estaba tu error
- }
- for(int x=0;x<5;x++){
- System.out.println("Mostrando resultados de captura: ");
- System.out.println("Pais: "+paises[x]+" Numero de habitantes: "+habitantes[x]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement