Foro de elhacker.net

Programación => Java => Mensaje iniciado por: sauce19 en 3 Agosto 2011, 23:23 pm



Título: arreglos de cadenas
Publicado por: sauce19 en 3 Agosto 2011, 23:23 pm
hola a todos! soy nueva en la programacion en java y sigo sin manejar arreglos de cadena; debo mostrar 10 veces la siguiente informacion: nombre del cliente,fecha de ingreso,fecha de retiro y  tipo de habitacion ocupada. Yo tenia en mente guardar en un vector los nombres de los 10 clientes, en otro vector las 10fechas de ingreso, en otro las 10fechas de retiro y las 10tipos de hab ocupada. aqui sta lo q he hecho hasta ahora, si alguien podria ayudarme seria de mucha ayuda. gracias!

Código
  1. public static String nomcliente ()throws IOException{
  2. String nombre;
  3. System.out.println("Introduzca primer nombre y primer apellido del cliente:");
  4. System.out.flush();
  5. nombre=en.readLine();
  6. return nombre;
  7. }
  8.  
  9. public static String fechaingreso ()throws IOException{
  10. String fi;
  11.  
  12. System.out.println("Introduzca la fecha de ingreso del cliente:");
  13. System.out.flush();
  14. fi=en.readLine();
  15. return fi;
  16. }
  17.  
  18. public static String fecharetiro ()throws IOException{
  19. String fr;
  20. System.out.println("Introduzca la fecha de retiro del cliente:");
  21. System.out.flush();
  22. fr=en.readLine();
  23. return fr;
  24. }
  25.  
  26. public static String tiphab ()throws IOException{
  27. String hab;
  28. System.out.println("Introduzca el tipo de habitacion:");
  29. System.out.flush();
  30. hab=en.readLine();
  31. return hab;
  32. }


Título: Re: arreglos de cadenas
Publicado por: bengy en 4 Agosto 2011, 02:17 am
quieres leer obligatoriamente de un texto??????
 


Título: Re: arreglos de cadenas
Publicado por: Valkyr en 4 Agosto 2011, 13:52 pm
Sí lo vas a hacer con arrays de cadenas no sería complicado.

Tú tienes ahora mismo los métodos que preguntan al usuario la información, luego en el método main tan solo debes ir llamandolos y ya está.

Yo quizás en lugar de hacer un método para cada cosa lo habría hecho todo en el mismo main ya que así no tendría que estar declarando constantemente un nuevo BufferedReader, o lo pasaría como parámetro.

Podrías hacer algo así:

Código
  1. public static String nomcliente (BufferedReader en, int numero)throws IOException{
  2.    String nombre;
  3.    System.out.println("Introduzca el nombre y primer apellido del cliente numero " + numero + ":");
  4.    System.out.flush();
  5.    nombre=en.readLine();
  6.    return nombre;
  7. }
  8.  
  9. public static String fechaingreso (BufferedReader en, int numero)throws IOException{
  10.    String fi;
  11.    System.out.println("Introduzca la fecha de ingreso del cliente numero " + numero + ":");
  12.    System.out.flush();
  13.    fi=en.readLine();
  14.    return fi;
  15. }
  16.  
  17. public static String fecharetiro (BufferedReader en, int numero)throws IOException{
  18.    String fr;
  19.    System.out.println("Introduzca la fecha de retiro del cliente numero " + numero + ":");
  20.    System.out.flush();
  21.    fr=en.readLine();
  22.    return fr;
  23. }
  24.  
  25. public static String tiphab (BufferedReader en, int numero)throws IOException{
  26.    String hab;
  27.    System.out.println("Introduzca el tipo de habitacion numero " + numero + ":");
  28.    System.out.flush();
  29.    hab=en.readLine();
  30.    return hab;
  31. }
  32.  
  33. public static void main(String[] args){
  34.    //Declaramos el buffer de lectura para leer de teclado
  35.  
  36.    //Declaramos los arrays que contendran la informacion introducida por el usuario
  37.    String[] clientes = new String[10];
  38.    String[] ingreso = new String[10];
  39.    String[] retiro = new String[10];
  40.    String[] tipoHabitacion = new String[10];
  41.  
  42.    //Insertamos en los arrays la informacion devuelta por los metodos
  43.    for(int i = 0; i<10; i++){
  44.        clientes[i] = nomcliente(br, i+1); //Aqui ponemos i+1 para que muestre el primer cliente con numero 1 en     lugar de 0
  45.        ingreso[i] = fechaingreso(br, i+1);
  46.        retiro[i] = fecharetiro(br, i+1);
  47.        tipoHabitacion[i] = tiphab(br, i+1);
  48.    }
  49.  
  50.    //Ahora si quieres puedes recorrer de nuevo los arrays y mostrar la informacion:
  51.  
  52.    for(int i = 0; i<10; i++){
  53.        System.out.println("Cliente: " + clientes[i]);
  54.        System.out.println("Fecha ingreos: " + ingreso[i]);
  55.        System.out.println("Fecha retiro: " + retiro[i]);
  56.        System.out.println("Tipo habitacion: " + tipoHabitacion[i]);
  57.        System.out.println(); //Para que deje una linea en blanco entre cada uno.
  58.    }
  59. }

