elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.
 
Inicio Ayuda Buscar Ingresar Registrarse
28 Mayo 2012, 01:24  


Tema destacado: Únete al Grupo Steam elhacker.NET

+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java (Moderadores: Debci, Leyer)
| | | |-+  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 860 veces)
sauce19

Desconectado Desconectado

Mensajes: 7


Ver Perfil
arreglos de cadenas
« en: 3 Agosto 2011, 23:23 »

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
public static String nomcliente ()throws IOException{
BufferedReader en=new BufferedReader(new InputStreamReader (System.in));
String nombre;
System.out.println("Introduzca primer nombre y primer apellido del cliente:");
System.out.flush();
nombre=en.readLine();
return nombre;
}
 
public static String fechaingreso ()throws IOException{
BufferedReader en=new BufferedReader(new InputStreamReader (System.in));
String fi;
 
System.out.println("Introduzca la fecha de ingreso del cliente:");
System.out.flush();
fi=en.readLine();
return fi;
}
 
public static String fecharetiro ()throws IOException{
BufferedReader en=new BufferedReader(new InputStreamReader (System.in));
String fr;
System.out.println("Introduzca la fecha de retiro del cliente:");
System.out.flush();
fr=en.readLine();
return fr;
}
 
public static String tiphab ()throws IOException{
BufferedReader en=new BufferedReader(new InputStreamReader (System.in));
String hab;
System.out.println("Introduzca el tipo de habitacion:");
System.out.flush();
hab=en.readLine();
return hab;
}


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

Desconectado Desconectado

Mensajes: 108



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

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


En línea

lo importante no es si haz caido si no es si puedes  levantarse
Valkyr


Desconectado Desconectado

Mensajes: 632


Divide y vencerás


Ver Perfil
Re: arreglos de cadenas
« Respuesta #2 en: 4 Agosto 2011, 13:52 »

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
public static String nomcliente (BufferedReader en, int numero)throws IOException{
   String nombre;
   System.out.println("Introduzca el nombre y primer apellido del cliente numero " + numero + ":");
   System.out.flush();
   nombre=en.readLine();
   return nombre;
}
 
public static String fechaingreso (BufferedReader en, int numero)throws IOException{
   String fi;
   System.out.println("Introduzca la fecha de ingreso del cliente numero " + numero + ":");
   System.out.flush();
   fi=en.readLine();
   return fi;
}
 
public static String fecharetiro (BufferedReader en, int numero)throws IOException{
   String fr;
   System.out.println("Introduzca la fecha de retiro del cliente numero " + numero + ":");
   System.out.flush();
   fr=en.readLine();
   return fr;
}
 
public static String tiphab (BufferedReader en, int numero)throws IOException{
   String hab;
   System.out.println("Introduzca el tipo de habitacion numero " + numero + ":");
   System.out.flush();
   hab=en.readLine();
   return hab;
}
 
public static void main(String[] args){
   //Declaramos el buffer de lectura para leer de teclado
   BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 
   //Declaramos los arrays que contendran la informacion introducida por el usuario
   String[] clientes = new String[10];
   String[] ingreso = new String[10];
   String[] retiro = new String[10];
   String[] tipoHabitacion = new String[10];
 
   //Insertamos en los arrays la informacion devuelta por los metodos
   for(int i = 0; i<10; i++){
       clientes[i] = nomcliente(br, i+1); //Aqui ponemos i+1 para que muestre el primer cliente con numero 1 en     lugar de 0
       ingreso[i] = fechaingreso(br, i+1);
       retiro[i] = fecharetiro(br, i+1);
       tipoHabitacion[i] = tiphab(br, i+1);
   }
 
   //Ahora si quieres puedes recorrer de nuevo los arrays y mostrar la informacion:
 
   for(int i = 0; i<10; i++){
       System.out.println("Cliente: " + clientes[i]);
       System.out.println("Fecha ingreos: " + ingreso[i]);
       System.out.println("Fecha retiro: " + retiro[i]);
       System.out.println("Tipo habitacion: " + tipoHabitacion[i]);
       System.out.println(); //Para que deje una linea en blanco entre cada uno.
   }
}

Saludos.
En línea

Didacso

Desconectado Desconectado

Mensajes: 202



Ver Perfil
Re: arreglos de cadenas
« Respuesta #3 en: 4 Agosto 2011, 15:31 »

