elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Los 10 CVE más críticos (peligrosos) de 2020


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  arreglos de cadenas
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: arreglos de cadenas  (Leído 4,085 veces)
sauce19

Desconectado Desconectado

Mensajes: 7


Ver Perfil
arreglos de cadenas
« 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. }


« Última modificación: 5 Agosto 2011, 11:10 am por Debci » En línea

bengy


Desconectado Desconectado

Mensajes: 501


mis virtudes y defectos son inseparables


Ver Perfil WWW
Re: arreglos de cadenas
« Respuesta #1 en: 4 Agosto 2011, 02:17 am »

quieres leer obligatoriamente de un texto??????
 


En línea

Valkyr


Desconectado Desconectado

Mensajes: 646


Divide y vencerás


Ver Perfil
Re: arreglos de cadenas
« Respuesta #2 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.
En línea

Gallu

Desconectado Desconectado

Mensajes: 247



Ver Perfil
Re: arreglos de cadenas
« Respuesta #3 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.  
En línea

Nadie alcanza la meta con un solo intento, ni perfecciona la vida con una sola rectificación, ni alcanza altura con un solo vuelo.
sauce19

Desconectado Desconectado

Mensajes: 7


Ver Perfil
Re: arreglos de cadenas
« Respuesta #4 en: 4 Agosto 2011, 17:56 pm »

ok, eso m va a servir de mucho, muchas gracias!!!!! ;D
En línea

Debci
Wiki

Desconectado Desconectado

Mensajes: 2.021


Actualizate o muere!


Ver Perfil WWW
Re: arreglos de cadenas
« Respuesta #5 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
En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
con arreglos
Java
artang 0 2,146 Último mensaje 31 Marzo 2009, 04:54 am
por artang
arreglos en c
Programación C/C++
el futuro 5 3,217 Último mensaje 21 Mayo 2010, 02:08 am
por @synthesize
Almacenar cadenas en arreglos de bytes
Java
m@o_614 1 4,196 Último mensaje 13 Enero 2015, 02:09 am
por madara1412
Arreglos y Cadenas de caracteres AYUDA
Programación C/C++
samantika 2 1,775 Último mensaje 5 Septiembre 2015, 04:28 am
por d91
Arreglos de cadenas en C
Programación C/C++
024mc 7 3,377 Último mensaje 4 Junio 2020, 04:14 am
por michellcrh
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines