You are on page 1of 6

..

T: / if/else /*To programma auto deixnei ena aplo paradeigma me *eisodo/exodo dedomenwn (I/O) apo kai se arxeio kai *To programma rwtaei to xrhsth na ftiaxei ena katalogo * twn epafwn tou *Oi epafes apothikeyontai se arxeio * eisagei thn ennoia ths antikeimenostrefeias *Eisagoume to paketo java.io.*; *pou emperiexei oles tis klaseis I/O */ //A' VERSION import java.io.*; public class Buffer //onoma klasis { /*de xreiazetai aparaithta constructor giati * de theloume th dhmiourgia antikeimenou * gia auth th klash kai episis * h efarmogh mas apoteleitai apo 1 mono klash */ //grafoume th main method h opoia throws IOException // dioti tha kanei eisodo/exodo dedomenwn public static void main (String [] args) throws IOException { /* Theloume na rwthsoume to xrhsth * na mas dwsei gia mia epafh to onoma, to * epwnymo * kai to kinhto thlefwno. * kai na tou dwsoume th dynatothta AN thelei * na mas dwsei kai stathero thlefwno */ //Epeidh o xrhsths mporei an thelei na prosthesei // parapanw apo mia epafes, vazoume olo ton parakatw //kwdika se mia while loop //Arxika dhlwnoume ena boolean boolean prostheseEpafi=true; //oso to boolean einai true ektelese ton kwdika ths while while(prostheseEpafi) { System.out.print("Enter name: "); /*Xrhsimopoioume th klash BufferedReader gia na paroume * thn apanthsh pou edwse. Xrhsimopoioume th methodo * readLine() ths klashs BufferedReader gia na * metatrepsoume thn apanthsh se String */ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 1
29 210.38.22.157 495 www.arnos.gr e-mail : info@arnos.gr

.. /* opws vlepete parapanw, gia na dhmiourghsoume ena * antikeimeno ths klashs BufferedReader, o constructor * ths zhtaei ena antikeimeno ths klashs * InputStreamReader. Gia na dhmiourghsoume ena * antikeimeno ths klashs InputStreamReader o constructor * ths zhtaei to keyboard input tou xrhsth dhl. to * System.in * Ean p.x. o xrhsths dwsei Alex tote h lexh Alex * metatrepetai se bytes kai o constructor ths * InputStreamReader to metatrepei se xarakthres * Ara to antikeimeno new InputStreamReader(System.in) * emperiexei xarakthres. To antikeimeno auto o * constructor ths BufferedReader to metatrepei se * line h' array. Gia na metatrapei ayto to line h' array * se String xrhsimopoioume th methodo readLine() ths * BufferedReader */ //Twra tha apothikeysoume to input tou xrhsth String onomaPouEdwse=br.readLine(); //Epeita theloume na ton rwthsoume to epwnymo tou System.out.print("Enter surname: "); //Twra tha apothikeysoume to 2o input tou xrhsth String epwnymoPouEdwse=br.readLine(); //Epeita theloume na ton rwthsoume to epwnymo tou System.out.print("Enter mobile phone: "); //Twra tha apothikeysoume to 3o input tou xrhsth String mobilePouEdwse=br.readLine(); //orizoume kai arxikopoioume sto 0 //ena long (se int de xwraei) to opoio tha //lavei argotera ti timh // pou edwse o xrhsths gia mobile noumero long mobileTelephone=0; //orizoume kai arxikopoioume sto 0 //ena long to opoio tha lavei argotera ti timh // pou edwse o xrhsths gia stathero noumero long statheroTelephone=0; //Elegxoume ean to plithos //twn xarakthrwn pou edwse einai 10 // xrhsimopoioume th methodo length() h opoia //epistrefei ena int (ton arithmo xarakthrwn enos String) //sth synthkh tou if parakatw o elegxos isothtas ginetai //me == kai oxi me equals kai auto giati i entolh //String.length() einai ena int 2
29 210.38.22.157 495 www.arnos.gr e-mail : info@arnos.gr

.. //ean oi xarakthres einai 10 if(mobilePouEdwse.length()==10) { //PROSOXH: theloume episis na elegxoume ean to input pou //edwse einai arithmoi //H diadikasia metatrophs enos String se int mporei // na prokalesei kapoia exairesh (exception) //gia auto kai th diadikasia auth th vazoume se mesa se //try /* -----OI EXAIRESEIS (EXCEPTIONS)-------* . ' , try block try block catch blocks . */ try { //h methodos parseLong metatrepei ena String se long mobileTelephone = Long.parseLong(mobilePouEdwse); } //ean erthei edw tote simainei oti to String den htan //long catch(NumberFormatException nfe) { System.out.println("Not an Integer"); } //Prosexte parapanw oti orizoume to mobileTelephone EXW //apo to if block kai auto giati theloume na to //xrhsimopoihsoume kai parakatw //dhl. exw apo to if }//telos if else// den einai 10 xarakthrwn { System.out.println("To tilefwno pou dwsate den einai swsto!"); break;//vges apo th while loop }

