Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Problema {
  4. private Scanner teclado;
  5. private String[] paises;
  6. private int[] habitantes;
  7.  
  8. public void cargar() {
  9. teclado = new Scanner(System.in);
  10. paises = new String[5];
  11. habitantes = new int[5];
  12. System.out.println("Carga de paises y cant de habitantes!");
  13.  
  14. for(int f = 0; f < paises.length; f++) {
  15. System.out.print("Ingrese nombre de país: ");
  16. paises[f] = teclado.nextLine();
  17. //teclado.nextLine();
  18. //paises[f] = teclado.nextLine();
  19. System.out.print("Ingrese la cantidad de habitantes: ");
  20. habitantes[f] = teclado.nextInt();
  21. }
  22. }
  23.  
  24. public void ordenarAlfabeticamente() {
  25. for(int k = 0; k < paises.length-1; k++) {
  26. for(int f = 0; f < paises.length-1-k; f++) {
  27. if(paises[f].compareTo(paises[f+1]) > 0) {
  28. String auxpaises;
  29. auxpaises = paises[f];
  30. paises[f] = paises[f+1];
  31. paises[f+1] = auxpaises;
  32. int auxhabitantes;
  33. auxhabitantes = habitantes[f];
  34. habitantes[f] = habitantes[f+1];
  35. habitantes[f+1] = auxhabitantes;
  36. }
  37. }
  38. }
  39. }
  40.  
  41. public void ordenarHabitantes() {
  42. for(int k = 0; k < habitantes.length-1; k++) {
  43. for(int f = 0; f < habitantes.length-1-k; f++) {
  44. if(habitantes[f] < habitantes[f+1]) {
  45. int auxhabitantes2;
  46. auxhabitantes2 = habitantes[f];
  47. habitantes[f] = habitantes[f+1];
  48. habitantes[f+1] = auxhabitantes2;
  49. String auxpaises2;
  50. auxpaises2 = paises[f];
  51. paises[f] = paises[f+1];
  52. paises[f+1] = auxpaises2;
  53. }
  54. }
  55. }
  56. }
  57.  
  58. public void imprimirA() {
  59. System.out.println("Nombres de paises en orden alfabeto y sus habitantes: ");
  60. for(int f = 0; f < paises.length; f++) {
  61. System.out.println(paises[f] + " - " + habitantes[f]);
  62. }
  63. }
  64.  
  65. public void imprimirB() {
  66. System.out.println("Nombres de paises y habitantes ordenado de mayor a menor: ");
  67. for(int f = 0; f < habitantes.length; f++) {
  68. System.out.println(paises[f] + " - " + habitantes[f]);
  69. }
  70. }
  71.  
  72. public static void main(String[] ar) {
  73. Problema prob = new Problema();
  74. prob.cargar();
  75. prob.ordenarAlfabeticamente();
  76. prob.imprimirA();
  77. prob.ordenarHabitantes();
  78. prob.imprimirB();
  79. }
  80. }
  81.  
  82. habitantes[f] = teclado.nextInt();
  83.  
  84. paises[f] = teclado.nextLine();
  85.  
  86. for(int f = 0; f < paises.length; f++) {
  87. System.out.print("Ingrese nombre de país: ");
  88. paises[f] = teclado.nextLine();
  89. //teclado.nextLine();
  90. //paises[f] = teclado.nextLine();
  91. System.out.print("Ingrese la cantidad de habitantes: ");
  92. habitantes[f] = teclado.nextInt();
  93. teclado.nextLine() // Esto quitará del buffer el cambio de línea
  94. }
  95.  
  96. for(int f = 0; f < paises.length; f++) {
  97. System.out.print("Ingrese nombre de país: ");
  98. paises[f] = teclado.nextLine();
  99. //teclado.nextLine();
  100. //paises[f] = teclado.nextLine();
  101. System.out.print("Ingrese la cantidad de habitantes: ");
  102. habitantes[f] = Integer.parseInt(teclado.nextLine()); //Esto extrae el número como String pero luego lo parsea como `Integer`
  103. }
  104.  
  105. habitantes[f] = teclado.nextInt();
  106.  
  107. habitantes[f] = Integer.parseInt(teclado.nextLine());
  108.  
  109. package ayuda;
  110. import java.util.Scanner;
  111.  
  112. public class Paises_habitantes {
  113.  
  114. public static void main(String[] args) {
  115. Scanner c = new Scanner(System.in);
  116.  
  117. String [] paises = new String[5];
  118. int [] habitantes = new int[5];
  119.  
  120. System.out.println("carga de paises y cantidad de habitantes");
  121.  
  122. for (int f=0;f<5;f++){
  123. System.out.print("Ingrese nombre del pais: ");
  124. paises[f]=c.nextLine();
  125. System.out.print("Ingrese la cantidad de haitantes: ");
  126. habitantes[f]=c.nextInt();
  127. c.nextLine(); //se utiliza para limpiar el buffer del teclado por variables int aqui estaba tu error
  128. }
  129. for(int x=0;x<5;x++){
  130. System.out.println("Mostrando resultados de captura: ");
  131. System.out.println("Pais: "+paises[x]+" Numero de habitantes: "+habitantes[x]);
  132. }
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement