I'm getting mad, I made a code that is very bad for maintain and for read.... please help
I am beginner with these classes that I am using, and I maybe missed an unknown methods for do my code easier...
I need some help for use another ways to extract strings easier that I did.
What i have to do:
It is a menu with 3 options:
1: add a name of a person, and a date , the file must contain the name in a new line, and the date in a new line ( like below):
Rexshine
21/2/2001
Norm
21/2/2000
2: read the file with data output like this:
[code=java]System.out.println("The birthday of "+ name +" is the day "+ day+" del "+ month +" (nació en el año "+ year +")");
3: exit
My solution (don't know easier, help):
maybe is easier to read the java code than read to my english
- For extract the name i make a split of \n of the file, and get it into a vector
once i do so, i do a split of the vector every 2 elements, so I do again an split "/" of every date, and get it into an array of ints, so i can fill a variable int "day", another "month" and other "year", and still have the vector with strings so i can fill "name"
I didn't finish of implement it, because i get error. so, any help is grateful
I get error in line 168, i know what i do wrong but can't fix, i don't think that is so hard to do it better, than fix what i'm triying to do. correct me please.
import java.io.EOFException; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.FileWriter; import java.io.FileReader; import java.util.StringTokenizer; import java.util.Vector; public class Birthday { static final int LINEAS = 1; static final int NUEVO_CUMP = 1; static final int CONSULTAR = 2; static final int SALIR = 3; public static void main(String[] args) { menu(); } static void menu(){ try{ BufferedReader lector = new BufferedReader(new InputStreamReader(System.in)); int opcion; /** NEW CODE FOR NORM, I CREATE THE FILE .txt AUTO*/ File file= new File("birth.txt"); String fichero = file.getName(); /* MY REAL-CODE do{ System.out.println("insert the name of the file"); fichero = lector.readLine(); if(!(new File(fichero).exists())) System.err.println("file no exists."); }while(!(new File(fichero).exists())); */ //Control de la salida del menú do{ //options do{ limpia(); limpia(); System.out.println(NUEVO_CUMP+".ADd new birthday."); System.out.println(CONSULTAR+". Get birthdays from file."); System.out.println(SALIR+". Exit"); opcion = Integer.parseInt(lector.readLine()); }while(opcion != NUEVO_CUMP && opcion != CONSULTAR && opcion != SALIR); switch(opcion){ case NUEVO_CUMP: //Se piden los datos System.out.println("write the name of the guy that you want to add:"); String cumpleaños = lector.readLine(); System.out.println("write new birth you want to add (format day/month/year):"); cumpleaños += "/"+lector.readLine(); //Llamada al método. nuevoCumpleaños(fichero, cumpleaños); //Se sostiene la pantalla System.out.println("Press a key to continue."); lector.readLine(); break; case CONSULTAR: muestraCumpleaños(fichero); //Se sostiene la pantalla System.out.println("Press a key to continue."); lector.readLine(); break; case SALIR: System.out.println("Normal exit."); } }while(opcion != SALIR); }catch(NumberFormatException e){ System.err.println("Error. number expected."); }catch(IOException e){ System.err.println("Error R/W."); }catch(Exception e){ System.err.println("Error. "); } } // METHOD NOT SO GOOD, BUT WORKS static void nuevoCumpleaños(String fichero, String datos) throws IOException{ String nombre; int dia; int mes; int año; try{ FileWriter escritor = new FileWriter(fichero, true); String[] array = datos.split("/"); nombre = array[0]; dia = Integer.parseInt(array[1]); mes = Integer.parseInt(array[2]); año = Integer.parseInt(array[3]); System.out.println("array[3]"+Integer.parseInt(array[3])+" y "+array[3]); //Formato datos del fichero: escritor.write("\n"+nombre+"\n"); escritor.write(dia+"/"); escritor.write(mes+"/"); escritor.write(año+" "); escritor.flush(); escritor.close(); }catch(EOFException e){ System.err.println("file not found."); } } //METHOD NOT SO GOOD static void muestraCumpleaños(String fichero) throws IOException{ try{ BufferedReader lectorArchivo = new BufferedReader(new FileReader(fichero)); Vector<String> dato = new Vector<String>(); String lin = ""; StringTokenizer st = new StringTokenizer("/"); while((lin = lectorArchivo.readLine()) != null){ dato.add(lin); //System.out.println(lin); } String[] nombreArray = new String[(dato.size()/2)+1]; int[] dia = new int[((dato.size()/2)+1)/3]; int[] mes = new int[((dato.size()/2)+1)/3]; int[] año = new int[((dato.size()/2)+1)/3]; String[] fecha = null ; for(int i=0, j=1, k=2; i<dato.size() ;i++, j+=2, k+=2){ nombreArray[i] = dato.elementAt(j); fecha = new String[dato.size()/2]; fecha = dato.elementAt(k).toString().split("/"); } for(int i=0, j=1, k=2; k<fecha.length ;i+=3, j+=3, k+=3){ dia[i] = Integer.parseInt(fecha[i]); mes[i] = Integer.parseInt(fecha[j]); año[i] = Integer.parseInt(fecha[k]); } for(int i=0; i<nombreArray.length ;i++){ System.out.println("Birthday of "+nombreArray[i]+" is the day "+dia[i]+" month: "+mes[i]+" (has born in... "+año[i]+")"); } lectorArchivo.close(); }catch(FileNotFoundException e){ System.err.println(); } } static void limpia (){ for(int i=0; i<LINEAS ;i++) System.out.println(); } }
Thank you in advance.