/* Opws parathreite, gia na paroume to input tou xrhsth * xrhsimopoioume synexeia thn idia entolh: br.readLine() * kai auto giati kathe fora pou ekteleitai h entolh ayth * diavazei to teleutaio input pou dothike */ //Twra tha ton rwthsoume an thelei na mas dwsei kai //stathero thl. // Ean thelei, tha prepei na pei yes, an oxi tha prepei //na pei no 3
29 210.38.22.157 495 www.arnos.gr e-mail : info@arnos.gr

.. System.out.print("If you want to enter stathero thl., enter yes.\nIf you don't, enter no: "); //me thn entolh \n allazoume grammh //Twra tha apothikeysoume to 3o input tou xrhsth String apanthshPouEdwse=br.readLine(); //Twra tha prepei na doume AN h apanthsh pou edwse einai //yes h' no // tha xrhsimopoihsoume th methodo equals() h opoia //epistrefei true ean isxyei h isothta kai false ean oxi if(apanthshPouEdwse.equals("yes"))//ean eipe yes { //Tha ton rwthsoume tin ilikia tou System.out.print("Enter stathero thl.:

");

//Twra tha apothikeysoume to 4o input tou xrhsth String statheroPouEdwse=br.readLine(); //kanoume tous elegxous pou kaname kai gia to mobile if(statheroPouEdwse.length()==10)//ean oi xarakthres einai 10 { try { //h methodos parseLong metatrepei ena String se Long mobileTelephone = Long.parseLong(mobilePouEdwse); } //ean erthei edw tote simainei oti to //String den htan long catch(NumberFormatException nfe) { System.out.println("Not an Integer"); } }//telos if else// den einai 10 xarakthrwn { System.out.println("To tilefwno pou dwsate den einai swsto!"); break;//vges apo th while loop } //Tha tou ektypwsoyme ta input pou mas edwse se arxeio //pou onomazetai contacts.doc

4
29 210.38.22.157 495 www.arnos.gr e-mail : info@arnos.gr

.. /* *Xrhsimopoioume th klash PrintWriter gia na ektypwsoume * se arxeio *Gia na dhmiourghsoume ena antikeimeno ths klashs ayths * xreiazomaste ena antikeimeno FileWriter. Ths teleutaias * o kataskeyasths zhtaei 2 parametrous: 1 to onoma tou * arxeiou kai 2 ena boolean. *An de theloume na kanoume overwrite tyxon hdh yparxontes * epafes sto arxeio dinoume true */ PrintWriter pw=new PrintWriter(new FileWriter("contacts.doc",true),false); //H epafh tha ektypwthei se mia grammh ws exhs: // Alex Tsiaousis 6944000000 2100000000 pw.println(onomaPouEdwse+" "+epwnymoPouEdwse+" "+mobilePouEdwse+" "+statheroPouEdwse); pw.close(); //POLY SHMANTIKO!! An den kanoume close (flush) tote //mporei na mhn egrafoun ta dedomena sto arxeio } else if (apanthshPouEdwse.equals("no"))//ean eipe no { //Tha tou ektypwsoyme ta input pou mas edwse PrintWriter pw=new PrintWriter(new FileWriter("contacts.doc",true),false); //H epafh tha ektypwthei se mia grammh ws exhs: // Alex Tsiaousis 6944000000 pw.println(onomaPouEdwse+" "+epwnymoPouEdwse+" "+mobilePouEdwse); pw.close(); //POLY SHMANTIKO!! An den kanoume close (flush) tote //mporei na mhn egrafoun ta dedomena sto arxeio } else//den edwse oute yes oute no { // Se ayth th periptwsh tha tou poume oti edwse lathos //input System.out.println("Bad input!"); break;//vges apo th while }

5
29 210.38.22.157 495 www.arnos.gr e-mail : info@arnos.gr

.. //rwta an thelei na prosthesei kai allh epafh System.out.print("If you want to enter another contact enter yes.\nIf you don't, enter no: "); //me thn entolh \n allazoume grammh //Twra tha apothikeysoume to input tou xrhsth String apanthshPouEdwse2=br.readLine(); if(apanthshPouEdwse2.equals("yes")) { //xanatrexe to kwdika ths while loop apo th arxh continue; } else { break;//telos ths loop } }//telos while }//telos main

}//telos klashs 2007


:

6
29 210.38.22.157 495 www.arnos.gr e-mail : info@arnos.gr

You might also like