Chapter 06 - File Processing

Chapter 06 - File Processing - &KDSWHU )LOH...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: &KDSWHU )LOH 3URFHVVLQJ &RS\ULJKW E\ 6WXDUW 5HJHV DQG 0DUW\ 6WHSS $GYDQFHG )LOH 3URFHVVLQJ 2XWSXW )LOHV ZLWK 3ULQW6WUHDP 7U\ &DWFK 6WDWHPHQWV &DVH 6WXG\ :HLJKWHG *3$ )LOH 5HDGLQJ %DVLFV 'DWD 'DWD (YHU\ZKHUH )LOH %DVLFV 5HDGLQJ D )LOH ZLWK D 6FDQQHU 'HWDLOV RI 7RNHQ %DVHG 3URFHVVLQJ 6WUXFWXUH RI )LOHV DQG &RQVXPLQJ ,QSXW 6FDQQHU 3DUDPHWHUV 3DWKV DQG 'LUHFWRULHV $ 0RUH &RPSOH[ ,QSXW )LOH /LQH %DVHG 3URFHVVLQJ 6WULQJ 6FDQQHUV DQG /LQH 7RNHQ &RPELQDWLRQV ,QWURGXFWLRQ ,Q &KDSWHU ZH VDZ KRZ WR FRQVWUXFW D Scanner REMHFW WR UHDG LQSXW IURP WKH FRQVROH 1RZ ZH ZLOO VHH KRZ WR FRQVWUXFW Scanner REMHFWV WR UHDG LQSXW IURP ILOHV 7KH LGHD LV IDLUO\ VWUDLJKWIRUZDUG EXW -DYD GRHV QRW PDNH LW HDV\ WR UHDG IURP LQSXW ILOHV 7KLV LV XQIRUWXQDWH EHFDXVH PDQ\ LQWHUHVWLQJ SUREOHPV FDQ EH IRUPXODWHG DV ILOH SURFHVVLQJ WDVNV 0DQ\ LQWURGXFWRU\ FRPSXWHU VFLHQFH FODVVHV KDYH DEDQGRQHG ILOH SURFHVVLQJ DOWRJHWKHU RU WKH WRSLF KDV PRYHG LQWR WKH VHFRQG FRXUVH EHFDXVH LW LV FRQVLGHUHG WRR DGYDQFHG IRU QRYLFHV 7KHUH LV QRWKLQJ LQWULQVLFDOO\ FRPSOH[ DERXW ILOH SURFHVVLQJ EXW -DYD ZDV QRW GHVLJQHG IRU ILOH SURFHVVLQJ DQG 6XQ KDV QRW EHHQ SDUWLFXODUO\ HDJHU WR SURYLGH D VLPSOH VROXWLRQ 6XQ GLG KRZHYHU LQWURGXFH WKH Scanner FODVV DV D ZD\ WR VLPSOLI\ VRPH RI WKH GHWDLOV DVVRFLDWHG ZLWK UHDGLQJ ILOHV 7KH UHVXOW LV WKDW ILOH UHDGLQJ LV VWLOO DZNZDUG LQ -DYD EXW DW OHDVW WKH OHYHO RI GHWDLO LV PDQDJHDEOH %HIRUH ZH FDQ ZULWH D ILOH SURFHVVLQJ SURJUDP ZH KDYH WR H[SORUH VRPH LVVXHV UHODWHG WR -DYD H[FHSWLRQV 5HPHPEHU WKDW H[FHSWLRQV DUH HUURUV WKDW KDOW WKH H[HFXWLRQ RI D SURJUDP ,Q WKH FDVH RI ILOH SURFHVVLQJ ZH PLJKW WU\ WR RSHQ D ILOH WKDW GRHVQ W H[LVW ZKLFK ZRXOG JHQHUDWH DQ H[FHSWLRQ 339 )LOH 5HDGLQJ %DVLFV ,Q WKLV VHFWLRQ ZH ORRN DW WKH PRVW EDVLF LVVXHV UHODWHG WR ILOH SURFHVVLQJ :KDW DUH ILOHV DQG ZK\ GR ZH FDUH DERXW WKHP" :KDW DUH WKH PRVW EDVLF WHFKQLTXHV IRU UHDGLQJ ILOHV LQ D -DYD SURJUDP" 2QFH ZH KDYH PDVWHUHG WKHVH EDVLFV ZH OO PRYH RQ WR D PRUH GHWDLOHG XQGHUVWDQGLQJ RI WKH GLIIHUHQW WHFKQLTXHV ZH FDQ XVH IRU SURFHVVLQJ ILOHV 'DWD 'DWD (YHU\ZKHUH :H VHHP WR KDYH D IDVFLQDWLRQ ZLWK GDWD :KHQ WKH ILHOG RI 6WDWLVWLFV HPHUJHG LQ WKH QLQHWHHQWK FHQWXU\ WKHUH ZDV DQ H[SORVLRQ RI LQWHUHVW LQ JDWKHULQJ DQG LQWHUSUHWLQJ ODUJH DPRXQWV RI GDWD 0DUN 7ZDLQ UHSRUWHG WKDW WKH %ULWLVK VWDWHVPDQ %HQMDPLQ 'LVUDHOL FRPSODLQHG WR KLP WKDW 7KHUH DUH WKUHH NLQGV RI OLHV OLHV GDPQ OLHV DQG VWDWLVWLFV 7KH DGYHQW RI WKH LQWHUQHW KDV RQO\ DGGHG IXHO WR WKH ILUH 7RGD\ HYHU\ SHUVRQ ZLWK DQ LQWHUQHW FRQQHFWLRQ KDV DFFHVV WR D YDVW DUUD\ RI GDWDEDVHV ZLWK LQIRUPDWLRQ DERXW HYHU\ IDFHW RI RXU H[LVWHQFH %HORZ DUH MXVW D IHZ H[DPSOHV *R WR ZZZ ODQGPDUN SURMHFW FRP DQG FOLFN RQ WKH OLQN IRU 5DZ 'DWD DQG \RX ZLOO ILQG GDWD ILOHV DERXW HDUWKTXDNHV DLU SROOXWLRQ EDVHEDOO ODERU FULPH ILQDQFLDO PDUNHWV 86 KLVWRU\ JHRJUDSK\ ZHDWKHU QDWLRQDO SDUNV D ZRUOG YDOXHV VXUYH\ DQG PRUH $W ZZZ JXWHQEHUJ FRP \RX OO ILQG WKRXVDQGV RI RQOLQH ERRNV LQFOXGLQJ WKH FRPSOHWH ZRUNV RI 6KDNHVSHDUH DQG ZRUNV E\ 6LU $UWKXU &RQDQ 'R\OH -DQH $XVWHQ + * :HOOV -DPHV -R\FH $OEHUW (LQVWHLQ 0DUN 7ZDLQ /HZLV &DUUROO 7 6 (OLRW (GJDU $OODQ 3RH DQG PDQ\ RWKHUV 7KHUH LV D ZHDOWK RI JHQRPLF GDWD RQ WKH ZHE DYDLODEOH IURP VLWHV OLNH ZZZ JGE FRP DQG ZZZ QFEL QLK JRY %LRORJLVWV KDYH GHFLGHG WKDW WKH YDVW TXDQWLWLHV RI GDWD DERXW WKH KXPDQ JHQRPH DQG WKH JHQRPHV RI RWKHU RUJDQLVPV VKRXOG EH SXEOLFO\ DYDLODEOH WR HYHU\RQH WR VWXG\ 0DQ\ SRSXODU ZHE VLWHV OLNH WKH ,QWHUQHW 0RYLH 'DWDEDVH PDNH WKHLU GDWD DYDLODEOH IRU GRZQORDG DV VLPSOH GDWD ILOHV GHVFULEHG DW ZZZ LPGE FRP LQWHUIDFHV 7KH 86 JRYHUQPHQW SURGXFHV UHDPV RI VWDWLVWLFDO GDWD 7KH ZHE VLWH ZZZ IHGVWDWV JRY SURYLGHV D OHQJWK\ OLVW RI DYDLODEOH GRZQORDGV LQFOXGLQJ PDSV DQG VWDWLVWLFV RQ HPSOR\PHQW FOLPDWH PDQXIDFWXULQJ GHPRJUDSKLFV KHDOWK FULPH DQG VR RQ 'LG \RX .QRZ 2ULJLQ RI 'DWD 3URFHVVLQJ 'DWD SURFHVVLQJ DV D ILHOG SUHGDWHV FRPSXWHUV E\ RYHU KDOI D FHQWXU\ ,W LV RIWHQ VDLG WKDW QHFHVVLW\ LV WKH PRWKHU RI LQYHQWLRQ DQG ZH ILQG D JRRG H[DPSOH RI WKLV SULQFLSOH LQ WKH HPHUJHQFH RI GDWD SURFHVVLQJ 7KH FULVLV WKDW VSDZQHG WKH LQGXVWU\ FDPH IURP D UHTXLUHPHQW LQFOXGHG LQ WKH 86 &RQVWLWXWLRQ $UWLFOH 6HFWLRQ RI WKH 86 &RQVWLWXWLRQ LQGLFDWHV WKDW SRSXODWLRQ ZLOO EH XVHG WR GHFLGH KRZ PDQ\ UHSUHVHQWDWLYHV HDFK VWDWH ZLOO JHW LQ WKH +RXVH RI 5HSUHVHQWDWLYHV 7R PDNH VXFK D FDOFXODWLRQ \RX QHHG WR NQRZ WKH SRSXODWLRQ QXPEHUV 6R WKH 340 &RQVWLWXWLRQ VD\V 7KH DFWXDO (QXPHUDWLRQ VKDOO EH PDGH ZLWKLQ WKUHH <HDUV DIWHU WKH ILUVW 0HHWLQJ RI WKH &RQJUHVV RI WKH 8QLWHG 6WDWHV DQG ZLWKLQ HYHU\ VXEVHTXHQW 7HUP RI WHQ <HDUV LQ VXFK 0DQQHU DV WKH\ VKDOO E\ /DZ GLUHFW 7KH ILUVW FHQVXV ZDV FRPSOHWHG UHODWLYHO\ TXLFNO\ LQ 6LQFH WKHQ HYHU\ WHQ \HDUV WKH 86 JRYHUQPHQW KDV KDG WR SHUIRUP DQRWKHU FRPSOHWH FHQVXV RI WKH SRSXODWLRQ 7KLV SURFHVV EHFDPH PRUH DQG PRUH GLIILFXOW DV WKH SRSXODWLRQ RI WKH FRXQWU\ JUHZ ODUJHU %\ WKH JRYHUQPHQW IRXQG WKDW ZLWK ROG IDVKLRQHG KDQG FRXQWLQJ WHFKQLTXHV LW EDUHO\ FRPSOHWHG WKH FHQVXV ZLWKLQ WKH WHQ \HDUV DOORWWHG WR LW 6R WKH JRYHUQPHQW DQQRXQFHG D FRPSHWLWLRQ IRU LQYHQWRUV WR SURSRVH PDFKLQHV WKDW FRXOG EH XVHG WR VSHHG XS WKH SURFHVV +HUPDQ +ROOHULWK ZRQ WKH FRPSHWLWLRQ ZLWK D V\VWHP LQYROYLQJ SXQFKHG FDUGV 2YHU PLOOLRQ FDUGV ZHUH SXQFKHG E\ FOHUNV DQG WKHQ FRXQWHG E\ D VHULHV RI RQH KXQGUHG FRXQWLQJ PDFKLQHV 7KLV DOORZHG WKH WDEXODWLRQ WR EH FRPSOHWHG LQ OHVV WKDQ KDOI WKH WLPH WDNHQ WR KDQG FRXQW WKH UHVXOWV HYHQ WKRXJK WKH SRSXODWLRQ KDG LQFUHDVHG E\ WZHQW\ ILYH SHUFHQW +ROOHULWK VWUXJJOHG IRU \HDUV WR WXUQ KLV LQYHQWLRQ LQWR D FRPPHUFLDO VXFFHVV +LV ELJJHVW SUREOHP LQLWLDOO\ ZDV WKDW KH KDG MXVW RQH FXVWRPHU WKH 86 JRYHUQPHQW (YHQWXDOO\ KH IRXQG RWKHU FXVWRPHUV DQG WKH FRPSDQ\ WKDW KH IRXQGHG PHUJHG ZLWK FRPSHWLWRUV DQG JUHZ LQWR WKH FRPSDQ\ ZH QRZ NQRZ DV ,QWHUQDWLRQDO %XVLQHVV 0DFKLQHV &RUSRUDWLRQ RU ,%0 :H WKLQN RI ,%0 DV D FRPSXWHU FRPSDQ\ EXW LW VROG D ZLGH YDULHW\ RI GDWD SURFHVVLQJ HTXLSPHQW LQYROYLQJ +ROOHULWK FDUGV ORQJ EHIRUH FRPSXWHUV EHFDPH SRSXODU /DWHU ZKHQ LW HQWHUHG WKH FRPSXWHU ILHOG ,%0 XVHG +ROOHULWK FDUGV IRU VWRULQJ SURJUDPV DQG GDWD 2QH RI WKH DXWKRUV RI WKLV ERRN XVHG +ROOHULWK SXQFKHG FDUGV LQ KLV IUHVKPDQ FRPSXWHU SURJUDPPLQJ FODVV LQ )LOH %DVLFV :KHQ \RX VWRUH GDWD OLNH WKLV RQ \RXU RZQ FRPSXWHU \RX VWRUH LW LQ D ILOH )LOH $ FROOHFWLRQ RI LQIRUPDWLRQ VWRUHG RQ D FRPSXWHU (YHU\ ILOH KDV D QDPH )RU H[DPSOH LI \RX ZHUH WR GRZQORDG WKH WH[W RI +DPOHW IURP WKH *XWHQEHUJ VLWH \RX PLJKW VWRUH LW RQ \RXU FRPSXWHU LQ D ILOH FDOOHG KDPOHW W[W )LOH QDPHV RIWHQ HQG ZLWK D VSHFLDO VXIIL[ WKDW LQGLFDWHV WKH NLQG RI GDWD LW FRQWDLQV RU WKH IRUPDW LW KDV EHHQ VWRUHG LQ 7KLV VXIIL[ LV NQRZQ DV D ILOH H[WHQVLRQ %HORZ LV D WDEOH RI FRPPRQ ILOH H[WHQVLRQV 341 &RPPRQ )LOH ([WHQVLRQV ([WHQVLRQ W[W GDW RXW MDYD FODVV GRF [OV SGI FVY 'HVFULSWLRQ WH[W ILOH GDWD ILOH RXWSXW ILOH -DYD VRXUFH FRGH ILOH FRPSLOHG -DYD E\WHFRGH ILOH 0LFURVRIW :RUG ILOH 0LFURVRIW ([FHO ILOH 3RUWDEOH 'RFXPHQW )LOH E\ $GREH &RPPD 6HSDUDWHG 9DOXHV ILOH :H VRPHWLPHV UHIHU WR ILOHV VWRUHG RQ \RXU FRPSXWHU DV H[WHUQDO ILOHV EHFDXVH WKH\ DUH QRW FRQWDLQHG ZLWKLQ D SURJUDP DQG DUH QRW REWDLQHG IURP WKH XVHU GXULQJ SURJUDP H[HFXWLRQ 7R DFFHVV VXFK D ILOH IURP LQVLGH D -DYD SURJUDP \RX QHHG WR FRQVWUXFW VRPH NLQG RI LQWHUQDO REMHFW WKDW ZLOO UHSUHVHQW WKH ILOH 7KH -DYD FODVV OLEUDULHV LQFOXGH D FODVV FDOOHG )LOH WKDW SHUIRUPV WKLV GXW\ <RX FRQVWUXFW D )LOH REMHFW E\ SDVVLQJ WKH QDPH RI D ILOH DV LQ File f = new File("hamlet.txt"); 2QFH FRQVWUXFWHG \RX FDQ FDOO D QXPEHU RI PHWKRGV WR PDQLSXODWH WKH ILOH )RU H[DPSOH WKH SURJUDP EHORZ FDOOV D PHWKRG WKDW GHWHUPLQHV ZKHWKHU D ILOH H[LVWV ZKHWKHU LW FDQ EH UHDG DQG ZKDW LW V OHQJWK LV L H KRZ PDQ\ FKDUDFWHUV DUH LQ WKH ILOH 1 2 3 4 5 6 7 8 9 10 11 12 // Report some basic information about a file import java.io.*; public class FileInfo { public static void main(String args) { File f = new File("hamlet.txt"); System.out.println("exists returns " + f.exists()); System.out.println("canRead returns " + f.canRead()); System.out.println("length returns " + f.length()); } } 1RWLFH WKDW LW LQFOXGHV DQ LPSRUW IURP WKH SDFNDJH MDYD LR EHFDXVH WKH )LOH FODVV LV SDUW RI WKDW SDFNDJH 7KH WHUP LR LV D ELW RI MDUJRQ XVHG E\ FRPSXWHU VFLHQFH SURIHVVLRQDOV WR PHDQ LQSXW RXWSXW $VVXPLQJ \RX KDYH VWRUHG WKH ILOH KDPOHW LQ WKH VDPH GLUHFWRU\ DV WKH SURJUDP \RX JHW WKH IROORZLQJ RXWSXW ZKHQ \RX UXQ WKLV SURJUDP exists returns true canRead returns true length returns 191734 %HORZ LV D OLVW RI XVHIXO PHWKRGV IRU )LOH REMHFWV 342 8VHIXO 0HWKRGV RI File 2EMHFWV 0HWKRG delete() exists() 'HVFULSWLRQ GHOHWHV WKH JLYHQ ILOH ZKHWKHU RU QRW WKLV ILOH H[LVWV RQ WKH V\VWHP getAbsolutePath() WKH IXOO SDWK ZKHUH WKLV ILOH LV ORFDWHG getName() WKH QDPH RI WKLV ILOH DV D String ZLWKRXW DQ\ SDWK DWWDFKHG getPath() WKH SDWK ZKHUH WKLV ILOH LV ORFDWHG ZLWKRXW WKH DFWXDO ILOH V QDPH isDirectory() ZKHWKHU WKLV ILOH UHSUHVHQWV D GLUHFWRU\ IROGHU RQ WKH V\VWHP isFile() ZKHWKHU WKLV ILOH UHSUHVHQWV D ILOH QRQ IROGHU RQ WKH V\VWHP length() WKH QXPEHU RI FKDUDFWHUV LQ WKLV ILOH mkdirs() FUHDWHV WKH GLUHFWRU\ UHSUHVHQWHG E\ WKLV ILOH LI LW GRHV QRW H[LVW renameTo(File) FKDQJHV WKLV ILOH V QDPH WR EH WKH JLYHQ ILOH V QDPH 5HDGLQJ D )LOH ZLWK D 6FDQQHU 7KH Scanner FODVV WKDW ZH KDYH EHHQ XVLQJ VLQFH &KDSWHU LV IOH[LEOH LQ WKDW ZH FDQ DWWDFK Scanner REMHFWV WR PDQ\ GLIIHUHQW NLQGV RI LQSXW <RX FDQ WKLQN RI D Scanner REMHFW DV EHLQJ OLNH D IDXFHW WKDW \RX FDQ DWWDFK WR D SLSH WKDW KDV ZDWHU IORZLQJ WKURXJK LW )RU H[DPSOH LQ D KRXVH ZH G DWWDFK D IDXFHW WR D SLSH FDUU\LQJ ZDWHU IURP WKH FLW\ RU IURP D ZHOO %XW ZH DOVR VHH IDXFHWV LQ PRELOH KRPHV DQG DLUSODQHV ZKHUH WKH VRXUFH RI ZDWHU LV GLIIHUHQW :H KDYH EHHQ FRQVWUXFWLQJ RXU Scanner REMHFWV E\ SDVVLQJ System.in WR WKH Scanner FRQVWUXFWRU Scanner console = new Scanner(System.in); 7KLV LQVWUXFWV WKH FRPSXWHU WR FRQVWUXFW D Scanner WKDW UHDGV IURP WKH FRQVROH L H SDXVLQJ IRU LQSXW IURP WKH XVHU ,QVWHDG RI SDVVLQJ System.in WR WKH FRQVWUXFWRU ZH FDQ SDVV D )LOH REMHFW File f = new File("hamlet.txt"); Scanner input = new Scanner(f); ,Q WKLV FDVH WKH YDULDEOH I LV QRW QHFHVVDU\ VR ZH FDQ VKRUWHQ WKLV WR Scanner input = new Scanner(new File("hamlet.txt")); 7KLV SDUWLFXODU OLQH RI FRGH RU VRPHWKLQJ OLNH LW ZLOO DSSHDU LQ DOO RI \RXU ILOH SURFHVVLQJ SURJUDPV 343 ScannerV FDQ EH FRQQHFWHG WR PDQ\ LQSXW VRXUFHV 8QIRUWXQDWHO\ ZH UXQ LQWR D VQDJ ZKHQ ZH WU\ WR FRPSLOH D SURJUDP WKDW FRQVWUXFWV D 6FDQQHU LQ WKLV PDQQHU 6XSSRVH WKDW ZH ZULWH D PDLQ PHWKRG WKDW EHJLQV E\ RSHQLQJ D 6FDQQHU LQ WKLV PDQQHU // Flawed method--does not compile. public static void main(String args) { Scanner input = new Scanner(new File("hamlet.txt")); ... } 7KLV SURJUDP GRHV QRW FRPSLOH ,W SURGXFHV D PHVVDJH OLNH WKH IROORZLQJ Count.java:8: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown Scanner input = new Scanner(new File("hamlet.txt")); ^ 1 error 7KH LVVXH LQYROYHV H[FHSWLRQV ZKLFK ZHUH ILUVW LQWURGXFHG LQ &KDSWHU 5HPHPEHU WKDW H[FHSWLRQV DUH HUURUV WKDW SUHYHQW D SURJUDPPLQJ IURP FRQWLQXLQJ QRUPDO H[HFXWLRQ ,Q WKLV FDVH WKH FRPSLOHU LV ZRUULHG WKDW LW PLJKW QRW EH DEOH WR ILQG D ILOH FDOOHG hamlet.txt :KDW LV LW VXSSRVHG WR GR LI WKDW KDSSHQV" ,W ZRXOGQ W KDYH DQ\ ZD\ WR FRQWLQXH H[HFXWLQJ WKH UHVW RI WKH FRGH EHFDXVH LW ZRXOGQ W KDYH D ILOH WR UHDG IURP ,I WKH SURJUDP LV XQDEOH WR ORFDWH WKH VSHFLILHG LQSXW ILOH LW ZLOO JHQHUDWH DQ HUURU E\ WKURZLQJ ZKDW LV NQRZQ DV D FileNotFoundException 7KLV SDUWLFXODU H[FHSWLRQ LV NQRZQ DV D FKHFNHG H[FHSWLRQ &KHFNHG ([FHSWLRQ $Q H[FHSWLRQ WKDW PXVW EH FDXJKW RU VSHFLILFDOO\ GHFODUHG LQ WKH KHDGHU RI WKH PHWKRG WKDW PLJKW JHQHUDWH LW %HFDXVH FileNotFoundException LV D FKHFNHG H[FHSWLRQ ZH FDQ W MXVW LJQRUH LW -DYD SURYLGHV D FRQVWUXFW NQRZQ DV WKH try catch VWDWHPHQW IRU KDQGOLQJ VXFK HUURUV /DWHU LQ WKLV FKDSWHU ZH ZLOO VHH KRZ WR XVH D try catch WR KDQGOH WKLV HUURU %XW IRU QRZ ZH ZLOO XVH D OHVV VRSKLVWLFDWHG EXW 344 VLPSOHU DSSURDFK -DYD DOORZV XV WR DYRLG KDQGOLQJ WKLV HUURU DV ORQJ DV ZH FOHDUO\ LQGLFDWH WKH IDFW WKDW ZH DUHQ W KDQGOLQJ WKH HUURU ,Q SDUWLFXODU ZH FDQ LQFOXGH ZKDW LV NQRZQ DV D WKURZV FODXVH LQ WKH KHDGHU IRU WKH main PHWKRG WR FOHDUO\ VWDWH WKH IDFW WKDW RXU main PHWKRG PLJKW JHQHUDWH WKLV H[FHSWLRQ throws FODXVH $ GHFODUDWLRQ WKDW D PHWKRG ZLOO QRW DWWHPSW WR KDQGOH D SDUWLFXODU W\SH RI H[FHSWLRQ :H FDQ PRGLI\ WKH KHDGHU IRU PHWKRG PDLQ WR LQFOXGH D WKURZV FODXVH LQGLFDWLQJ WKDW LW FDQ WKURZ D )LOH1RW)RXQG([FHSWLRQ public static void main(String args) throws FileNotFoundException { Scanner input = new Scanner(new File("hamlet.txt")); ... } :LWK WKLV PRGLILFDWLRQ WKH SURJUDP FRPSLOHV 2QFH ZH YH FRQVWUXFWHG WKH Scanner VR WKDW LW UHDGV IURP WKH ILOH ZH FDQ PDQLSXODWH LW OLNH DQ\ RWKHU Scanner :KHQ ZH UHDG IURP WKH FRQVROH ZH DOZD\V SURPSW EHIRUH UHDGLQJ WR JLYH WKH XVHU DQ LQGLFDWLRQ RI ZKDW NLQG RI GDWD ZH ZDQW :KHQ ZH UHDG IURP D ILOH ZH GRQ W QHHG WR SURPSW EHFDXVH WKH GDWD LV DOUHDG\ WKHUH VWRUHG LQ WKH ILOH )RU H[DPSOH ZH PLJKW ZULWH WKH IROORZLQJ SURJUDP WR FRXQW WKH QXPEHU RI ZRUGV LQ +DPOHW 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 // Counts the number of words in Hamlet. import java.io.*; import java.util.*; public class Count { public static void main(String args) throws FileNotFoundException { Scanner input = new Scanner(new File("hamlet.txt")); int count = 0; while (input.hasNext()) { String word = input.next(); count++; } System.out.println("total words = " + count); } } :H KDYH WR LQFOXGH DQ LPSRUW IURP MDYD XWLO IRU WKH 6FDQQHU FODVV DQG DQ LPSRUW IURP MDYD LR IRU WKH )LOH FODVV 7KH SURJUDP JHQHUDWHV WKH IROORZLQJ RXWSXW total words = 31956 'HWDLOV RI 7RNHQ %DVHG 3URFHVVLQJ 1RZ WKDW ZH KDYH VHHQ VRPH RI WKH EDVLF LVVXHV LQYROYHG ZLWK UHDGLQJ DQ H[WHUQDO LQSXW ILOH OHW V H[SORUH LQ PRUH GHWDLO WKH LVVXHV LQYROYHG LQ UHDGLQJ D ILOH 2QH ZD\ WR SURFHVV D ILOH LV WRNHQ E\ WRNHQ 345 7RNHQ %DVHG 3URFHVVLQJ 3URFHVVLQJ LQSXW WRNHQ E\ WRNHQ L H RQH ZRUG DW D WLPH RU RQH QXPEHU DW D WLPH 5HPHPEHU IURP &KDSWHU WKDW WKH SULPDU\ WRNHQ UHDGLQJ PHWKRGV IRU WKH 6FDQQHU FODVV DUH QH[W,QW IRU UHDGLQJ DQ LQW YDOXH QH[W'RXEOH IRU UHDGLQJ D GRXEOH YDOXH QH[W IRU UHDGLQJ WKH QH[W WRNHQ DV D 6WULQJ )RU H[DPSOH ZH PLJKW ZDQW WR FUHDWH D ILOH FDOOHG numbers.dat ZLWK WKH IROORZLQJ FRQWHQW 308.2 14.9 7.4 2.8 3.9 4.7 -15.4 2.8 <RX FDQ FUHDWH VXFK D ILOH ZLWK DQ HGLWRU OLNH 1RWH3DG RQ :LQGRZV RU 7H[W(GLW RQ WKH 0DFLQWRVK 7KHQ \RX PLJKW ZULWH D SURJUDP WKDW SURFHVVHV WKLV H[WHUQDO LQSXW ILOH DQG SURGXFHV VRPH NLQG RI UHSRUW )RU H[DPSOH WKH SURJUDP EHORZ UHDGV WKH ILUVW ILYH QXPEHUV IURP WKH ILOH DQG UHSRUWV WKHLU VXP 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // Program that reads 5 numbers and reports their sum. import java.io.*; import java.util.*; public class Echo1 { public static void main(String args) throws FileNotFoundException { Scanner input = new Scanner(new File("numbers.dat")); double sum = 0.0; for (int i = 1; i <= 5; i++) { double next = input.nextDouble(); System.out.println("number " + i + " = " + next); sum += next; } System.out.println("Sum = " + sum); } } 5HPHPEHU WKDW ZH QHHG D WKURZV FODXVH LQ WKH KHDGHU IRU PDLQ EHFDXVH RI WKH SRWHQWLDO )LOH1RW)RXQG([FHSWLRQ 7KH SURJUDP SURGXFHV WKH IROORZLQJ RXWSXW number 1 = 308.2 number 2 = 14.9 number 3 = 7.4 number 4 = 2.8 number 5 = 3.9 Sum = 337.19999999999993 346 1RWLFH WKDW WKH QXPEHUV DUH QRW UHSRUWHG DV DGGLQJ XS WR RII HUURU ZKLFK ZDV GHVFULEHG LQ &KDSWHU 7KLV LV DQRWKHU H[DPSOH RI D URXQG 7KH SUHFHGLQJ SURJUDP UHDGV H[DFWO\ ILYH QXPEHUV IURP WKH ILOH 0RUH W\SLFDOO\ ZH UHDG LQGHILQLWHO\ XVLQJ D ZKLOH ORRS DV ORQJ DV WKHUH DUH PRUH QXPEHUV WR UHDG 5HPHPEHU WKDW WKH Scanner FODVV LQFOXGHV D VHULHV RI hasNext PHWKRGV WKDW SDUDOOHO WKH YDULRXV next PHWKRGV ,Q WKLV FDVH ZH DUH XVLQJ nextDouble WR UHDG D YDOXH RI W\SH GRXEOH VR ZH FDQ XVH hasNextDouble WR WHVW ZKHWKHU WKHUH LV VXFK D YDOXH WR UHDG 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 // Variation that reads while there are more numbers to read. import java.io.*; import java.util.*; public class Echo2 { public static void main(String args) throws FileNotFoundException { Scanner input = new Scanner(new File("numbers.dat")); double sum = 0.0; int count = 0; while (input.hasNextDouble()) { double next = input.nextDouble(); count++; System.out.println("number " + count + " = " + next); sum += next; } System.out.println("Sum = " + sum); } } 7KLV SURJUDP ZRXOG ZRUN RQ DQ LQSXW ILOH ZLWK DQ\ QXPEHU RI QXPEHUV 2XU ILOH KDSSHQV WR KDYH HLJKW QXPEHUV DQG ZKHQ ZH UXQ WKLV YHUVLRQ RI WKH SURJUDP ZH JHW WKH IROORZLQJ RXWSXW number 1 = 308.2 number 2 = 14.9 number 3 = 7.4 number 4 = 2.8 number 5 = 3.9 number 6 = 4.7 number 7 = -15.4 number 8 = 2.8 Sum = 329.29999999999995 6WUXFWXUH RI )LOHV DQG &RQVXPLQJ ,QSXW :H WKLQN RI WH[W DV EHLQJ WZR GLPHQVLRQDO OLNH D VKHHW RI SDSHU EXW IURP WKH FRPSXWHU V SRLQW RI YLHZ HDFK ILOH LV MXVW D RQH GLPHQVLRQDO VHTXHQFH RI FKDUDFWHUV )RU H[DPSOH FRQVLGHU WKH ILOH FDOOHG numbers.dat WKDW ZH VDZ LQ WKH ODVW VHFWLRQ 308.2 14.9 7.4 2.8 3.9 4.7 -15.4 2.8 347 :H WKLQN RI WKLV DV D OLQH ILOH ZLWK WH[W JRLQJ DFURVV DQG GRZQ DQG WZR EODQN OLQHV LQ WKH PLGGOH 7KH FRPSXWHU YLHZV WKH ILOH GLIIHUHQWO\ :KHQ VRPHRQH W\SHV LQ D ILOH OLNH WKLV WKH\ KLW WKH (QWHU NH\ WR JR WR D QHZ OLQH 7KLV LQVHUWV VSHFLDO QHZ OLQH FKDUDFWHUV LQ WKH ILOH :H KDYH VHHQ WKDW WKH HVFDSH VHTXHQFH \n FDQ EH XVHG WR SURGXFH D QHZOLQH FKDUDFWHU IRU RXWSXW :H FDQ DQQRWDWH WKH ILOH DERYH ZLWK \n FKDUDFWHUV WR LQGLFDWH WKH HQG RI HDFK OLQH 308.2 14.9 7.4\n 2.8\n \n \n 3.9 4.7 -15.4\n 2.8\n 2QFH ZH KDYH PDUNHG WKH HQG RI HDFK OLQH ZH QR ORQJHU QHHG WR XVH D UHSUHVHQWDWLRQ :H FDQ FROODSVH WKLV WR D RQH GLPHQVLRQDO VHTXHQFH RI FKDUDFWHUV 308.2 14.9 7.4\n2.8\n\n\n3.9 4.7 -15.4\n2.8\n GLPHQVLRQDO WUHDW ILOHV DV RQH GLPHQVLRQDO VWULQJV RI FKDUDFWHUV DQG FRQYHUW WKHLU FRQWHQWV LQWR D VHULHV RI ZKLWHVSDFH VHSDUDWHG WRNHQV ScannerV )URP WKLV RQH GLPHQVLRQDO VHTXHQFH ZH FDQ UHFRQVWUXFW WKH YDULRXV OLQHV RI WKH ILOH 7KLV LV KRZ WKH FRPSXWHU YLHZV WKH ILOH DV D RQH GLPHQVLRQDO VHTXHQFH RI FKDUDFWHUV LQFOXGLQJ VSHFLDO FKDUDFWHUV WKDW UHSUHVHQW QHZ OLQH 2Q VRPH V\VWHPV LQFOXGLQJ :LQGRZV PDFKLQHV WKHUH DUH WZR GLIIHUHQW FKDUDFWHUV WKDW UHSUHVHQW QHZ OLQH EXW IRU RXU GLVFXVVLRQ ZH OO XVH MXVW \n WR UHSUHVHQW ERWK 2EMHFWV OLNH Scanner KDQGOH WKHVH GLIIHUHQFHV IRU XV VR ZH FDQ JHQHUDOO\ LJQRUH WKHP )RU WKRVH ZKR DUH LQWHUHVWHG WKH EULHI H[SODQDWLRQ LV WKDW :LQGRZV PDFKLQHV HQG HDFK OLQH ZLWK D \r IROORZHG E\ D \n 7R SURFHVV D ILOH WKH Scanner REMHFW NHHSV WUDFN RI D FXUUHQW SRVLWLRQ LQ WKH ILOH <RX FDQ WKLQN RI WKLV DV D FXUVRU RU SRLQWHU LQWR WKH ILOH ,QSXW FXUVRU $ SRLQWHU WR WKH FXUUHQW SRVLWLRQ LQ DQ LQSXW ILOH :KHQ WKH Scanner REMHFW LV ILUVW FRQVWUXFWHG WKLV FXUVRU SRLQWV WR WKH EHJLQQLQJ RI WKH ILOH %XW DV ZH SHUIRUP YDULRXV next RSHUDWLRQV WKLV FXUVRU PRYHV IRUZDUG 7KH (FKR SURJUDP IURP WKH ODVW VHFWLRQ SURFHVVHV WKH ILOH WKURXJK D VHULHV RI FDOOV RQ nextDouble /HW V WDNH D PRPHQW WR H[DPLQH LQ GHWDLO KRZ WKDW ZRUNV :KHQ WKH Scanner LV ILUVW FRQVWUXFWHG WKH LQSXW FXUVRU ZLOO EH SRVLWLRQHG DW WKH EHJLQQLQJ RI WKH ILOH LQGLFDWHG EHORZ ZLWK DQ XS DUURZ SRLQWLQJ DW WKH ILUVW FKDUDFWHU LQ WKH ILOH 348 308.2 14.9 7.4\n2.8\n\n\n3.9 4.7 -15.4\n2.8\n ^ input cursor $IWHU WKH ILUVW FDOO RQ nextDouble WKH FXUVRU ZLOO EH SRVLWLRQHG LQ WKH PLGGOH RI WKH ILUVW OLQH DIWHU WKH WRNHQ "308.2" 308.2 14.9 7.4\n2.8\n\n\n3.9 4.7 -15.4\n2.8\n ^ input cursor :H UHIHU WR WKLV SURFHVV DV FRQVXPLQJ LQSXW &RQVXPLQJ LQSXW 0RYLQJ WKH LQSXW FXUVRU IRUZDUG SDVW VRPH LQSXW 7KH ILUVW FDOO RQ nextDouble FRQVXPHV WKH WH[W "308.2" IURP WKH LQSXW ILOH DQG OHDYHV WKH LQSXW FXUVRU SRVLWLRQHG DW WKH ILUVW FKDUDFWHU DIWHU WKLV WRNHQ 1RWLFH WKDW WKLV OHDYHV WKH LQSXW FXUVRU SRVLWLRQHG DW D VSDFH :KHQ WKH VHFRQG FDOO LV PDGH RQ nextDouble WKH Scanner ILUVW VNLSV SDVW WKLV VSDFH WR JHW WR WKH QH[W WRNHQ DQG WKHQ FRQVXPHV WKH WH[W "14.9" DQG OHDYHV WKH FXUVRU SRVLWLRQHG DW WKH VSDFH WKDW IROORZV LW 308.2 14.9 7.4\n2.8\n\n\n3.9 4.7 -15.4\n2.8\n ^ input cursor $ WKLUG FDOO RQ nextDouble VNLSV WKH VSDFH LW LV SRVLWLRQHG DW DQG FRQVXPHV WKH WH[W "7.4" 308.2 14.9 7.4\n2.8\n\n\n3.9 4.7 -15.4\n2.8\n ^ input cursor $W WKLV SRLQW WKH LQSXW FXUVRU LV SRVLWLRQHG DW WKH QHZOLQH FKDUDFWHU DW WKH HQG RI WKH ILUVW OLQH RI LQSXW $ IRXUWK FDOO RQ nextDouble VNLSV SDVW WKLV QHZOLQH FKDUDFWHU DQG FRQVXPHV WKH WH[W "2.8" 308.2 14.9 7.4\n2.8\n\n\n3.9 4.7 -15.4\n2.8\n ^ input cursor 1RWLFH WKDW LQ VNLSSLQJ SDVW WKH ILUVW QHZOLQH FKDUDFWHU WKH LQSXW FXUVRU KDV PRYHG LQWR GDWD VWRUHG RQ WKH VHFRQG OLQH RI LQSXW WKH $W WKLV SRLQW WKH LQSXW FXUVRU LV SRVLWLRQHG DW WKH HQG RI WKH VHFRQG OLQH RI LQSXW EHFDXVH LW KDV FRQVXPHG WKH "2.8" WRNHQ :KHQ D ILIWK FDOO LV PDGH RQ nextDouble WKH Scanner ILQGV WZR QHZOLQH FKDUDFWHUV LQ D URZ 7KLV LVQ W D SUREOHP IRU WKH Scanner EHFDXVH LW VLPSO\ VNLSV SDVW DQ\ OHDGLQJ ZKLWHVSDFH FKDUDFWHUV VSDFHV WDEV QHZOLQH FKDUDFWHUV XQWLO LW ILQGV DQ DFWXDO WRNHQ 6R LW VNLSV ERWK RI WKHVH QHZOLQH FKDUDFWHUV DQG FRQVXPHV WKH WH[W "3.9" 349 308.2 14.9 7.4\n2.8\n\n\n3.9 4.7 -15.4\n2.8\n ^ input cursor $W WKLV SRLQW WKH LQSXW FXUVRU LV SRVLWLRQHG LQ WKH PLGGOH RI WKH ILIWK OLQH RI LQSXW WKH WKLUG DQG IRXUWK OLQHV ZHUH EODQN 7KH VL[WK FDOO RQ nextDouble FRQVXPHV WKH WH[W "4.7" 308.2 14.9 7.4\n2.8\n\n\n3.9 4.7 -15.4\n2.8\n ^ input cursor 7KH VHYHQWK FDOO FRQVXPHV WKH WH[W "-15.4" 308.2 14.9 7.4\n2.8\n\n\n3.9 4.7 -15.4\n2.8\n ^ input cursor $QG WKH HLJKW FDOO FRQVXPHV WKH WH[W "2.8" 308.2 14.9 7.4\n2.8\n\n\n3.9 4.7 -15.4\n2.8\n ^ input cursor $W WKLV SRLQW WKH LQSXW FXUVRU LV SRVLWLRQHG DW WKH QHZOLQH FKDUDFWHU DW WKH HQG RI WKH ILOH ,I \RX DWWHPSWHG WR FDOO nextDouble DJDLQ LW ZRXOG WKURZ DQ H[FHSWLRQ EHFDXVH WKHUH DUH QR PRUH WRNHQV OHIW WR SURFHVV %XW UHPHPEHU WKDW WKH (FKR SURJUDP KDV D ZKLOH ORRS WKDW FDOOV hasNextDouble EHIRUH FDOOLQJ nextDouble WR PDNH VXUH WKDW WKHUH DFWXDOO\ LV D GRXEOH YDOXH WR SURFHVV :KHQ WKH LQSXW FXUVRU UHDFKHV WKH QHZOLQH DW WKH HQG RI WKH ILOH WKH Scanner QRWLFHV WKDW WKHUH DUH QR PRUH GRXEOH YDOXHV WR UHDG DQG LW UHWXUQV IDOVH ZKHQ hasNextDouble LV FDOOHG 7KDW VWRSV WKH ZKLOH ORRS IURP H[HFXWLQJ DQG \RX H[LW WKH SURJUDP :KHQ \RX FDOO PHWKRGV OLNH hasNextDouble WKH Scanner ORRNV DKHDG LQ WKH ILOH WR VHH ZKHWKHU WKHUH LV D QH[W WRNHQ DQG ZKHWKHU LW FRXOG EH LQWHUSUHWHG DV EHLQJ RI WKDW W\SH LQ WKLV FDVH D GRXEOH 6R WKH (FKR SURJUDP ZLOO FRQWLQXH H[HFXWLQJ XQWLO LW UHDFKHV WKH HQG RI WKH ILOH RU XQWLO LW HQFRXQWHUV D WRNHQ WKDW FRXOG QRW EH LQWHUSUHWHG DV D GRXEOH 6FDQQHU REMHFWV DUH GHVLJQHG IRU ILOH SURFHVVLQJ LQ D IRUZDUG PDQQHU 7KH\ SURYLGH D JUHDW GHDO RI IOH[LELOLW\ IRU ORRNLQJ DKHDG LQ DQ LQSXW ILOH EXW QR VXSSRUW IRU UHDGLQJ WKH LQSXW EDFNZDUGV 7KHUH DUH QR SUHYLRXV PHWKRGV DQG QR PHFKDQLVP IRU UHVHWWLQJ D 6FDQQHU EDFN WR WKH EHJLQQLQJ RI WKH LQSXW ,I D SURJUDP QHHGV WR UHDG DQ LQSXW ILOH PRUH WKDQ RQFH \RX ZRXOG QHHG WR FRQVWUXFW PRUH WKDQ RQH 6FDQQHU REMHFW WLHG WR WKH VDPH ILOH 6FDQQHU 3DUDPHWHUV 1RYLFHV DUH VRPHWLPHV VXUSULVHG WKDW WKH LQSXW FXUVRU IRU D 6FDQQHU GRHV QRW UHVHW WR WKH EHJLQQLQJ RI WKH ILOH SDUWLFXODUO\ ZKHQ LW LV SDVVHG DV D SDUDPHWHU WR D PHWKRG )RU H[DPSOH FRQVLGHU WKH IROORZLQJ YDULDWLRQ RI WKH (FKR SURJUDP ,W KDV D PHWKRG WKDW WDNHV D 6FDQQHU DV LQSXW DQG DQ LQWHJHU VSHFLI\LQJ KRZ PDQ\ QXPEHUV WR SURFHVV 350 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 // Demonstrates a Scanner as a parameter to a method that // can consume an arbitrary number of tokens import java.io.*; import java.util.*; public class Echo3 { public static void main(String args) throws FileNotFoundException { Scanner input = new Scanner(new File("numbers.dat")); processTokens(input, 2); processTokens(input, 3); processTokens(input, 2); } public static void processTokens(Scanner input, int n) { double sum = 0.0; for (int i = 1; i <= n; i++) { double next = input.nextDouble(); System.out.println("number " + i + " = " + next); sum += next; } System.out.println("Sum = " + sum); System.out.println(); } } 7KH PDLQ PHWKRG FUHDWHV D 6FDQQHU REMHFW WKDW LV WLHG WR WKH QXPEHUV GDW ILOH ,W WKHQ FDOOV WKH SURFHVV7RNHQV PHWKRG VHYHUDO WLPHV LQGLFDWLQJ WKH QXPEHU RI WRNHQV WR SURFHVV 7KH ILUVW FDOO LQVWUXFWV WKH PHWKRG WR SURFHVV WRNHQV ZKLFK RSHUDWHV RQ WKH ILUVW WZR WRNHQV RI WKH ILOH JHQHUDWLQJ WKH IROORZLQJ RXWSXW number 1 = 308.2 number 2 = 14.9 Sum = 323.09999999999997 :KDW GR \RX VXSSRVH WKH VHFRQG FDOO RQ WKH PHWKRG GRHV" 7KH VHFRQG FDOO LQGLFDWHV WKDW WRNHQV DUH WR EH SURFHVVHG 6RPH SHRSOH H[SHFW WKH PHWKRG WR SURFHVV WKH ILUVW WKUHH WRNHQV RI WKH ILOH EXW WKDW V QRW ZKDW KDSSHQV 5HPHPEHU WKDW WKH 6FDQQHU NHHSV WUDFN RI ZKHUH WKH LQSXW FXUVRU LV SRVLWLRQHG $IWHU WKH ILUVW FDOO RQ WKH PHWKRG WKH LQSXW FXUVRU LV SRVLWLRQHG EH\RQG WKH ILUVW WZR WRNHQV 6R WKH VHFRQG FDOO RQ WKH PHWKRG SURFHVVHV WKH QH[W WKUHH WRNHQV IURP WKH ILOH SURGXFLQJ WKLV RXWSXW number 1 = 7.4 number 2 = 2.8 number 3 = 3.9 Sum = 14.1 7KH ILQDO FDOO RQ WKH PHWKRG DVNV WKH 6FDQQHU WR SURFHVV WKH QH[W WZR WRNHQV IURP WKH ILOH ZKLFK HQGV XS SURFHVVLQJ WKH VL[WK DQG VHYHQWK QXPEHUV IURP WKH ILOH number 1 = 4.7 number 2 = -15.4 Sum = -10.7 7KH SURJUDP WKHQ WHUPLQDWHV QHYHU KDYLQJ SURFHVVHG WKH HLJKWK QXPEHU LQ WKH ILOH 351 7KH NH\ SRLQW WR UHPHPEHU LV WKDW D 6FDQQHU NHHSV WUDFN RI WKH LQSXW FXUVRU VR WKDW \RX FDQ SURFHVV DQ LQSXW ILOH SLHFH E\ SLHFH LQ D YHU\ IOH[LEOH PDQQHU (YHQ ZKHQ WKH 6FDQQHU LV SDVVHG DV D SDUDPHWHU WR D PHWKRG LW UHPHPEHUV KRZ PXFK RI WKH LQSXW ILOH KDV EHHQ SURFHVVHG VR IDU 3DWKV DQG 'LUHFWRULHV $Q\ JLYHQ ILOH ZLOO EH VWRUHG LQ D SDUWLFXODU IROGHU RU GLUHFWRU\ 'LUHFWRULHV DUH RUJDQL]HG LQ D KLHUDUFK\ VWDUWLQJ IURP D URRW GLUHFWRU\ DW WKH WRS )RU H[DPSOH PRVW :LQGRZV PDFKLQHV KDYH D GLVN GULYH NQRZQ DV & $W WKH WRS OHYHO RI WKLV GULYH ZH KDYH ZKDW LV NQRZQ DV WKH URRW GLUHFWRU\ ZKLFK ZH FDQ GHVFULEH DV & ? 7KHUH ZLOO EH YDULRXV WRS OHYHO GLUHFWRULHV FRQWDLQHG LQ WKLV URRW GLUHFWRU\ DQG HDFK RI WKHP FDQ KDYH VXEGLUHFWRULHV DQG HDFK VXEGLUHFWRU\ FDQ KDYH VXEGLUHFWRULHV DQG VR RQ (YHU\ ILOH LV VWRUHG LQ RQH RI WKHVH GLUHFWRULHV 7KH GHVFULSWLRQ RI KRZ WR JHW IURP WKH WRS OHYHO GLUHFWRU\ WR WKH SDUWLFXODU GLUHFWRU\ WKDW VWRUHV D ILOH LV NQRZQ DV WKH SDWK RI WKH ILOH )LOH 3DWK $ GHVFULSWLRQ RI D ILOH V ORFDWLRQ RQ D FRPSXWHU VWDUWLQJ ZLWK D GULYH DQG LQFOXGLQJ WKH SDWK IURP WKH URRW GLUHFWRU\ WR WKH GLUHFWRU\ ZKHUH WKH ILOH LV VWRUHG )RU H[DPSOH D ILOH PLJKW EH VWRUHG LQ & ?VFKRRO?GDWD?KDPOHW W[W :H UHDG WKH SDWK LQIRUPDWLRQ IURP OHIW WR ULJKW 7KH ILOH LV RQ WKH & GULYH LQ D IROGHU FDOOHG VFKRRO DQG LQ D VXEIROGHU FDOOHG GDWD ,Q WKH SUHYLRXV VHFWLRQ ZH XVHG WKH ILOH QDPH numbers.dat :KHQ -DYD ILQGV \RX XVLQJ D VLPSOH QDPH OLNH WKDW DOVR FDOOHG D UHODWLYH ILOH SDWK LW ORRNV LQ WKH FXUUHQW GLUHFWRU\ WR ILQG WKH ILOH &XUUHQW GLUHFWRU\ DOVR FDOOHG ZRUNLQJ GLUHFWRU\ 7KH GLUHFWRU\ WKDW -DYD XVHV DV WKH GHIDXOW ZKHQ D VLPSOH ILOH QDPH LV XVHG 7KH GHILQLWLRQ RI FXUUHQW GLUHFWRU\ YDULHV GHSHQGLQJ XSRQ ZKDW -DYD HQYLURQPHQW \RX DUH XVLQJ ,Q PRVW HQYLURQPHQWV WKH FXUUHQW GLUHFWRU\ LV WKH GLUHFWRU\ LQ ZKLFK \RXU SURJUDP DSSHDUV 7KLV LV WKH EHKDYLRU ZH OO DVVXPH IRU WKH H[DPSOHV LQ WKLV WH[WERRN <RX FDQ DOVR XVH D IXOO\ TXDOLILHG ILOH QDPH VRPHWLPHV FDOOHG DQ DEVROXWH ILOH SDWK 7KLV DSSURDFK ZRUNV ZHOO RQO\ ZKHQ \RX NQRZ H[DFWO\ ZKHUH \RXU ILOH LV JRLQJ WR EH VWRUHG RQ \RXU V\VWHP )RU H[DPSOH LI \RX DUH RQ D :LQGRZV PDFKLQH DQG \RX KDYH VWRUHG WKH ILOH LQ D GLUHFWRU\ NQRZQ DV F ?GDWD ZH FRXOG XVH D ILOH QDPH OLNH WKLV Scanner input = new Scanner(new File("c:/data/numbers.dat")); 1RWLFH WKDW WKH SDWK LV ZULWWHQ ZLWK IRUZDUG VODVK FKDUDFWHUV UDWKHU WKDQ EDFNVODVK FKDUDFWHUV 2Q :LQGRZV \RX ZRXOG QRUPDOO\ XVH D EDFNVODVK EXW -DYD DOORZV \RX WR XVH D IRUZDUG VODVK LQVWHDG ,I ZH ZDQWHG WR XVH D EDFNVODVK ZH ZRXOG KDYH WR XVH DQ HVFDSH VHTXHQFH 0RVW SURJUDPPHUV XVH WKH VLPSOHU DSSURDFK RI XVLQJ IRUZDUG VODVK FKDUDFWHUV EHFDXVH -DYD GRHV WKH DSSURSULDWH WUDQVODWLRQ RQ :LQGRZV PDFKLQHV 352 6RPHWLPHV UDWKHU WKDQ ZULWLQJ D ILOH V SDWK QDPH LQ WKH FRGH RXUVHOYHV ZH DVN WKH XVHU IRU D ILOH QDPH ,Q WKH ODVW FKDSWHU ZH VDZ D SURJUDP FDOOHG )LQG6XP WKDW SURPSWHG WKH XVHU IRU D VHULHV RI QXPEHUV WR DGG WRJHWKHU %HORZ LV D YDULDWLRQ WKDW SURPSWV WKH XVHU IRU WKH QDPH RI D ILOH RI QXPEHUV WR EH DGGHG WRJHWKHU 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 // This program adds together a series of numbers from a file. It prompts // the user for the file name, then reads the file and reports the sum. import java.io.*; import java.util.*; public class FindSum2 { public static void main(String args) throws FileNotFoundException { System.out.println("This program will add together a series of real"); System.out.println("numbers from a file."); System.out.println(); Scanner console = new Scanner(System.in); System.out.print("What is the file name? "); String name = console.nextLine(); Scanner input = new Scanner(new File(name)); System.out.println(); double sum = 0; while (input.hasNextDouble()) { double next = input.nextDouble(); sum += next; } System.out.println("Sum = " + sum); } } :H UHDG WKH ILOH QDPH XVLQJ D FDOO RQ nextLine WR UHDG DQ HQWLUH OLQH RI LQSXW IURP WKH XVHU 7KLV DOORZV WKH XVHU WR W\SH LQ ILOH QDPHV WKDW KDYH VSDFHV LQ WKHP 1RWLFH WKDW ZH VWLOO QHHG WKH throws FileNotFoundException LQ WKH KHDGHU IRU main EHFDXVH HYHQ WKRXJK ZH DUH SURPSWLQJ WKH XVHU IRU D ILOH QDPH WKHUH ZRQ W QHFHVVDULO\ EH D ILOH RI WKDW QDPH ,I ZH KDYH WKLV SURJUDP UHDG IURP WKH ILOH numbers.dat WKDW ZH VDZ LQ WKH ODVW VHFWLRQ WKHQ WKH SURJUDP ZRXOG H[HFXWH OLNH WKLV This program will add together a series of real numbers from a file. What is the file name? numbers.dat Sum = 329.29999999999995 7KH XVHU DOVR KDV WKH RSWLRQ RI VSHFLI\LQJ D IXOO ILOH QDPH DV LQ 353 This program will add together a series of real numbers from a file. What is the file name? c:\data\numbers.dat Sum = 329.29999999999995 1RWLFH WKDW WKH XVHU GRHVQ W KDYH WR W\SH WZR EDFNVODVKHV WR JHW D VLQJOH EDFNVODVK 7KDW V EHFDXVH WKH Scanner REMHFW WKDW UHDGV WKH XVHU V LQSXW LV DEOH WR UHDG LW ZLWKRXW HVFDSH VHTXHQFHV $ 0RUH &RPSOH[ ,QSXW )LOH 6XSSRVH WKDW \RX KDYH DQ LQSXW ILOH WKDW KDV LQIRUPDWLRQ DERXW KRZ PDQ\ KRXUV KDYH EHHQ ZRUNHG E\ HDFK HPSOR\HH RI D FRPSDQ\ )RU H[DPSOH LW PLJKW ORRN OLNH WKH IROORZLQJ Erica 7.5 8.5 10.25 8 8.5 Greenlee 10.5 11.5 12 11 10.75 Simone 8 8 8 Ryan 6.5 8 9.25 8 Kendall 2.5 3 7KH LGHD LV WKDW ZH KDYH D OLVW RI KRXUV ZRUNHG E\ HDFK HPSOR\HH DQG ZH ZDQW WR ILQG RXW WKH WRWDO KRXUV ZRUNHG E\ HDFK LQGLYLGXDO :H FDQ FRQVWUXFW D Scanner REMHFW OLQNHG WR WKLV ILOH WR VROYH WKLV WDVN $V \RX VWDUW ZULWLQJ PRUH FRPSOH[ ILOH SURFHVVLQJ SURJUDPV \RX ZLOO ZDQW WR GLYLGH WKH SURJUDP LQWR PHWKRGV WR EUHDN XS WKH FRGH LQWR ORJLFDO VXEWDVNV ,Q WKLV FDVH ZH FDQ RSHQ WKH ILOH LQ main DQG ZULWH D VHSDUDWH PHWKRG WR SURFHVV WKH ILOH 0RVW ILOH SURFHVVLQJ ZLOO LQYROYH ZKLOH ORRSV EHFDXVH ZH ZRQ W NQRZ LQ DGYDQFH KRZ PXFK GDWD WKH ILOH KDV LQ LW :H OO FKRRVH GLIIHUHQW WHVWV GHSHQGLQJ XSRQ WKH SDUWLFXODU ILOH ZH DUH SURFHVVLQJ EXW WKH\ ZLOO DOPRVW DOO EH FDOOV RQ WKH YDULRXV hasNext PHWKRGV RI WKH Scanner FODVV :H EDVLFDOO\ ZDQW WR VD\ ZKLOH \RX KDYH PRUH GDWD IRU PH WR SURFHVV OHW V NHHS UHDGLQJ ,Q WKLV FDVH ZH KDYH D VHULHV RI LQSXW OLQHV WKDW HDFK EHJLQ ZLWK D QDPH )RU WKLV SURJUDP ZH DUH DVVXPLQJ WKDW QDPHV DUH VLPSOH ZLWK QR VSDFHV LQ WKH PLGGOH 7KDW PHDQV ZH OO EH UHDGLQJ WKHP ZLWK D FDOO RQ WKH next PHWKRG $V D UHVXOW RXU RYHUDOO WHVW LQYROYHV VHHLQJ LI WKHUH LV DQRWKHU QDPH LQ WKH LQSXW ILOH while (input.hasNext()) { <process next person> } 6R KRZ GR ZH SURFHVV RQH SHUVRQ" :H KDYH WR UHDG WKHLU QDPH DQG WKHQ UHDG WKHLU OLVW RI KRXUV ,I \RX ORRN DW WKH VDPSOH LQSXW ILOH \RX ZLOO VHH WKDW WKH OLVW RI KRXUV LV QRW DOZD\V WKH VDPH OHQJWK 7KLV LV D FRPPRQ RFFXUUHQFH LQ LQSXW ILOHV )RU H[DPSOH VRPH HPSOR\HHV PLJKW KDYH ZRUNHG RQ GLIIHUHQW GD\V ZKLOH RWKHUV ZRUNHG RQO\ GD\V RU GD\V 6R ZH ZLOO XVH D ORRS IRU WKLV DV ZHOO 7KLV LV D QHVWHG ORRS 7KH RXWHU ORRS LV KDQGOLQJ RQH SHUVRQ DW D WLPH DQG WKH LQQHU ORRS ZLOO KDQGOH RQH QXPEHU DW D WLPH 7KH WDVN LV D IDLUO\ VWUDLJKWIRUZDUG FXPXODWLYH VXP double sum = 0.0; while (input.hasNextDouble()) { sum += input.nextDouble(); } 354 3XWWLQJ WKLV DOO WRJHWKHU ZH HQG XS ZLWK WKH IROORZLQJ FRPSOHWH SURJUDP 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 // // // // // // // // // This program reads an input file of hours worked by various employees. Each line of the input file should have an employee's name (without any spaces) followed by a list of hours worked, as in: Erica 7.5 8.5 10.25 8 Greenlee 10.5 11.5 12 11 Ryan 6.5 8 9.25 8 The program reports the total hours worked by each employee. import java.io.*; import java.util.*; public class HoursWorked { public static void main(String args) throws FileNotFoundException { Scanner input = new Scanner(new File("hours.dat")); process(input); } public static void process(Scanner input) { while (input.hasNext()) { String name = input.next(); double sum = 0.0; while (input.hasNextDouble()) { sum += input.nextDouble(); } System.out.println("Total hours worked by " + name + " = " + sum); } } } 1RWLFH WKDW ZH DJDLQ QHHG WKH throws FileNotFoundException LQ WKH KHDGHU IRU main :H GRQ W QHHG WR LQFOXGH WKLV LQ WKH process PHWKRG EHFDXVH WKH FRGH WR RSHQ WKH ILOH DSSHDUV LQ PHWKRG main ,I ZH SXW WKH LQSXW DERYH LQWR D ILOH FDOOHG hours.dat DQG H[HFXWH WKH SURJUDP ZH JHW WKH IROORZLQJ UHVXOW Total Total Total Total Total hours hours hours hours hours worked worked worked worked worked by by by by by Erica = 42.75 Erin = 55.75 Simone = 24.0 Ryan = 31.75 Kendall = 5.5 /LQH EDVHG 3URFHVVLQJ 6R IDU ZH KDYH EHHQ ORRNLQJ DW SURJUDPV WKDW SURFHVV LQSXW WRNHQ E\ WRNHQ :H RIWHQ ILQG RXUVHOYHV ZRUNLQJ ZLWK LQSXW ILOHV WKDW DUH OLQH EDVHG ZKHUH HDFK OLQH RI LQSXW UHSUHVHQWV D GLIIHUHQW FDVH WR EH KDQGOHG VHSDUDWHO\ IURP WKH UHVW 7KLV OHDGV WR D VHFRQG VW\OH RI ILOH SURFHVVLQJ /LQH %DVHG 3URFHVVLQJ 3URFHVVLQJ LQSXW OLQH E\ OLQH L H UHDGLQJ LQ HQWLUH OLQHV RI LQSXW DW D WLPH 355 0RVW ILOH SURFHVVLQJ LQYROYHV D FRPELQDWLRQ RI WKHVH WZR VW\OHV DQG WKH Scanner FODVV LV IOH[LEOH HQRXJK WR DOORZ XV WR ZULWH SURJUDPV WKDW KDYH ERWK VW\OHV RI SURFHVVLQJ )RU OLQH EDVHG SURFHVVLQJ ZH XVH WKH nextLine DQG hasNextLine PHWKRGV RI WKH Scanner REMHFW )RU H[DPSOH EHORZ LV D SURJUDP WKDW HFKRV DQ LQSXW ILOH LQ XSSHUFDVH 1 2 3 4 5 6 7 8 9 10 11 12 13 14 // Reads a file and echos it in uppercase. import java.io.*; import java.util.*; public class EchoUppercase { public static void main(String args) throws FileNotFoundException { Scanner input = new Scanner(new File("poem.txt")); while (input.hasNextLine()) { String text = input.nextLine(); System.out.println(text.toUpperCase()); } } } 7KLV ORRS UHDGV WKH LQSXW OLQH E\ OLQH XQWLO LW UXQV RXW RI OLQHV WR SURFHVV SULQWLQJ HDFK OLQH LQ XSSHUFDVH ,W UHDGV IURP D ILOH FDOOHG SRHP W[W *LYHQ WKLV LQSXW ILOH My candle burns at both ends It will not last the night; But ah, my foes, and oh, my friends It gives a lovely light. --Edna St. Vincent Millay ,W SURGXFHV WKH IROORZLQJ RXWSXW MY CANDLE BURNS AT BOTH ENDS IT WILL NOT LAST THE NIGHT; BUT AH, MY FOES, AND OH, MY FRIENDS IT GIVES A LOVELY LIGHT. --EDNA ST. VINCENT MILLAY 1RWLFH WKDW ZH FRXOG QRW KDYH DFFRPSOLVKHG WKH VDPH WDVN ZLWK WRNHQ EDVHG SURFHVVLQJ +HUH WKH OLQH EUHDNV DUH VLJQLILFDQW EHFDXVH WKH\ DUH SDUW RI WKH SRHP $OVR ZKHQ ZH UHDG D ILOH WRNHQ E\ WRNHQ ZH ORVH WKH VSDFLQJ ZLWKLQ WKH OLQH EHFDXVH WKH 6FDQQHU VNLSV DQ\ OHDGLQJ ZKLWHVSDFH ZKHQ LW UHDGV D WRNHQ 7KH ILQDO OLQH RI WKLV LQSXW ILOH LV LQGHQWHG WR PDNH LW FOHDU WKDW LW LV WKH QDPH RI WKH DXWKRU DQG QRW SDUW RI WKH SRHP :H ZRXOG ORVH WKDW VSDFLQJ LI ZH UHDG WKH ILOH DV D VHULHV RI WRNHQV 6WULQJ 6FDQQHUV DQG /LQH 7RNHQ &RPELQDWLRQV ,Q WKH ODVW VHFWLRQ ZH ORRNHG DW D SURJUDP FDOOHG +RXUV:RUNHG WKDW SURFHVVHG WKH IROORZLQJ GDWD ILOH 356 Erica 7.5 8.5 10.25 8 8.5 Greenlee 10.5 11.5 12 11 10.75 Simone 8 8 8 Ryan 6.5 8 9.25 8 Kendall 2.5 3 ,Q WKLV FDVH WKH GDWD LV OLQH RULHQWHG ZLWK RQH HPSOR\HH V LQIRUPDWLRQ RQ D GLIIHUHQW OLQH RI LQSXW EXW ZH GLGQ W LQFRUSRUDWH WKLV DVSHFW RI WKH GDWD LQWR RXU SURJUDP :H SURFHVVHG WKH ILOH LQ D WRNHQ EDVHG PDQQHU ,Q D VHQVH ZH JRW OXFN\ WKDW WKLV ZRUNHG RXW )RU H[DPSOH FRQVLGHU D VOLJKW YDULDWLRQ LQ WKH GDWD ZKHUH HDFK OLQH RI LQSXW EHJLQV ZLWK DQ HPSOR\HH LG DQG D QDPH UDWKHU WKDQ MXVW D QDPH 101 783 114 238 156 Erica 7.5 8.5 10.25 8 8.5 Erin 10.5 11.5 12 11 10.75 Simone 8 8 8 Ryan 6.5 8 9.25 8 Kendall 2.5 3 7KLV VHHPV OLNH D IDLUO\ VLPSOH FKDQJH WKDW VKRXOGQ W UHTXLUH PDMRU FKDQJHV WR WKH FRGH 5HFDOO WKDW WKH SURJUDP KDV D PHWKRG WR SURFHVV WKH ILOH public static void process(Scanner input) { while (input.hasNext()) { String name = input.next(); double sum = 0.0; while (input.hasNextDouble()) { sum += input.nextDouble(); } System.out.println("Total hours worked by " + name + " = " + sum); } } 6XSSRVH ZH DGG D OLQH RI FRGH WR UHDG WKH HPSOR\HH LG DQG ZH PRGLI\ WKH SULQWOQ WR UHSRUW LW public static void process(Scanner input) { while (input.hasNext()) { int id = input.nextInt(); String name = input.next(); double sum = 0.0; while (input.hasNextDouble()) { sum += input.nextDouble(); } System.out.println("Total hours worked by " + name + " (id#" + id + ") = " + sum); } } :KHQ ZH UXQ WKLV YHUVLRQ RI WKH SURJUDP RQ WKH QHZ LQSXW ILOH ZH JHW RQH OLQH RI RXWSXW DQG WKHQ DQ H[FHSWLRQ LV WKURZQ Total hours worked by Erica (id#101) = 825.75 Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:819) at java.util.Scanner.next(Scanner.java:1431) at java.util.Scanner.nextInt(Scanner.java:2040) ... 357 7KH SURJUDP FRUUHFWO\ UHDGV (ULFD .DQH V HPSOR\HH LG DQG UHSRUWV LW LQ WKH SULQWOQ VWDWHPHQW EXW WKHQ LW GLHV $OVR QRWLFH WKDW WKH SURJUDP UHSRUWV (ULFD V WRWDO KRXUV ZRUNHG DV ZKHQ LW VKRXOG EH :KHUH GLG LW JR ZURQJ" ,I \RX FRPSXWH WKH GLIIHUHQFH EHWZHHQ WKH UHSRUWHG VXP RI KRXUV DQG WKH FRUUHFW VXP RI KRXUV \RX OO ILQG LW LV HTXDO WR 7KDW QXPEHU DSSHDUV LQ RXU GDWD ILOH ,W V WKH HPSOR\HH LG RI WKH VHFRQG HPSOR\HH (ULQ ,Q DGGLQJ XS WKH KRXUV IRU (ULFD WKH SURJUDP KDV DFFLGHQWDOO\ UHDG (ULQ V HPSOR\HH LG DQG DGGHG LW WR WKH VXP 7KDW V DOVR ZK\ WKH H[FHSWLRQ LV KDSSHQLQJ EHFDXVH RQ WKH VHFRQG LWHUDWLRQ RI WKH ORRS ZH DUH WU\LQJ WR UHDG DQ HPSOR\HH LG IRU (ULQ ZKHQ WKH QH[W WRNHQ LQ WKH ILOH LV QRW DQ LQWHJHU 7KH VROXWLRQ LV WR VRPHKRZ JHW WKH SURJUDP WR VWRS UHDGLQJ ZKHQ LW JHWV WR WKH HQG RI DQ LQSXW OLQH 8QIRUWXQDWHO\ WKHUH LV QR HDV\ ZD\ WR GR WKLV ZLWK D WRNHQ EDVHG DSSURDFK 2XU ORRS LV DVNLQJ ZKHWKHU WKH 6FDQQHU KDV D QH[W GRXEOH YDOXH WR UHDG DQG WKH HPSOR\HH LG ZLOO ORRN OLNH D GRXEOH WKDW FDQ EH UHDG :H PLJKW WU\ WR IRUP D FRPSOH[ WHVW WKDW ORRNV IRU D GRXEOH WKDW LV QRW DOVR DQ LQWHJHU EXW HYHQ WKDW ZRQ W ZRUN EHFDXVH VRPH RI WKH KRXUV DUH LQWHJHUV 7R PDNH WKLV SURJUDP ZRUN ZH QHHG WR ZULWH D PRUH VRSKLVWLFDWHG SURJUDP WKDW SD\V DWWHQWLRQ WR WKH OLQH EUHDNV :H KDYH WR UHDG DQ HQWLUH OLQH RI LQSXW DW D WLPH DQG SURFHVV WKDW OLQH E\ LWVHOI 5HFDOO WKDW WKH main PHWKRG IRU WKH SURJUDP ORRNV OLNH WKLV public static void main(String args) throws FileNotFoundException { Scanner input = new Scanner(new File("hours2.dat")); process(input); } ,I ZH LQFRUSRUDWH D OLQH EDVHG ORRS ZH HQG XS ZLWK VRPHWKLQJ OLNH WKLV public static void main(String args) throws FileNotFoundException { Scanner input = new Scanner(new File("hours2.dat")); while (input.hasNextLine()) { String text = input.nextLine(); processLine(text); } } %\ UHDGLQJ WKH ILOH OLQH E\ OLQH ZH JXDUDQWHH WKDW ZH GRQ W DFFLGHQWDOO\ FRPELQH GDWD IRU WZR HPSOR\HHV 7KH GRZQVLGH LV WKDW ZH KDYH WR ZULWH D PHWKRG FDOOHG SURFHVV/LQH WKDW WDNHV D 6WULQJ DV D SDUDPHWHU DQG ZH KDYH WR SXOO DSDUW WKDW 6WULQJ ,W FRQWDLQV WKH HPSOR\HH LG IROORZHG E\ WKH HPSOR\HH QDPH IROORZHG E\ WKH QXPEHUV LQGLFDWLQJ KRZ PDQ\ KRXUV ZHUH ZRUNHG RQ GLIIHUHQW GD\V ,Q RWKHU ZRUGV WKH LQSXW OLQH LV FRPSRVHG RI VHYHUDO SLHFHV VHYHUDO WRNHQV WKDW ZH ZDQW WR SURFHVV SLHFH E\ SLHFH ,W ZDV PXFK HDVLHU WR SURFHVV WKLV GDWD LQ D WRNHQ EDVHG PDQQHU WKDQ WR KDYH LW LQ D 6WULQJ )RUWXQDWHO\ WKHUH LV D FRQYHQLHQW ZD\ WR GR WKLV :H FDQ FRQVWUXFW D Scanner REMHFW IURP DQ LQGLYLGXDO String 5HPHPEHU WKDW MXVW DV D IDXFHW FDQ EH DWWDFKHG WR GLIIHUHQW VRXUFHV RI ZDWHU D IDXFHW LQ D KRXVH DWWDFKHG WR FLW\ ZDWHU RU ZHOO ZDWHU YHUVXV D IDXFHW RQ DQ DLUSODQH DWWDFKHG WR D WDQN RI ZDWHU ZH FDQ DWWDFK D Scanner WR GLIIHUHQW VRXUFHV RI LQSXW :H YH VHHQ KRZ WR DWWDFK LW WR WKH FRQVROH System.in DQG WR D ILOH SDVVLQJ D File REMHFW :H FDQ DOVR DWWDFK LW WR DQ LQGLYLGXDO String )RU H[DPSOH ZH PLJKW ZULWH WKLV FRGH 358 Scanner input = new Scanner("18.4 17.9 8.3 2.9"); 7KLV FRQVWUXFWV D Scanner WKDW JHWV LWV LQSXW IURP WKH String WKDW ZH XVHG WR FRQVWUXFW LW 7KLV Scanner KDV DQ LQSXW FXUVRU MXVW OLNH D Scanner OLQNHG WR D ILOH ,QLWLDOO\ WKH LQSXW FXUVRU LV SRVLWLRQHG DW WKH ILUVW FKDUDFWHU LQ WKH String DQG LW PRYHV IRUZDUG DV ZH UHDG WRNHQV IURP WKH Scanner %HORZ LV D VKRUW SURJUDP WR GHPRQVWUDWH WKLV 1 2 3 4 5 6 7 8 9 10 11 import java.util.*; public class StringScannerExample { public static void main(String args) { Scanner input = new Scanner("18.4 17.9 8.3 2.9"); while (input.hasNextDouble()) { double next = input.nextDouble(); System.out.println(next); } } } ,W SURGXFHV WKH IROORZLQJ RXWSXW 18.4 17.9 8.3 2.9 1RWLFH WKDW LW SURGXFHV IRXU OLQHV RI RXWSXW EHFDXVH WKHUH DUH IRXU QXPEHUV LQ WKH String WKDW ZH XVH WR FRQVWUXFW WKH Scanner :KHQ ZH KDYH D ILOH WKDW UHTXLUHV WKLV FRPELQDWLRQ RI OLQH EDVHG SURFHVVLQJ DQG WRNHQ EDVHG SURFHVVLQJ ZH FDQ XVH D VOLJKWO\ GLIIHUHQW DSSURDFK E\ FRQVWUXFWLQJ D GLIIHUHQW String EDVHG Scanner IRU HDFK OLQH RI WKH LQSXW ILOH :LWK WKLV DSSURDFK ZH HQG XS ZLWK D ORW RI Scanner REMHFWV :H KDYH D Scanner REMHFW WKDW LV NHHSLQJ WUDFN RI WKH H[WHUQDO LQSXW ILOH :H XVH WKDW Scanner WR UHDG HQWLUH OLQHV RI LQSXW %XW HDFK WLPH ZH UHDG D OLQH RI WH[W IURP WKH ILOH ZH FRQVWUXFW D PLQL 6FDQQHU IRU MXVW WKDW OLQH RI LQSXW :H FDQ WKHQ XVHG WRNHQ EDVHG SURFHVVLQJ IRU WKHVH PLQL 6FDQQHU REMHFWV EHFDXVH WKH\ HDFK FRQWDLQ MXVW D VLQJOH OLQH RI GDWD LQ WKHP 7KLV FRPELQDWLRQ RI OLQH EDVHG DQG WRNHQ EDVHG SURFHVVLQJ LV SRZHUIXO <RX ZLOO ILQG WKDW WKH DSSURDFK DQG VOLJKW YDULDWLRQV FDQ EH XVHG WR SURFHVV D ODUJH YDULHW\ RI LQSXW ILOHV ,Q WKH FDVH RI WKH +RXUV:RUNHG SURJUDP HDFK LQSXW OLQH FRQWDLQHG LQIRUPDWLRQ IRU D VLQJOH HPSOR\HH 3URFHVVLQJ WKH LQSXW OLQH LQYROYHV PDNLQJ D 6FDQQHU IRU WKH OLQH DQG WKHQ UHDGLQJ LWV YDULRXV SDUWV LQ D WRNHQ EDVHG PDQQHU LG QDPH KRXUV :H FDQ SXW WKLV DOO WRJHWKHU LQWR D QHZ YHUVLRQ RI WKH SURJUDP 359 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 // // // // // // // // // This program reads an input file of hours worked by various employees. Each line of the input file should have an employee's name (without any spaces) followed by a list of hours worked, as in: Erica 7.5 8.5 10.25 8 Greenlee 10.5 11.5 12 11 Ryan 6.5 8 9.25 8 The program reports the total hours worked by each employee. import java.io.*; import java.util.*; public class HoursWorked2 { public static void main(String args) throws FileNotFoundException { Scanner input = new Scanner(new File("hours2.dat")); while (input.hasNextLine()) { String text = input.nextLine(); processLine(text); } } // Processes the given String (id, name and hours worked) public static void processLine(String text) { Scanner data = new Scanner(text); int id = data.nextInt(); String name = data.next(); double sum = 0.0; while (data.hasNextDouble()) { sum += data.nextDouble(); } System.out.println("Total hours worked by " + name + " (id#" + id + ") = " + sum); } } 1RWLFH WKDW WKH main PHWKRG LQFOXGHV WKH OLQH EDVHG SURFHVVLQJ RI UHDGLQJ HQWLUH OLQHV RI LQSXW IURP WKH ILOH (DFK VXFK OLQH LV SDVVHG WR WKH SURFHVV/LQH PHWKRG (DFK WLPH ZH FDOO SURFHVV/LQH ZH PDNH D PLQL 6FDQQHU IRU MXVW WKDW OLQH RI LQSXW DQG ZH XVH WRNHQ EDVHG SURFHVVLQJ FDOOLQJ WKH PHWKRGV nextInt next DQG nextDouble 7KLV QHZ YHUVLRQ RI WKH SURJUDP SURGXFHV WKH IROORZLQJ RXWSXW Total Total Total Total Total hours hours hours hours hours worked worked worked worked worked by by by by by Erica (id#101) = 42.75 Erin (id#783) = 55.75 Simone (id#114) = 24.0 Ryan (id#238) = 31.75 Kendall (id#156) = 5.5 :KLOH WKLV YHUVLRQ RI WKH SURJUDP LV D OLWWOH PRUH FRPSOH[ WKDQ WKH RULJLQDO LW LV PXFK PRUH IOH[LEOH EHFDXVH LW SD\V DWWHQWLRQ WR OLQH EUHDNV $GYDQFHG )LOH 3URFHVVLQJ ,Q WKLV VHFWLRQ ZH H[SORUH WZR DGYDQFHG WRSLFV UHODWHG WR ILOH SURFHVVLQJ )LUVW ZH VHH KRZ WR SURGXFH H[WHUQDO RXWSXW ILOHV 7KHQ ZH ORRN DW KRZ WR KDQGOH HUURUV XVLQJ WU\ FDWFK VWDWHPHQWV 360 2XWSXW )LOHV ZLWK 3ULQW6WUHDP $OO RI RXU SURJUDPV VR IDU KDYH VHQW WKHLU RXWSXW WR WKH FRQVROH ZLQGRZ E\ FDOOLQJ System.out.print RU System.out.println -XVW DV ZH FDQ UHDG LQSXW IURP DQ H[WHUQDO ILOH LQVWHDG RI UHDGLQJ IURP WKH FRQVROH ZH FDQ ZULWH RXWSXW WR DQ H[WHUQDO ILOH LQVWHDG RI ZULWLQJ LW WR WKH FRQVROH 7KHUH DUH PDQ\ ZD\V WR DFFRPSOLVK WKLV 7KH VLPSOHVW DSSURDFK LV WR WDNH DGYDQWDJH RI ZKDW \RX DOUHDG\ NQRZ %\ QRZ \RX YH OHDUQHG DOO DERXW KRZ print DQG println VWDWHPHQWV ZRUN :H FDQ OHYHUDJH WKDW NQRZOHGJH WR DOORZ \RX WR HDVLO\ FUHDWH H[WHUQDO RXWSXW ILOHV ,I \RX ORRN DW 6XQ V -DYD GRFXPHQWDWLRQ \RX ZLOO ILQG WKDW System.out LV D YDULDEOH WKDW VWRUHV D UHIHUHQFH WR DQ REMHFW RI W\SH PrintStream 7KH print DQG println VWDWHPHQWV \RX YH EHHQ ZULWLQJ DUH FDOOV RQ PHWKRGV WKDW DUH SDUW RI WKH PrintStream FODVV 7KH YDULDEOH System.out VWRUHV D UHIHUHQFH WR D VSHFLDO PrintStream REMHFW WKDW LV WLHG WR WKH FRQVROH ZLQGRZ %XW ZH FDQ FRQVWUXFW RWKHU PrintStream REMHFWV WKDW VHQG WKHLU RXWSXW WR RWKHU SODFHV 6XSSRVH IRU H[DPSOH WKDW ZH ZDQW WR VHQG RXWSXW WR D ILOH FDOOHG UHVXOWV W[W :H FDQ FRQVWUXFW D PrintStream REMHFW DV IROORZV PrintStream output = new PrintStream(new File("results.txt")); 7KLV ORRNV D ORW OLNH WKH OLQH RI FRGH ZH YH EHHQ XVLQJ WR FRQVWUXFW D Scanner WLHG WR DQ H[WHUQDO LQSXW ILOH ,Q WKLV FDVH WKH FRPSXWHU LV FUHDWLQJ DQ H[WHUQDO RXWSXW ILOH ,I WKHUH LV QR VXFK ILOH WKHQ WKH SURJUDP FUHDWHV LW ,I WKHUH LV VXFK D ILOH LW RYHUZULWHV WKH FXUUHQW YHUVLRQ ,QLWLDOO\ WKH ILOH ZLOO EH HPSW\ ,W ZLOO HQG XS FRQWDLQLQJ ZKDWHYHU RXWSXW \RX WHOO LW WR SURGXFH WKURXJK FDOOV RQ print DQG println 7KLV OLQH RI FRGH FDQ JHQHUDWH DQ H[FHSWLRQ LI -DYD LV XQDEOH WR FUHDWH WKH ILOH \RX YH GHVFULEHG 7KHUH DUH PDQ\ UHDVRQV WKLV PLJKW KDSSHQ <RX PLJKW QRW KDYH ZULWH DFFHVV WR WKH GLUHFWRU\ RU WKH ILOH PLJKW EH ORFNHG LQ VRPH ZD\ EHFDXVH LW LV EHLQJ XVHG E\ DQRWKHU SURJUDP -XVW DV ZLWK WKH OLQH RI FRGH WKDW FUHDWHV D ILOH EDVHG Scanner WKLV OLQH RI FRGH SRWHQWLDOO\ WKURZV D FileNotFoundException 7KDW PHDQV WKDW -DYD ZLOO UHTXLUH XV WR KDYH WKH throws FODXVH LQ ZKDWHYHU PHWKRG FRQWDLQV WKLV OLQH RI FRGH 7KH VLPSOHVW DSSURDFK LV WR KDYH WKLV OLQH RI FRGH LQ main ,Q IDFW LW LV FRPPRQ SUDFWLFH WR KDYH WKH main PHWKRG EHJLQ ZLWK WKH OLQHV RI FRGH WKDW GHDO ZLWK WKHVH H[WHUQDO ILOHV ERWK LQSXW DQG RXWSXW 2QFH \RX KDYH FRQVWUXFWHG D PrintStream REMHFW KRZ GR \RX XVH LW" <RX DOUHDG\ KDYH D JRRG LGHD RI ZKDW WR GR :H KDYH EHHQ PDNLQJ FDOOV RQ 6\VWHP RXW SULQW DQG 6\VWHP RXW SULQWOQ VLQFH &KDSWHU $V LW WXUQV RXW 6\VWHP RXW LV WKH QDPH RI D YDULDEOH WKDW VWRUHV D UHIHUHQFH WR D 3ULQW6WUHDP REMHFW 6R \RX KDYH KDG TXLWH D ELW RI SUDFWLFH DOUHDG\ WDONLQJ WR WKH 3ULQW6WUHDP NQRZQ DV 6\VWHP RXW 2XU SODQ LV WR KDYH D YDULDEOH FDOOHG RXWSXW WKDW ZLOO VWRUH D UHIHUHQFH WR RXU 3ULQW6WUHDP REMHFW ,I \RX MXVW WKLQN RI HYHU\WKLQJ WKDW \RX NQRZ DERXW 6\VWHP RXW \RX OO KDYH D JRRG LGHD RI ZKDW WR GR %XW IRU WKLV SURJUDP \RX ZLOO FDOO RXWSXW SULQW LQVWHDG RI 6\VWHP RXW SULQW DQG RXWSXW SULQWOQ LQVWHDG RI 6\VWHP RXW SULQWOQ $V D VLPSOH H[DPSOH UHPHPEHU WKDW LQ &KDSWHU ZH ORRNHG DW WKH IROORZLQJ YDULDWLRQ RI WKH VLPSOH KHOOR ZRUOG SURJUDP WKDW SURGXFHV VHYHUDO OLQHV RI RXWSXW 361 1 2 3 4 5 6 7 public class Hello3 { public static void main(String args) { System.out.println("Hello, world!"); System.out.println(); System.out.println("This program produces three lines of output."); } } %HORZ LV D YDULDWLRQ WKDW VHQGV LWV RXWSXW WR D ILOH FDOOHG hello.txt 1 2 3 4 5 6 7 8 9 10 11 import java.io.*; import java.util.*; public class Hello4 { public static void main(String args) throws FileNotFoundException { PrintStream output = new PrintStream(new File("hello.txt")); output.println("Hello world."); output.println(); output.println("This program produces three lines of output."); } } :KHQ \RX UXQ WKLV QHZ YHUVLRQ RI WKH SURJUDP D FXULRXV WKLQJ KDSSHQV 7KH SURJUDP GRHVQ W VHHP WR GR DQ\WKLQJ QR RXWSXW DSSHDUV RQ WKH FRQVROH DW DOO 7KDW V EHFDXVH ZH UH XVHG WR KDYLQJ WKH RXWSXW RI WKH SURJUDP JR WR WKH FRQVROH ZLQGRZ ,Q WKLV FDVH WKH RXWSXW ZDV GLUHFWHG WR D ILOH LQVWHDG $IWHU WKH SURJUDP ILQLVKHV H[HFXWLQJ ZH FDQ RSHQ XS WKH ILOH FDOOHG hello.txt DQG ZH OO ILQG WKDW LW FRQWDLQV WKH IROORZLQJ Hello world. This program produces three lines of output. 7KH PDLQ SRLQW LV WKDW HYHU\WKLQJ \RX YH OHDUQHG WR GR ZLWK System.out \RX FDQ DOVR GR ZLWK WKHVH PrintStream REMHFWV WKDW DUH WLHG WR ILOHV :H FDQ DOVR ZULWH PHWKRGV WKDW WDNH PrintStream REMHFWV DV SDUDPHWHUV )RU H[DPSOH FRQVLGHU WKH WDVN RI IL[LQJ WKH VSDFLQJ IRU D VHULHV RI ZRUGV :H PLJKW KDYH D OLQH RI WH[W WKDW KDV HUUDWLF VSDFLQJ DV LQ a new nation, conceived in liberty 6XSSRVH ZH ZDQW WR SULQW WKLV WH[W ZLWK WKH VSDFLQJ IL[HG VR WKDW WKHUH LV H[DFWO\ RQH VSDFH EHWZHHQ HDFK SDLU RI ZRUGV a new nation, conceived in liberty +RZ GR ZH GR WKDW" $VVXPH WKDW ZH DUH ZULWLQJ D PHWKRG WKDW LV SDVVHG D String WR HFKR DQG D PrintStream REMHFW WR VHQG WKH RXWSXW WR public static void echoFixed(String text, PrintStream output) 362 :H FDQ FRQVWUXFW D Scanner IURP WKH String DQG WKHQ XVH WKH next PHWKRG WR UHDG RQH ZRUG DW D WLPH 5HFDOO WKDW WKH Scanner FODVV LJQRUHV ZKLWHVSDFH VR ZH OO JHW MXVW WKH LQGLYLGXDO ZRUGV ZLWKRXW DOO RI WKH VSDFHV EHWZHHQ WKHP $V ZH UHDG ZRUGV ZH QHHG WR HFKR WKHP WR WKH PrintStream REMHFW +HUH V D ILUVW DWWHPSW Scanner data = new Scanner(text); while (data.hasNext()) { output.print(data.next()); } 7KLV FRGH GRHV D JUHDW MRE RI GHOHWLQJ WKH ORQJ VHTXHQFHV RI VSDFHV IURP WKH String EXW LW JRHV WRR IDU ,W HOLPLQDWHV DOO RI WKH VSDFHV :H ZDQW RQH VSDFH EHWZHHQ HDFK SDLU RI ZRUGV VR ZH KDYH WR LQFOXGH VRPH VSDFHV IRU RXU RXWSXW Scanner data = new Scanner(text); while (data.hasNext()) { output.print(data.next() + " "); } 7KLV HQGV XS ORRNLQJ SUHWW\ JRRG EXW LW SULQWV DQ H[WUD VSDFH DW WKH HQG RI WKH OLQH ,I ZH ZDQW WR JHW ULG RI WKDW VSDFH VR WKDW ZH WUXO\ KDYH VSDFHV DSSHDULQJ RQO\ EHWZHHQ SDLUV RI ZRUGV ZH KDYH WR FKDQJH WKLV VOLJKWO\ 7KLV LV D FODVVLF IHQFHSRVW SUREOHP :H ZDQW WKH VSDFHV EHWZHHQ WKH ZRUGV VR ZH KDYH RQH PRUH ZRUG WKDQ ZH KDYH VSDFHV :H FDQ XVH WKH W\SLFDO VROXWLRQ RI SURFHVVLQJ WKH ILUVW ZRUG EHIRUH WKH ORRS EHJLQV DQG WXUQLQJ DURXQG WKH RUGHU RI WKH RWKHU WZR RSHUDWLRQV LQVLGH WKH ORRS SULQW D VSDFH DQG WKHQ WKH ZRUG Scanner data = new Scanner(text); output.print(data.next()); while (data.hasNext()) { output.print(" " + data.next()); } 7KLV QRZ ZRUNV ZHOO IRU DOPRVW DOO FDVHV %\ LQFOXGLQJ RXU IHQFHSRVW VROXWLRQ RI HFKRLQJ WKH ILUVW ZRUG EHIRUH WKH ORRS EHJLQV ZH YH LQWURGXFHG DQ DVVXPSWLRQ WKDW WKHUH LV D ILUVW ZRUG ,I WKH String KDV QR ZRUGV DW DOO WKHQ WKLV FDOO RQ next ZLOO WKURZ DQ H[FHSWLRQ 6R ZH QHHG D WHVW IRU WKH FDVH ZKHUH WKH String KDV QR ZRUGV DW DOO $QG LI ZH DOVR ZDQW WKLV WR SURGXFH D FRPSOHWH OLQH RI RXWSXW ZH KDYH WR LQFOXGH D FDOO RQ println WR FRPSOHWH WKH OLQH RI RXWSXW DIWHU SULQWLQJ WKH LQGLYLGXDO ZRUGV 3XWWLQJ DOO RI WKLV WRJHWKHU ZH JHW WKH IROORZLQJ public static void echoFixed(String text, PrintStream output) { Scanner data = new Scanner(text); if (data.hasNext()) { output.print(data.next()); while (data.hasNext()) { output.print(" " + data.next()); } } output.println(); } 363 1RWLFH KRZ ZH DUH QRZ FDOOLQJ RXWSXW SULQW DQG RXWSXW SULQWOQ LQVWHDG RI FDOOLQJ System.out.print DQG System.out.println $Q LQWHUHVWLQJ WKLQJ DERXW WKLV PHWKRG LV WKDW LW FDQ EH XVHG WR VHQG RXWSXW WR DQ H[WHUQDO RXWSXW ILOH EXW LW FDQ DOVR EH XVHG WR VHQG RXWSXW WR System.out 7KH PHWKRG KHDGHU LQGLFDWHV WKDW LW ZRUNV RQ DQ\ PrintStream 6R ZH FDQ FDOO LW WR VHQG RXWSXW WR D PrintStream REMHFW WLHG WR DQ H[WHUQDO ILOH RU ZH FDQ FDOO LW WR VHQG RXWSXW WR System.out %HORZ LV D FRPSOHWH SURJUDP WKDW XVHV WKLV PHWKRG WR IL[ WKH VSDFLQJ LQ DQ HQWLUH LQSXW ILOH RI WH[W 7R XQGHUVFRUH WKH IOH[LELOLW\ RI WKH PHWKRG WKH SURJUDP VHQGV LWV RXWSXW WR ERWK WKH H[WHUQDO ILOH DQG WKH FRQVROH 6R \RX OO VHH WKH RXWSXW DSSHDU LQ WKH FRQVROH ZLQGRZ DV XVXDO EXW \RX OO DOVR ILQG WKDW D ILOH KDV EHHQ FRQVWUXFWHG FDOOHG words2.txt WKDW FRQWDLQV WKH RXWSXW 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 import java.io.*; import java.util.*; public class FixSpacing { public static void main(String args) throws FileNotFoundException { Scanner input = new Scanner(new File("words.txt")); PrintStream output = new PrintStream(new File("words2.txt")); while (input.hasNextLine()) { String text = input.nextLine(); echoFixed(text, output); echoFixed(text, System.out); } } public static void echoFixed(String text, PrintStream output) { Scanner data = new Scanner(text); if (data.hasNext()) { output.print(data.next()); while (data.hasNext()) { output.print(" " + data.next()); } } output.println(); } } *LYHQ WKH IROORZLQJ LQSXW ILOH four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty and dedicated to the proposition that all men are created equal ,W SURGXFHV WKH IROORZLQJ RXWSXW ILOH FDOOHG words2.txt four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty and dedicated to the proposition that all men are created equal 364 7U\ &DWFK 6WDWHPHQWV ,QFOXGLQJ WKH FODXVH throws FileNotFoundException LQ WKH KHDGHU IRU main DOORZV RXU SURJUDPV WR FRPSLOH EXW LW V QRW D YHU\ VDWLVI\LQJ VROXWLRQ WR WKH XQGHUO\LQJ SUREOHP 7R DFWXDOO\ KDQGOH WKH SRWHQWLDO HUURU ZH G ZDQW WR XVH VRPHWKLQJ FDOOHG D try catch VWDWHPHQW :H ZLOO QRW EH H[SORULQJ DOO RI WKH GHWDLOV RI try catch EXW ZH ZLOO H[DPLQH KRZ WR ZULWH VRPH EDVLF try catch VWDWHPHQWV WKDW ZH FRXOG XVH IRU ILOH SURFHVVLQJ 7KH JHQHUDO V\QWD[ RI WKH try catch VWDWHPHQW LV WKH IROORZLQJ try { <statement>; <statement>; ... <statement>; } catch (<type> <name>) { <statement>; <statement>; ... <statement>; } 1RWLFH WKDW LW LV GLYLGHG LQWR WZR EORFNV XVLQJ WKH NH\ZRUGV try DQG catch 7KH ILUVW EORFN KDV WKH FRGH \RX ZDQW WR H[HFXWH 7KH VHFRQG EORFN KDV HUURU UHFRYHU\ FRGH WKDW VKRXOG EH H[HFXWHG LI DQ H[FHSWLRQ LV WKURZQ 6R WKLQN RI WKLV DV VD\LQJ 7U\ WR H[HFXWH WKHVH VWDWHPHQWV EXW LI VRPHWKLQJ JRHV ZURQJ , P JRLQJ WR JLYH \RX VRPH RWKHU FRGH LQ WKH catch SDUW WKDW \RX VKRXOG H[HFXWH LI DQ HUURU RFFXUV 1RWLFH WKDW WKH FDWFK SDUW RI WKLV VWDWHPHQW KDV D VHW RI SDUHQWKHVHV LQ ZKLFK \RX LQFOXGH D W\SH DQG QDPH 7KH W\SH VKRXOG EH WKH W\SH RI H[FHSWLRQ \RX DUH WU\LQJ WR FDWFK 7KH QDPH FDQ EH DQ\ OHJDO LGHQWLILHU )RU H[DPSOH LQ WKH FDVH RI RXU Scanner FRGH ZH NQRZ WKDW D FileNotFoundException PLJKW EH WKURZQ :KDW GR ZH GR LI WKH H[FHSWLRQ RFFXUV" 7KDW V D WRXJK TXHVWLRQ EXW IRU QRZ OHW V MXVW ZULWH DQ HUURU PHVVDJH try { Scanner input = new Scanner(new File("numbers.dat")); } catch (FileNotFoundException e) { System.out.println("File not found"); } 7KLV FRGH VD\V WR WU\ FRQVWUXFWLQJ WKH Scanner IURP WKH ILOH numbers.dat EXW LI WKH ILOH LV QRW IRXQG WKHQ SULQW DQ HUURU PHVVDJH LQVWHDG 7KLV LV WKH EDVLF LGHD ZH ZDQW WR IROORZ EXW WKHUH DUH VHYHUDO LVVXHV ZH PXVW DGGUHVV WR PDNH WKLV FRGH ZRUN IRU XV )LUVW RI DOO WKHUH LV D VFRSH LVVXH 7KH YDULDEOH LQSXW LVQ W JRLQJ WR EH PXFK XVH WR XV LI LW V WUDSSHG LQVLGH WKH WU\ EORFN 6R ZH KDYH WR GHFODUH WKH Scanner YDULDEOH RXWVLGH WKH try catch VWDWHPHQW Scanner input; try { input = new Scanner(new File("numbers.dat")); } catch (FileNotFoundException e) { System.out.println("File not found"); } 365 :H KDYH D ELJJHU SUREOHP LQ WKDW VLPSO\ SULQWLQJ DQ HUURU PHVVDJH LVQ W D JRRG ZD\ WR UHFRYHU IURP WKLV SUREOHP +RZ LV WKH SURJUDP VXSSRVHG WR SURFHHG ZLWK H[HFXWLRQ LI LW FDQ W UHDG IURP WKH ILOH" ,W SUREDEO\ FDQ W 6R ZKDW ZRXOG EH D PRUH DSSURSULDWH ZD\ WR UHFRYHU IURP WKH HUURU" 7KDW GHSHQGV D ORW RQ WKH SDUWLFXODU SURJUDP \RX DUH ZULWLQJ VR WKH DQVZHU LV OLNHO\ WR YDU\ IURP RQH SURJUDP WR WKH QH[W /HW V H[SORUH KRZ \RX PLJKW KDQGOH WKLV ZKHQ \RX DUH SURPSWLQJ WKH XVHU IRU D ILOH QDPH LQ WKH FRQVROH ZLQGRZ ,Q WKDW FDVH ZH FRXOG NHHS SURPSWLQJ WKH XVHU XQWLO WKH\ JLYH XV D OHJDO ILOH QDPH /HW V EHJLQ E\ PRGLI\LQJ WKH FRGH DERYH WR SURPSW DQG UHDG D ILOH QDPH Scanner input; System.out.print("What is the name of the input file? "); String name = console.nextLine(); try { input = new Scanner(new File(name)); } catch (FileNotFoundException e) { System.out.println("File not found"); } 7KLV FRGH FDWFKHV WKH SRWHQWLDO H[FHSWLRQ DQG SULQWV DQ HUURU PHVVDJH EXW ZH ZDQW WR DGG D ORRS WKDW H[HFXWHV ZKLOH WKH XVHU KDV QRW JLYHQ XV D OHJDO ILOH QDPH :H ZDQW LW WR ORRN VRPHWKLQJ OLNH WKLV Scanner input; while (user hasn't given a legal name) { <prompt for name> <try to open file, generating error message if illegal> } :H KDYH D FODVVLF SUREOHP RI KRZ WR SULPH WKLV ORRS VR WKDW LW HQWHUV WKH ILUVW WLPH WKURXJK :H UH WU\LQJ WR FRQVWUXFW D Scanner IURP D ILOH :KHQ ZH VXFFHHG ZH OO EH JLYLQJ D YDOXH WR WKH YDULDEOH input &DQ ZH LQLWLDOL]H LQSXW WR VRPHWKLQJ WKDW ZRXOG LQGLFDWH WKDW ZH DUHQ W \HW GRQH" 7KH DQVZHU LV \HV 7KHUH LV D VSHFLDO NH\ZRUG LQ -DYD FDOOHG null WKDW LV XVHG WR UHSUHVHQW QR REMHFW :H FDQ LQLWLDOL]H WKH YDULDEOH LQSXW WR QXOO DV D ZD\ WR VD\ 7KLV YDULDEOH GRHVQ W \HW SRLQW WR DQ DFWXDO REMHFW 7KH SULPDU\ DGYDQWDJH RI LQLWLDOL]LQJ WKH YDULDEOH WR QXOO LV WKDW ZH FDQ WHVW ZKHWKHU LW V QXOO LQ WKH ZKLOH ORRS Scanner input = null; while (input == null) { <prompt for name> <try to open file, generating error message if illegal> } null $ -DYD NH\ZRUG VLJQLI\LQJ WKH DEVHQFH RI DQ REMHFW $VVLJQLQJ DQ REMHFW YDULDEOH WR null OHDYHV LW LQ DQ XQXVDEOH VWDWH XQWLO D QRQ null YDOXH LV ODWHU VWRUHG WKHUH 366 :H VWDUW WKH YDULDEOH ZLWK WKH YDOXH QXOO VR LW HQWHUV WKH ZKLOH ORRS WKH ILUVW WLPH WKURXJK ,I WKH FRGH LQ WKH try catch IDLOV WR SURSHUO\ RSHQ WKH ILOH WKHQ WKH YDULDEOH ZLOO VWLOO EH QXOO DQG ZH OO H[HFXWH WKH ORRS D VHFRQG WLPH SURPSWLQJ IRU DQRWKHU ILOH QDPH DQG WU\LQJ WR RSHQ LW ,I WKH FRGH LQ WKH try catch IDLOV DJDLQ WKHQ ZH JHQHUDWH \HW DQRWKHU HUURU PHVVDJH DQG JR WKURXJK WKH ORRS D WKLUG WLPH :H FDQ FRPELQH WKLV SVHXGRFRGH ZLWK WKH try catch FRGH ZH VDZ HDUOLHU ,W VHHPV SUXGHQW WR PRGLI\ WKH HUURU PHVVDJH WR PDNH LW FOHDU WKDW WKH XVHU LV EHLQJ JLYHQ DQRWKHU FKDQFH WR HQWHU D OHJDO ILOH QDPH Scanner input = null; while (input == null) { System.out.print("What is the name of the input file? "); String name = console.nextLine(); try { input = new Scanner(new File(name)); } catch (FileNotFoundException e) { System.out.println("File not found. Please try again."); } } 7KLV ORRS H[HFXWHV UHSHDWHGO\ XQWLO WKH FDOO RQ new Scanner LQVLGH WKH WU\ EORFN VXFFHHGV DQG JLYHV WKH YDULDEOH LQSXW D QRQ QXOO YDOXH 7KLV FRGH FRXOG EH LQFOXGHG LQ PHWKRG main DOWKRXJK ZH G KDYH WR FRQVWUXFW D Scanner IRU FRQVROH LQSXW WR EH DEOH WR SURPSW WKH XVHU IRU D ILOH QDPH %HORZ LV D YDULDWLRQ RI WKH +RXUV:RUNHG SURJUDP WKDW SURPSWV IRU D ILOH QDPH 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 // Variation of HoursWorked2 that prompts for a file name import java.io.*; import java.util.*; public class HoursWorked3 { public static void main(String args) { Scanner console = new Scanner(System.in); Scanner input = null; while (input == null) { System.out.print("What is the name of the input file? "); String name = console.nextLine(); try { input = new Scanner(new File(name)); } catch (FileNotFoundException e) { System.out.println("File not found. Please try again."); } } while (input.hasNextLine()) { String text = input.nextLine(); processLine(text); } } // Processes the given String (id, name and hours worked) public static void processLine(String text) { Scanner data = new Scanner(text); int id = data.nextInt(); String name = data.next(); double sum = 0.0; while (data.hasNextDouble()) { 367 32 33 34 35 36 37 sum += data.nextDouble(); } System.out.println("Total hours worked by " + name + " (id#" + id + ") = " + sum); } } 1RWLFH WKDW ZH QR ORQJHU QHHG WKH throws FileNotFoundException LQ WKH KHDGHU IRU main EHFDXVH ZH KDQGOH WKH SRWHQWLDO H[FHSWLRQ +HUH LV D ORJ RI H[HFXWLRQ VKRZLQJ ZKDW KDSSHQV ZKHQ WKH XVHU W\SHV LQ LOOHJDO ILOH QDPHV What is the name of the input file? ours2.dat File not found. Please try again. What is the name of the input file? hours2.txt File not found. Please try again. What is the name of the input file? data.txt File not found. Please try again. What is the name of the input file? file.dat File not found. Please try again. What is the name of the input file? hours2.dat Total hours worked by Erica (id#101) = 42.75 Total hours worked by Erin (id#783) = 55.75 Total hours worked by Simone (id#114) = 24.0 Total hours worked by Ryan (id#238) = 31.75 Total hours worked by Kendall (id#156) = 5.5 7KLV FRGH IRU RSHQLQJ D ILOH LV FRPSOLFDWHG HQRXJK WKDW \RX PLJKW ZDQW WR SXW LW LQ LWV RZQ VWDWLF PHWKRG %HORZ LV D ILQDO YDULDWLRQ WKDW LQFOXGHV D PHWKRG FDOOHG JHW,QSXW WKDW SURPSWV WKH XVHU IRU D OHJDO ILOH QDPH WKDW FDQ EH XVHG WR FRQVWUXFW D Scanner 368 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 // Variation of HoursWorked3 with file opening code in a method import java.io.*; import java.util.*; public class HoursWorked4 { public static void main(String args) { Scanner console = new Scanner(System.in); Scanner input = getInput(console); while (input.hasNextLine()) { String text = input.nextLine(); processLine(text); } } // prompt the user for a legal file name, create and return // a Scanner tied to the file public static Scanner getInput(Scanner console) { Scanner result = null; while (result == null) { System.out.print("What is the name of the input file? "); String name = console.nextLine(); try { result = new Scanner(new File(name)); } catch (FileNotFoundException e) { System.out.println("File not found. Please try again."); } } return result; } // Processes the given String (id, name and hours worked) public static void processLine(String text) { Scanner data = new Scanner(text); int id = data.nextInt(); String name = data.next(); double sum = 0.0; while (data.hasNextDouble()) { sum += data.nextDouble(); } System.out.println("Total hours worked by " + name + " (id#" + id + ") = " + sum); } } 7KH FRGH ZH KDYH ZULWWHQ IRU RSHQLQJ D ILOH WHQGV WR EH IDLUO\ VWDQGDUG LQ WKDW ZH FRXOG XVH LW ZLWKRXW PRGLILFDWLRQ LQ PDQ\ SURJUDPV :H UHIHU WR WKLV DV ERLOHUSODWH FRGH %RLOHUSODWH &RGH &RGH WKDW WHQGV WR EH WKH VDPH IURP RQH SURJUDP WR DQRWKHU 7KH PHWKRG JHW,QSXW LV D JRRG H[DPSOH RI WKH NLQG RI ERLOHUSODWH FRGH WKDW \RX PLJKW XVH LQ PDQ\ GLIIHUHQW ILOH SURFHVVLQJ SURJUDPV 369 &DVH 6WXG\ :HLJKWHG *3$ 7KH +RXUV:RUNHG SURJUDP UHTXLUHG WKDW QDPHV KDYH QR VSDFHV LQ WKHP 7KLV LVQ W D YHU\ SUDFWLFDO UHVWULFWLRQ ,W ZRXOG EH PRUH FRQYHQLHQW WR EH DEOH WR W\SH DQ\WKLQJ IRU D QDPH LQFOXGLQJ VSDFHV 2QH ZD\ WR GR WKDW LV WR SXW WKH QDPH RQ D VHSDUDWH OLQH IURP WKH UHVW RI WKH GDWD )RU H[DPSOH VXSSRVH WKDW \RX ZDQW WR FRPSXWH ZHLJKWHG *3$V IRU D VHULHV RI VWXGHQWV 6XSSRVH IRU H[DPSOH WKDW D VWXGHQW KDV D XQLW D XQLW D XQLW DQG D XQLW :H FDQ FRPSXWH DQ RYHUDOO *3$ WKDW LV ZHLJKWHG E\ WKH LQGLYLGXDO XQLWV IRU HDFK FRXUVH 6R ZH PLJKW KDYH DQ LQSXW ILOH WKDW KDV LWV GDWD RQ SDLUV RI OLQHV )RU HDFK SDLU WKH QDPH ZLOO DSSHDU RQ WKH ILUVW OLQH DQG WKH JUDGH GDWD ZLOO DSSHDU RQ WKH VHFRQG OLQH )RU H[DPSOH ZH PLJKW KDYH DQ LQSXW ILOH WKDW ORRNV OLNH WKLV Erica Kane 3 2.8 4 3.9 3 3.1 Greenlee Smythe 3 3.9 3 4.0 4 3.9 Ryan Laveree 2 4.0 3 3.6 4 3.8 1 2.8 Adam Chandler 3 3.0 4 2.9 3 3.2 2 2.5 Adam Chandler, Jr 4 1.5 5 1.9 7KLV GDWD LV OLQH EDVHG EXW VRPH RI WKH OLQHV KDYH LQGLYLGXDO WRNHQV WKDW ZH OO QHHG WR SURFHVV 6R ZH OO ZDQW WR XVH D VOLJKW YDULDWLRQ RI WKH WHPSODWH JLYHQ LQ WKH ODVW VHFWLRQ 5HFDOO WKDW LQ JHQHUDO WKH ZD\ WKDW ZH JHW D FRPELQDWLRQ RI OLQH EDVHG DQG WRNHQ EDVHG SURFHVVLQJ LV WR ZULWH FRGH OLNH WKLV while (input.hasNextLine()) { String text = input.nextLine(); Scanner data = new Scanner(text); <process data> } )RU WKLV ILOH RXU GDWD DSSHDUV DV SDLUV RI OLQHV ZKHUH WKH ILUVW OLQH VWRUHV WKH QDPH DQG WKH VHFRQG OLQH VWRUHV WKH JUDGH GDWD :H ZRQ W QHHG WR SURFHVV WKH QDPH WRNHQ E\ WRNHQ EXW ZH OO QHHG WR SURFHVV WKH JUDGHV DV LQGLYLGXDO WRNHQV 6R ZH FDQ XVH WKH IROORZLQJ VOLJKW YDULDWLRQ WKDW UHDGV WZR OLQHV RI GDWD HDFK WLPH WKURXJK WKH ORRS DQG WKDW FRQVWUXFWV D Scanner REMHFW IRU WKH VHFRQG OLQH RI LQSXW WKDW FRQWDLQV WKH JUDGH LQIRUPDWLRQ while (input.hasNextLine()) { String name = input.nextLine(); String grades = input.nextLine(); Scanner data = new Scanner(grades); <process this student's data> } 7R FRPSOHWH WKLV SURJUDP ZH KDYH WR ILJXUH RXW KRZ WR SURFHVV WKH JUDGH GDWD LQ RXU Scanner FDOOHG data 7KLV LV D JRRG SODFH WR LQWURGXFH D VWDWLF PHWKRG 7KH FRGH DERYH LQYROYHV SURFHVVLQJ WKH RYHUDOO ILOH 7KH WDVN RI SURFHVVLQJ RQH OLVW RI JUDGHV VKRXOG EH VSOLW RII LQWR LWV RZQ PHWKRG /HW V FDOO LW SURFHVV*UDGHV 2EYLRXVO\ LW FDQ W GR LWV ZRUN ZLWKRXW WKH Scanner REMHFW WKDW KDV WKH 370 JUDGHV VR ZH OO SDVV WKDW DV D SDUDPHWHU :KDW H[DFWO\ QHHGV WR EH GRQH" 7KH SODQ ZDV WR FRPSXWH D ZHLJKWHG *3$ IRU HDFK VWXGHQW 6R WKLV PHWKRG QHHGV WR UHDG WKH LQGLYLGXDO JUDGHV DQG WXUQ WKDW LQWR D VLQJOH *3$ VFRUH :HLJKWHG *3$V LQYROYH FRPSXWLQJ D YDOXH NQRZQ DV WKH TXDOLW\ SRLQWV IRU HDFK JUDGH 7KH TXDOLW\ SRLQWV DUH GHILQHG DV WKH XQLWV WLPHV WKH JUDGH 7KH ZHLJKWHG *3$ LV FDOFXODWHG E\ GLYLGLQJ WKH WRWDO TXDOLW\ SRLQWV E\ WKH WRWDO XQLWV 6R ZH MXVW QHHG WR DGG XS WKH WRWDO TXDOLW\ SRLQWV DQG DGG XS WKH WRWDO XQLWV WKHQ GLYLGH 7KLV LQYROYHV D SDLU RI FXPXODWLYH VXP WDVNV WKDW ZH FDQ H[SUHVV LQ SVHXGRFRGH DV IROORZV set total units to 0. set total quality points to 0. while (more grades to process) { read next units and next grade. add next units to total units. add (next units) * (next grade) to total quality points. } set gpa to (total quality points)/(total units). 7KLV LV IDLUO\ VLPSOH WR WUDQVODWH LQWR -DYD FRGH E\ LQFRUSRUDWLQJ RXU Scanner REMHFW FDOOHG data double totalQualityPoints = 0.0; double totalUnits = 0; while (data.hasNextInt()) { int units = data.nextInt(); double grade = data.nextDouble(); totalUnits += units; totalQualityPoints += units * grade; } double gpa = totalQualityPoints/totalUnits; %HFDXVH RXU Scanner REMHFW GDWD ZDV FRQVWUXFWHG IURP D VLQJOH OLQH RI LQSXW ZH FDQ SURFHVV MXVW RQH SHUVRQ V JUDGHV ZLWK WKLV ORRS 7KHUH LV VWLOO D SRWHQWLDO SUREOHP :KDW LI WKHUH DUH QR JUDGHV" 6RPH VWXGHQWV PLJKW KDYH GURSSHG DOO RI WKHLU FODVVHV IRU H[DPSOH 7KHUH DUH VHYHUDO ZD\V ZH PLJKW KDQGOH WKDW VLWXDWLRQ EXW OHW V DVVXPH WKDW LW LV DSSURSULDWH WR XVH D *3$ RI ZKHQ WKHUH DUH QR JUDGHV 0DNLQJ WKDW FRUUHFWLRQ DQG SXWWLQJ WKLV LQWR D PHWKRG ZH HQG XS ZLWK WKH IROORZLQJ FRGH 371 public static double processGrades(Scanner data) { double totalQualityPoints = 0.0; double totalUnits = 0; while (data.hasNextInt()) { int units = data.nextInt(); double grade = data.nextDouble(); totalUnits += units; totalQualityPoints += units * grade; } if (totalUnits == 0) { return 0.0; } else { return totalQualityPoints/totalUnits; } } 5HFDOO WKDW RXU KLJK OHYHO FRGH ORRNHG OLNH WKLV while (input.hasNextLine()) { String name = input.nextLine(); String grades = input.nextLine(); Scanner data = new Scanner(grades); <process this student's data> } :H FDQ QRZ VWDUW WR ILOO LQ WKH GHWDLOV RI ZKDW LW PHDQV WR SURFHVV WKLV VWXGHQW V GDWD :H ZLOO FDOO WKH PHWKRG ZH MXVW ZURWH WR SURFHVV WKH JUDGHV IRU WKLV VWXGHQW DQG WR WXUQ LW LQWR D ZHLJKWHG *3$ DQG WKHQ SULQW WKH UHVXOWV double gpa = processGrades(data); System.out.println("GPA for " + name + " = " + gpa); 7KLV ZRXOG FRPSOHWH WKH SURJUDP EXW OHW V DGG RQH PRUH FDOFXODWLRQ /HW V FRPSXWH WKH PD[ DQG PLQ *3$ WKDW ZH VHH DPRQJ WKHVH VWXGHQWV :H FDQ DFFRPSOLVK WKLV IDLUO\ HDVLO\ ZLWK VRPH VLPSOH LI VWDWHPHQWV DIWHU WKH println if (gpa max } if (gpa min } > max) { = gpa; < min) { = gpa; :H VLPSO\ FRPSDUH WKH FXUUHQW JSD DJDLQVW ZKDW ZH FXUUHQWO\ FRQVLGHU WKH PD[ DQG PLQ UHVHWWLQJ LI WKH QHZ JSD UHSUHVHQWV D QHZ PD[ RU D QHZ PLQ %XW KRZ GR ZH LQLWLDOL]H WKHVH YDULDEOHV" :H KDYH WZR DSSURDFKHV WR FKRRVH IURP 2QH DSSURDFK LQYROYHV LQLWLDOL]LQJ WKH PD[ DQG WKH PLQ WR WKH ILUVW YDOXH LQ WKH VHTXHQFH :H FRXOG GR WKDW EXW LW ZRXOG PDNH RXU ORRS PXFK PRUH FRPSOLFDWHG WKDQ LW LV FXUUHQWO\ 7KH VHFRQG DSSURDFK LQYROYHV VHWWLQJ WKH PD[ WR WKH ORZHVW SRVVLEOH YDOXH DQG VHWWLQJ WKH PLQ WR WKH KLJKHVW SRVVLEOH YDOXH 7KLV DSSURDFK LVQ W DOZD\V SRVVLEOH EHFDXVH ZH GRQ W DOZD\V NQRZ KRZ KLJK RU ORZ RXU YDOXHV PLJKW JR %XW LQ WKH FDVH RI *3$V ZH NQRZ WKDW WKH\ ZLOO DOZD\V EH EHWZHHQ DQG 7KXV ZH FDQ LQLWLDOL]H WKH YDULDEOHV DV IROORZV 372 double max = 0.0; double min = 4.0; ,W PD\ VHHP RGG WR VHW WKH PD[ WR DQG WKH PLQ WR EXW WKDW V EHFDXVH ZH DUH LQWHQGLQJ WR KDYH WKHP UHVHW LQVLGH WKH ORRS ,I WKH ILUVW VWXGHQW KDV D *3$ RI IRU H[DPSOH WKHQ WKLV ZLOO FRQVWLWXWH D QHZ PD[ KLJKHU WKDQ DQG D QHZ PLQ ORZHU WKDQ 2I FRXUVH LW V SRVVLEOH WKDW DOO VWXGHQWV HQG XS ZLWK D EXW WKHQ RXU FKRLFH RI IRU WKH PLQ LV DSSURSULDWH 2U DOO VWXGHQWV FRXOG HQG XS ZLWK D LQ ZKLFK FDVH RXU FKRLFH RI D PD[ RI LV DSSURSULDWH 3XWWLQJ WKLV DOO WRJHWKHU ZH JHW WKH IROORZLQJ FRPSOHWH SURJUDP 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 // // // // // // // // // // // // // // This program reads an input file with GPA data for a series of students and reports a weighted GPA for each. The input file should consist of a series of line pairs where the first line has a student's name and the second line has a series of grade entries. The grade entries should be a number of units (an integer) followed by a grade (a number between 0.0 and 4.0). For example, the input might look like this: Erica Kane 3 2.8 4 3.9 3 3.1 Ryan Laveree 2 4.0 3 3.6 4 3.8 1 2.8 The program reports the weighted GPA for each student along with the max and min GPA. import java.io.*; import java.util.*; public class Gpa { public static void main(String args) throws FileNotFoundException { Scanner input = new Scanner(new File("gpa.dat")); process(input); } public static void process(Scanner input) { double max = 0.0; double min = 4.0; while (input.hasNextLine()) { String name = input.nextLine(); String grades = input.nextLine(); Scanner data = new Scanner(grades); double gpa = processGrades(data); System.out.println("GPA for " + name + " = " + gpa); if (gpa > max) { max = gpa; } if (gpa < min) { min = gpa; } } System.out.println(); System.out.println("max GPA = " + max); System.out.println("min GPA = " + min); } public static double processGrades(Scanner data) { double totalQualityPoints = 0.0; 373 48 49 50 51 52 53 54 55 56 57 58 59 60 61 double totalUnits = 0; while (data.hasNextInt()) { int units = data.nextInt(); double grade = data.nextDouble(); totalUnits += units; totalQualityPoints += units * grade; } if (totalUnits == 0) { return 0.0; } else { return totalQualityPoints / totalUnits; } } } 2QFH DJDLQ RXU main PHWKRG KDV WKH throws FileNotFoundException LQ LWV KHDGHU 7KLV SURJUDP H[HFXWHV DV IROORZV DVVXPLQJ WKH GDWD DERYH LV SODFHG LQ D ILOH FDOOHG gpa.dat GPA GPA GPA GPA GPA for for for for for Erica Kane = 3.3299999999999996 Greenlee Smythe = 3.9299999999999997 Ryan Laveree = 3.6799999999999997 Adam Chandler = 2.9333333333333336 Adam Chandler, Jr = 1.7222222222222223 max GPA = 3.9299999999999997 min GPA = 1.7222222222222223 7KH SURJUDP FRXOG EH PRGLILHG WR VHQG LWV RXWSXW WR DQ H[WHUQDO ILOH VXFK DV JSD RXW 7KLV LV OHIW DV DQ H[HUFLVH &KDSWHU 6XPPDU\ $ Scanner REMHFW FDQ UHDG LQSXW IURP D ILOH UDWKHU WKDQ IURP WKH NH\ERDUG 7KLV LV DFKLHYHG E\ SDVVLQJ D new File(filename) WR WKH Scanner V FRQVWUXFWRU UDWKHU WKDQ SDVVLQJ System.in $ FKHFNHG H[FHSWLRQ LV D SURJUDP HUURU FRQGLWLRQ WKDW PXVW EH FKHFNHG IRU WKH SURJUDP WR FRPSLOH :KHQ FRQVWUXFWLQJ D Scanner WKDW UHDGV D ILOH ZH PXVW ZULWH WKH SKUDVH throws FileNotFoundException RQ WKH main PHWKRG V KHDGHU ,QSXW ILOHV DUH WUHDWHG E\ WKH Scanner DV D RQH GLPHQVLRQDO VWUHDP RI GDWD WKDW LV UHDG LQ RUGHU IURP VWDUW WR HQG 7KH LQSXW FXUVRU FRQVXPHV PRYHV SDVW LQSXW WRNHQV DV WKH\ DUH UHDG DQG UHWXUQV WKHP WR \RXU SURJUDP $ ILOH QDPH FDQ EH VSHFLILHG DV D UHODWLYH SDWK VXFK DV "data/text/numbers.dat" ZKLFK ZRXOG EH DVVXPHG WR H[LVW LQ WKH data/text/ VXEIROGHU RI WKH FXUUHQW GLUHFWRU\ $OVR D IXOO ILOH SDWK FDQ EH VSHFLILHG VXFK DV "C:/Documents and Settings/user/My Documents/data/text/ numbers.dat" ScannerV WKDW UHDG ILOHV PDNH XVH RI WKH YDULRXV hasNext PHWKRGV WR GLVFRYHU ZKHQ WKH ILOH V LQSXW KDV EHHQ H[KDXVWHG 374 0DQ\ ILOHV KDYH WKHLU LQSXW VWUXFWXUHG E\ OLQHV DQG LW PDNHV VHQVH WR SURFHVV WKRVH ILOHV OLQH E\ OLQH ,Q VXFK FDVHV LW LV FRPPRQ WR XVH QHVWHG ORRSV DQ RXWHU ORRS RYHU HDFK OLQH RI WKH ILOH DQG DQ LQQHU ORRS WKDW SURFHVVHV WKH WRNHQV LQ HDFK OLQH 2XWSXW WR D ILOH FDQ EH DFKLHYHG ZLWK D PrintStream REMHFW ZKLFK LV FRQVWUXFWHG ZLWK D File DQG KDV WKH VDPH PHWKRGV DV 6\VWHP RXW VXFK DV println DQG print -DYD KDV D try catch VWDWHPHQW WKDW FDQ EH XVHG WR FKHFN IRU HUURUV DQG KDQGOH WKHP ZKLOH WKH SURJUDP LV H[HFXWLQJ $ try catch VWDWHPHQW FDQ EH XVHG ZKHQ RSHQLQJ D ILOH WR KDQGOH WKH SRVVLELOLW\ WKDW WKH ILOH GRHV QRW H[LVW 6HOI &KHFN 3UREOHPV 6HFWLRQ )LOH 5HDGLQJ %DVLFV :KDW LV DQ H[WHUQDO ILOH" +RZ FDQ ZH UHDG GDWD IURP DQ H[WHUQDO ILOH LQ -DYD" :KDW LV ZURQJ ZLWK WKH IROORZLQJ OLQH RI FRGH" Scanner input = new Scanner("test.dat"); :ULWH FRGH WR FRQVWUXFW D 6FDQQHU REMHFW WR UHDG WKH ILOH LQSXW W[W ZKLFK H[LVWV LQ WKH VDPH IROGHU DV \RXU SURJUDP 6HFWLRQ 'HWDLOV RI 7RNHQ %DVHG 3URFHVVLQJ :KDW LV ZURQJ ZLWK WKH IROORZLQJ OLQH RI FRGH" Scanner input = new Scanner(new File("C:\temp\new files\test.dat")); +LQW 7U\ SULQWLQJ WKH DERYH String ,I \RXU -DYD SURJUDP LV ORFDWHG RQ D :LQGRZV PDFKLQH LQ WKH IROGHU C:\Documents and Settings\amanda\My Documents\programs DQVZHU WKH IROORZLQJ :KDW DUH WZR OHJDO ZD\V \RX FDQ UHIHU WR WKH ILOH C:\Documents and Settings\amanda\My Documents\numbers.dat " :KDW DERXW WKH ILOH C:\Documents and Settings\amanda\My Documents\programs\data\homework6\input.dat " " :KDW LV DUH +RZ PDQ\ OHJDO ZD\V FDQ \RX UHIHU WR WKH ILOH C:\Documents and Settings\amanda\My Documents\homework\data.txt WKH\" ,I \RXU -DYD SURJUDP LV ORFDWHG RQ D /LQX[ PDFKLQH LQ WKH IROGHU /home/amanda/Documents/ hw6 DQVZHU WKH IROORZLQJ :KDW DUH WZR OHJDO ZD\V \RX FDQ UHIHU WR WKH ILOH /home/amanda/Documents/hw6/ names.txt " :KDW DERXW WKH ILOH /home/amanda/Documents/hw6/data/numbers.txt " 375 +RZ PDQ\ OHJDO ZD\V FDQ \RX UHIHU WR WKH ILOH /home/amanda/download/saved.html " :KDW LV DUH WKH\" 6HFWLRQ /LQH EDVHG 3URFHVVLQJ )RU WKH QH[W VHYHUDO TXHVWLRQV FRQVLGHU D ILOH QDPHG readme.txt WKDW FRQWDLQV WKH IROORZLQJ FRQWHQWV 6.7 This file has several input lines. 30 40 10 20 test :KDW ZRXOG EH WKH RXWSXW IURP WKH IROORZLQJ FRGH ZKHQ UXQ RQ WKH DERYH readme.txt ILOH" Scanner input = new Scanner(new File("readme.txt")); int count = 0; while (input.hasNextLine()) { System.out.println("input: " + input.nextLine()); count++; } System.out.println(count + " total"); :KDW ZRXOG EH WKH RXWSXW IURP WKH FRGH LQ WKH SUHYLRXV H[HUFLVH LI WKH FDOOV WR hasNextLine DQG nextLine DUH UHSODFHG E\ FDOOV WR hasNext DQG next UHVSHFWLYHO\" :KDW ZRXOG EH WKH RXWSXW IURP WKH FRGH LQ WKH SUHYLRXV H[HUFLVH LI WKH FDOOV WR hasNextLine DQG nextLine DUH UHSODFHG E\ FDOOV WR hasNextInt DQG nextInt UHVSHFWLYHO\" +RZ DERXW hasNextDouble DQG nextDouble" :ULWH D SURJUDP WKDW SULQWV LWVHOI WR WKH FRQVROH DV RXWSXW 7KDW LV LI WKH SURJUDP LV VWRUHG LQ ([DPSOH MDYD LW ZLOO RSHQ WKH ILOH ([DPSOH MDYD DQG SULQW LWV FRQWHQWV :ULWH FRGH WKDW SURPSWV WKH XVHU IRU D ILOH QDPH DQG SULQWV WKH FRQWHQWV RI WKDW ILOH WR WKH FRQVROH DV RXWSXW $VVXPH WKDW WKH ILOH H[LVWV <RX PD\ ZLVK WR SODFH WKLV FRGH LQWR D PHWKRG QDPHG printEntireFile 6HFWLRQ $GYDQFHG )LOH 3URFHVVLQJ :KDW REMHFW LV XVHG WR ZULWH RXWSXW LQWR DQ H[WHUQDO ILOH" :KDW PHWKRGV GRHV WKLV REMHFW KDYH DYDLODEOH IRU \RX WR XVH" :ULWH FRGH WR SULQW WKH IROORZLQJ IRXU OLQHV RI WH[W LQWR D ILOH QDPHG PHVVDJH W[W Testing, 1, 2, 3. This is my output file. 376 :ULWH FRGH WKDW UHSHDWHGO\ SURPSWV WKH XVHU IRU D ILOH QDPH XQWLO WKH XVHU W\SHV WKH QDPH RI D ILOH WKDW H[LVWV RQ WKH V\VWHP <RX PD\ ZLVK WR SODFH WKLV FRGH LQWR D PHWKRG QDPHG getFileName ZKLFK ZRXOG UHWXUQ WKDW ILOH QDPH DV D String $ SUHYLRXV SUREOHP KDG \RX ZULWH D SLHFH RI FRGH WR SURPSW WKH XVHU IRU D ILOH QDPH DQG SULQW WKDW ILOH V FRQWHQWV WR WKH FRQVROH 0RGLI\ \RXU FRGH VR WKDW LW ZLOO UHSHDWHGO\ SURPSW WKH XVHU IRU WKH ILOH QDPH XQWLO WKH XVHU W\SHV D YDOLG ILOH WKDW H[LVWV ([HUFLVHV :ULWH D PHWKRG QDPHG doubleSpace WKDW DFFHSWV WZR StringV UHSUHVHQWLQJ ILOH QDPHV DV LWV SDUDPHWHUV ZULWLQJ LQWR WKH VHFRQG ILOH D GRXEOH VSDFHG YHUVLRQ RI WKH WH[W LQ WKH ILUVW ILOH <RX FDQ DFKLHYH WKLV E\ LQVHUWLQJ D EODQN OLQH EHWZHHQ HDFK OLQH RI RXWSXW 7R PDNH LW PRUH FKDOOHQJLQJ WU\ WR PDNH \RXU FRGH VR WKDW LW ZRXOG ZRUN HYHQ LI WKH WZR ZHUH WKH VDPH LQ RWKHU ZRUGV WR ZULWH WKH GRXEOH VSDFHG RXWSXW EDFN LQWR WKH VDPH ILOH StringV :ULWH D PHWKRG QDPHG wordWrap WKDW DFFHSWV D Scanner UHSUHVHQWLQJ DQ LQSXW ILOH DV LWV SDUDPHWHU DQG RXWSXWV HDFK OLQH RI WKH ILOH WR WKH FRQVROH ZRUG ZUDSSLQJ DOO OLQHV WKDW DUH ORQJHU WKDQ FKDUDFWHUV )RU H[DPSOH LI D OLQH FRQWDLQV FKDUDFWHUV UHSODFH LW E\ WZR OLQHV RQH FRQWDLQLQJ WKH ILUVW FKDUDFWHUV DQG DQRWKHU FRQWDLQLQJ WKH ILQDO FKDUDFWHUV $ OLQH FRQWDLQLQJ FKDUDFWHUV ZRXOG EH ZUDSSHG LQWR IRXU OLQHV WKUHH RI OHQJWK DQG D ILQDO OLQH RI OHQJWK 0RGLI\ WKH SUHFHGLQJ ZRUG ZUDS PHWKRG VR WKDW LW RXWSXWV WKH QHZO\ ZUDSSHG WH[W EDFN LQWR WKH RULJLQDO ILOH %H FDUHIXO GRQ W RXWSXW LQWR D ILOH ZKLOH \RX DUH UHDGLQJ LW $OVR PRGLI\ LW WR XVH D FODVV FRQVWDQW IRU WKH PD[LPXP OLQH OHQJWK UDWKHU WKDQ KDUG FRGLQJ 0RGLI\ WKH SUHFHGLQJ ZRUG ZUDS PHWKRG VR WKDW LW RQO\ ZUDSV ZKROH ZRUGV QHYHU FKRSSLQJ D ZRUG LQ KDOI $VVXPH WKDW D ZRUG LV DQ\ ZKLWHVSDFH VHSDUDWHG WRNHQ DQG WKDW DOO ZRUGV DUH XQGHU FKDUDFWHUV LQ OHQJWK :ULWH D SURJUDP WKDW SURPSWV WKH XVHU IRU D ILOH QDPH WKHQ UHDGV WKDW ILOH DVVXPLQJ WKDW LWV FRQWHQWV FRQVLVW HQWLUHO\ RI LQWHJHUV DQG SULQWV WKH PD[LPXP PLQLPXP VXP FRXQW QXPEHU RI LQWHJHUV LQ WKH ILOH DQG DYHUDJH RI WKH QXPEHUV )RU H[DPSOH LI WKH ILOH numberinput.dat KDV WKH IROORZLQJ FRQWHQWV 4 -2 18 15 31 27 <RXU SURJUDP ZRXOG SURGXFH WKH IROORZLQJ RXWSXW What is the name of the input file? numberinput.dat Maximum = 31 Minimum = -2 Sum = 93 Count = 6 Average = 15.5 377 0RGLI\ WKH SUHYLRXV SURJUDP VR WKDW LW ZRUNV HYHQ RQ DQ LQSXW ILOH WKDW FRQWDLQV QRQ LQWHJHU WRNHQV <RXU FRGH VKRXOG VNLS RYHU DQ\ WRNHQV WKDW DUH QRW YDOLG LQWHJHUV )RU H[DPSOH LI WKH ILOH numberinput2.dat KDV WKH IROORZLQJ FRQWHQWV 4 billy bob -2 18 15 31 NotANumber true 'c' 27 2.54 <RXU SURJUDP ZRXOG SURGXFH WKH VDPH RXWSXW DV LQ WKH SUHYLRXV H[HUFLVH :ULWH D PHWKRG QDPHG collapseSpaces WKDW DFFHSWV D Scanner UHSUHVHQWLQJ DQ LQSXW ILOH DV LWV SDUDPHWHU WKHQ UHDGV WKDW ILOH DQG RXWSXWV LW ZLWK DOO LWV WRNHQV VHSDUDWHG E\ D VLQJOH VSDFH <RXU FRGH ZLOO FROODSVH VHTXHQFHV RI PXOWLSOH VSDFHV LQWR D VLQJOH VSDFH 7KDW LV LI D OLQH RI WKH ILOH FRQWDLQV WKH IROORZLQJ WH[W many spaces on this line! 7KH VDPH OLQH RI WKH ILOH ZRXOG EH RXWSXW DV IROORZV many spaces on this line! :ULWH D PHWKRG QDPHG readEntireFile WKDW DFFHSWV D Scanner UHSUHVHQWLQJ DQ LQSXW ILOH DV LWV SDUDPHWHU WKHQ UHDGV WKDW ILOH DQG UHWXUQV WKH HQWLUH WH[W FRQWHQWV RI WKDW ILOH DV D String :ULWH D PHWKRG QDPHG stripHtmlTags WKDW DFFHSWV D Scanner UHSUHVHQWLQJ DQ LQSXW ILOH DV LWV SDUDPHWHU WKHQ UHDGV WKDW ILOH DVVXPLQJ WKDW WKH ILOH FRQWDLQV DQ +70/ ZHE SDJH DQG SULQWV WKH ILOH V WH[W ZLWK DOO +70/ WDJV UHPRYHG $ WDJ LV DQ\ WH[W EHWZHHQ DQG ! FKDUDFWHUV )RU H[DPSOH LI WKH ILOH FRQWDLQV WKH IROORZLQJ WH[W <html> <head> <title>My web page</title> </head> <body> <p>There are many pictures of my cat here, as well as my <b>very cool</b> blog page, which contains <font color="red">awesome</font> stuff about my trip to Vegas.<p> Here's my cat now: <img src="cat.jpg"> </body> </html> <RXU SURJUDP VKRXOG RXWSXW WKH IROORZLQJ WH[W 378 My web page There are many pictures of my cat here, as well as my very cool blog page, which contains awesome stuff about my trip to Vegas. Here's my cat now: <RX PD\ DVVXPH WKDW WKH ILOH LV D ZHOO IRUPHG +70/ GRFXPHQW DQG WKDW QR WDJ FRQWDLQV D RU ! FKDUDFWHU LQVLGH LWVHOI :ULWH D PHWKRG QDPHG stripComments WKDW DFFHSWV D Scanner UHSUHVHQWLQJ DQ LQSXW ILOH DV LWV SDUDPHWHU WKHQ UHDGV WKDW ILOH DVVXPLQJ WKDW WKH ILOH FRQWDLQV D -DYD SURJUDP DQG SULQWV WKH ILOH V WH[W ZLWK DOO FRPPHQWV UHPRYHG $ WDJ LV DQ\ WH[W RQ D OLQH IURP WR WKH HQG RI WKH OLQH DQG DQ\ WH[W EHWZHHQ DQG FKDUDFWHUV )RU H[DPSOH LI WKH ILOH FRQWDLQV WKH IROORZLQJ WH[W import java.util.*; /* My program by Suzy Student */ public class Program { public static void main(String args) { System.out.println("Hello, world!"); } public static /* Hello there */ void foo() { System.out.println("Goodbye!"); } /* */ } // comment here // prints a message <RXU SURJUDP VKRXOG RXWSXW WKH IROORZLQJ WH[W import java.util.*; public class Program { public static void main(String args) { System.out.println("Hello, world!"); } public static void foo() { System.out.println("Goodbye!"); } } 379 3URJUDPPLQJ 3URMHFWV 6WXGHQWV DUH RIWHQ WROG WKDW WKHLU WHUP SDSHUV VKRXOG KDYH D FHUWDLQ QXPEHU RI ZRUGV LQ WKHP &RXQWLQJ ZRUGV LQ D ORQJ SDSHU LV D WHGLRXV WDVN EXW WKH FRPSXWHU FDQ KHOS :ULWH D SURJUDP WKDW FRXQWV WKH QXPEHU RI ZRUGV LQ D SDSHU DVVXPLQJ WKDW FRQVHFXWLYH ZRUGV DUH VHSDUDWHG HLWKHU E\ VSDFHV RU HQG RI OLQH FKDUDFWHUV <RX FRXOG WKHQ H[WHQG WKH SURJUDP WR FRXQW QRW MXVW WKH QXPEHU RI ZRUGV EXW WKH QXPEHU RI OLQHV DQG WKH WRWDO QXPEHU RI FKDUDFWHUV LQ WKH ILOH :ULWH D SURJUDP WKDW WDNHV DV LQSXW OLQHV RI WH[W OLNH This is some text here. DQG SURGXFHV DV RXWSXW WKH VDPH WH[W LQVLGH D ER[ DV LQ +------------+ |This is some| |text here. | +------------+ <RXU SURJUDP ZLOO KDYH WR DVVXPH VRPH PD[LPXP OLQH OHQJWK H J DERYH :ULWH D SURJUDP WKDW FRPSDUHV WZR ILOHV DQG SULQWV LQIRUPDWLRQ DERXW WKH GLIIHUHQFHV EHWZHHQ WKHP )RU H[DPSOH LI D ILOH data1.txt H[LVWV ZLWK WKH IROORZLQJ FRQWHQWV This file has a great deal of text in it which needs to be processed. DQG DQRWKHU ILOH data2.txt H[LVWV ZLWK WKH IROORZLQJ FRQWHQWV This file has a grate deal of text in it which needs to bee procesed. 7KHQ D GLDORJXH RI WKH XVHU UXQQLQJ \RXU SURJUDP PLJKW ORRN OLNH WKLV Enter a first file name: data1.txt Enter a second file name: data2.txt Differences found: Line 1: < This file has a great deal of > This file has a grate deal of Line 4: < be processed. > bee procesed. :ULWH D SURJUDP WKDW SURPSWV WKH XVHU IRU D ILOH QDPH DVVXPLQJ WKDW WKH ILOH FRQWDLQV D -DYD SURJUDP <RXU SURJUDP VKRXOG UHDG WKH ILOH DQG SULQW LWV FRQWHQWV SURSHUO\ LQGHQWHG :KHQ \RX VHH D ^ OHIW EUDFH FKDUDFWHU LQ WKH ILOH LQFUHDVH \RXU LQGHQWDWLRQ OHYHO E\ IRXU VSDFHV :KHQ 380 \RX VHH D ` ULJKW EUDFH FKDUDFWHU GHFUHDVH \RXU LQGHQWDWLRQ OHYHO E\ IRXU VSDFHV <RX PD\ DVVXPH WKDW WKH ILOH KDV RQO\ RQH RSHQLQJ RU FORVLQJ EUDFH SHU OLQH WKDW HYHU\ EORFN VWDWHPHQW VXFK DV if RU for XVHV EUDFHV UDWKHU WKDQ RPLWWLQJ WKHP DQG WKDW HYHU\ UHOHYDQW RFFXUUHQFH RI D ^ RU ` FKDUDFWHU LQ WKH ILOH RFFXUV DW WKH HQG RI D OLQH ,I WKH ILOH BadlyIndented.java FRQWDLQV WKH IROORZLQJ FRQWHQWV 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 // This file is poorly indented! public class BadlyIndented { public static void main(String args) { System.out.println("Hello, world!"); myMethod(); } public static void myMethod() { System.out.println("How ugly I am!"); if (1 + 1 == 3) { System.out.println("the sky is falling."); } else { System.out.println("Okay."); } } } <RXU SURJUDP VKRXOG SURGXFH WKH IROORZLQJ RXWSXW What is the name of the input file? BadlyIndented.java // This file is poorly indented! public class BadlyIndented { public static void main(String args) { System.out.println("Hello, world!"); myMethod(); } public static void myMethod() { System.out.println("How ugly I am!"); if (1 + 1 == 3) { System.out.println("the sky is falling."); } else { System.out.println("Okay."); } } } &RQVLGHU XVLQJ D FODVV FRQVWDQW IRU WKH QXPEHU RI VSDFHV WR LQGHQW HDVLO\ FKDQJHG ODWHU 6WXDUW 5HJHV 0DUW\ 6WHSS VR WKDW LW FRXOG EH 381 382 ...
View Full Document

This note was uploaded on 05/18/2008 for the course CSE 142 taught by Professor Martystepp during the Spring '07 term at University of Washington.

Ask a homework question - tutors are online