No olvides que java es orientado a objetos, acostumbrate plantear las soluciones de esa manera

Así:

Código
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
 
class Cliente{
private BufferedReader en = null;
//contador de objetos creados
private static int numeroDeCliente = 0;
private String nombre;    
private String fechaIngreso;    
private String fechaRetiro;    
private String tipoHabitacion;    
 
public Cliente(BufferedReader en){
//has de agregar validación para el objeto en
this.en = en;
//por cada objeto creado sumamos una al contador
numeroDeCliente++;
}
 
public void leerCliente()throws IOException{
System.out.println("Introduzca el nombre y primer apellido del cliente numero " + Cliente.numeroDeCliente + ":");
System.out.flush();    
this.setNombre(en.readLine());
}
 
public void leerFechaIngreso()throws IOException{  
System.out.println("Introduzca la fecha de ingreso del cliente numero " + Cliente.numeroDeCliente + ":");  
System.out.flush();  
this.setFechaIngreso(en.readLine());  
}
 
 
public void leerFechaRetiro()throws IOException{  
System.out.println("Introduzca la fecha de retiro del cliente numero " + Cliente.numeroDeCliente + ":");  
System.out.flush();  
this.setFechaRetiro(en.readLine());    
}
 
public void leerTipoHabitacion()throws IOException{  
System.out.println("Introduzca el tipo de habitacion numero " + Cliente.numeroDeCliente + ":");  
System.out.flush();    
this.setTipoHabitacion(en.readLine());
}
 
public void leerDatosCliente()throws IOException{
leerCliente();
leerFechaIngreso();
leerFechaRetiro();
leerTipoHabitacion();
}
 
/**
* Retorna la información que nos interesa del cliente
*/

public String toString(){
StringBuffer buffer = new StringBuffer();
buffer.append("Cliente :"+ this.getNombre());
buffer.append("\n");
 
buffer.append("FechaIngreso :"+ this.getFechaIngreso());
buffer.append("\n");
 
buffer.append("Fecha Retiro :"+ this.getFechaRetiro());
buffer.append("\n");
 
buffer.append("Tipo Habitación :"+ this.getTipoHabitacion());
buffer.append("\n");
 
return buffer.toString();
}
 
 
 
public String getFechaIngreso() {
return fechaIngreso;
}
 
public void setFechaIngreso(String fechaIngreso) {
this.fechaIngreso = fechaIngreso;
}
 
public String getFechaRetiro() {
return fechaRetiro;
}
 
public void setFechaRetiro(String fechaRetiro) {
this.fechaRetiro = fechaRetiro;
}
 
public String getNombre() {
return nombre;
}
 
public void setNombre(String nombre) {
this.nombre = nombre;
}
 
public String getTipoHabitacion() {
return tipoHabitacion;
}
 
public void setTipoHabitacion(String tipoHabitacion) {
this.tipoHabitacion = tipoHabitacion;
}
}
 
public class prueba {
 
public static void main(String[] args){  
//Declaramos el buffer de lectura para leer de teclado    
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));    
 
Cliente [] clientes = new Cliente[10];
 
try{
//por cada cliente en el array , utilizamos el bucle for mejorado
for(Cliente cli : clientes){  
cli = new Cliente(br);
cli.leerDatosCliente();
System.out.println(cli.toString());  
}  
 
 
}catch(IOException error){
error.printStackTrace();
}
 
}
}
 
 
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 »

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

Mensajes: 1.945


Actualizate o muere!


Ver Perfil WWW
Re: arreglos de cadenas
« Respuesta #5 en: 5 Agosto 2011, 11:11 »

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
REcorrer ARreglos???
Programación General
Mr.Zeros0 2 713 Último mensaje 10 Mayo 2005, 01:04
por Slasher-K
Arreglos Multidimensionales en C++?
Programación C/C++
NightZpy 4 1,197 Último mensaje 11 Junio 2006, 18:40
por NightZpy
arreglos arrays
Hacking Básico
beethovenludwing 2 367 Último mensaje 7 Mayo 2007, 00:11
por yeikos
con arreglos
Java
artang 0 701 Último mensaje 31 Marzo 2009, 04:54
por artang
arreglos en c
Programación C/C++
el futuro 5 853 Último mensaje 21 Mayo 2010, 02:08
por Daemon Freedom
Powered by SMF 1.1.16 | SMF © 2006-2008, Simple Machines