Saludos.


Título: Re: arreglos de cadenas
Publicado por: Gallu en 4 Agosto 2011, 15:31 pm
No olvides que java es orientado a objetos, acostumbrate plantear las soluciones de esa manera

Así:

Código
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5.  
  6. class Cliente{
  7. private BufferedReader en = null;
  8. //contador de objetos creados
  9. private static int numeroDeCliente = 0;
  10. private String nombre;    
  11. private String fechaIngreso;    
  12. private String fechaRetiro;    
  13. private String tipoHabitacion;    
  14.  
  15. public Cliente(BufferedReader en){
  16. //has de agregar validación para el objeto en
  17. this.en = en;
  18. //por cada objeto creado sumamos una al contador
  19. numeroDeCliente++;
  20. }
  21.  
  22. public void leerCliente()throws IOException{
  23. System.out.println("Introduzca el nombre y primer apellido del cliente numero " + Cliente.numeroDeCliente + ":");
  24. System.out.flush();    
  25. this.setNombre(en.readLine());
  26. }
  27.  
  28. public void leerFechaIngreso()throws IOException{  
  29. System.out.println("Introduzca la fecha de ingreso del cliente numero " + Cliente.numeroDeCliente + ":");  
  30. System.out.flush();  
  31. this.setFechaIngreso(en.readLine());  
  32. }
  33.  
  34.  
  35. public void leerFechaRetiro()throws IOException{  
  36. System.out.println("Introduzca la fecha de retiro del cliente numero " + Cliente.numeroDeCliente + ":");  
  37. System.out.flush();  
  38. this.setFechaRetiro(en.readLine());    
  39. }
  40.  
  41. public void leerTipoHabitacion()throws IOException{  
  42. System.out.println("Introduzca el tipo de habitacion numero " + Cliente.numeroDeCliente + ":");  
  43. System.out.flush();    
  44. this.setTipoHabitacion(en.readLine());
  45. }
  46.  
  47. public void leerDatosCliente()throws IOException{
  48. leerCliente();
  49. leerFechaIngreso();
  50. leerFechaRetiro();
  51. leerTipoHabitacion();
  52. }
  53.  
  54. /**
  55. * Retorna la información que nos interesa del cliente
  56. */
  57. public String toString(){
  58. StringBuffer buffer = new StringBuffer();
  59. buffer.append("Cliente :"+ this.getNombre());
  60. buffer.append("\n");
  61.  
  62. buffer.append("FechaIngreso :"+ this.getFechaIngreso());
  63. buffer.append("\n");
  64.  
  65. buffer.append("Fecha Retiro :"+ this.getFechaRetiro());
  66. buffer.append("\n");
  67.  
  68. buffer.append("Tipo Habitación :"+ this.getTipoHabitacion());
  69. buffer.append("\n");
  70.  
  71. return buffer.toString();
  72. }
  73.  
  74.  
  75.  
  76. public String getFechaIngreso() {
  77. return fechaIngreso;
  78. }
  79.  
  80. public void setFechaIngreso(String fechaIngreso) {
  81. this.fechaIngreso = fechaIngreso;
  82. }
  83.  
  84. public String getFechaRetiro() {
  85. return fechaRetiro;
  86. }
  87.  
  88. public void setFechaRetiro(String fechaRetiro) {
  89. this.fechaRetiro = fechaRetiro;
  90. }
  91.  
  92. public String getNombre() {
  93. return nombre;
  94. }
  95.  
  96. public void setNombre(String nombre) {
  97. this.nombre = nombre;
  98. }
  99.  
  100. public String getTipoHabitacion() {
  101. return tipoHabitacion;
  102. }
  103.  
  104. public void setTipoHabitacion(String tipoHabitacion) {
  105. this.tipoHabitacion = tipoHabitacion;
  106. }
  107. }
  108.  
  109. public class prueba {
  110.  
  111. public static void main(String[] args){  
  112. //Declaramos el buffer de lectura para leer de teclado    
  113.  
  114. Cliente [] clientes = new Cliente[10];
  115.  
  116. try{
  117. //por cada cliente en el array , utilizamos el bucle for mejorado
  118. for(Cliente cli : clientes){  
  119. cli = new Cliente(br);
  120. cli.leerDatosCliente();
  121. System.out.println(cli.toString());  
  122. }  
  123.  
  124.  
  125. }catch(IOException error){
  126. error.printStackTrace();
  127. }
  128.  
  129. }
  130. }
  131.  
  132.  


Título: Re: arreglos de cadenas
Publicado por: sauce19 en 4 Agosto 2011, 17:56 pm
ok, eso m va a servir de mucho, muchas gracias!!!!! ;D


Título: Re: arreglos de cadenas
Publicado por: Debci en 5 Agosto 2011, 11:11 am
Como ya te han contestado, me limito a RECORDAR que si es posible, uses, por favor las tags de código Java GeSHi.

Un saludo