ࡱ> dfc 6bjbjqq B?ee&8?\D4O  NNNNNNN@QSNA! A!A!NN$$$A!~N$A!N$$I Ne" L<NO04OGLbTe#bTxNbTN  $ Y NN# 4OA!A!A!A!bT  : Computer Programming II Instructor: Greg ShawPRIVATE  COP 3337 Intro to The Scanner Class I. Using Scanner for Interactive Input There are several different ways to do interactive input in Java. Most likely you are familiar with the showInputDialog method of the JOptionPane class The Scanner class may also be used for interactive input (as well as for file input and for extracting individual tokens from a string) Data entered by the user is stored in a temporary memory area known as the input buffer When an input statement is executed, the program is interrupted i.e. execution is suspended until data is read (extracted) from the input buffer If the input buffer is empty, then the program is interrupted until the user enters more data The Scanner class has methods that read (i.e., extract) data from the input buffer and store it in program variables. Data in the input buffer that is not read by a given method call remains in the buffer to be read by future method calls This allows the user to enter multiple data values separated by whitespace at the same time The Golden Rule of Interactive Input: Never confront the user with an unexplained input prompt always use a print() or println() statement first to tell the user exactly what input is required When using Netbeans, you must prompt with println() and not with print() or the user will not see the prompt until after the data are entered!!!!! Import the Class, Create an Object The Scanner class is in Javas utilities package, so use this import statement: import java.util.Scanner ; For interactive input, create a Scanner object and associate it with System.in, the input stream object that always flows from the standard system input device (the keyboard): Scanner object-variable-name = new Scanner(System.in) ; III. Scanner Class Methods next(), nextInt(), and nextDouble() Assume we have created a Scanner object pointed to by scan (see above). Below are some typical method calls and their effects The term token means a whitespace-delimited string. Whitespace characters are spaces, tabs, and newlines. Scanner methods next(), nextInt(), and nextDouble() skip over any leading whitespace characters, and then extract all consecutive non-whitespace characters, stopping when the next whitespace character is found. String s = scan.next() ; Removes the next token and returns it as a String int number = scan.nextInt() ; Removes the next token and returns it as an int double ecks = scan.nextDouble() ; Removes the next token and returns it as a double Regarding nextInt and nextDouble: If the next token is not a valid int or double literal, respectively, then an InputMismatchException is thrown See InputDemo.java for examples of how to call the methods of the Scanner class to do interactive input IV. The nextLine() Method The nextLine() method works differently than next(), nextInt(), and nextDouble() nextLine() extracts all characters including whitespace from the current cursor position on the current line up to and including the newline character (which ends the line). It then discards the newline character and returns everything else as a string Example: String line = scan.nextLine() ; V. Using Scanner to Extract the Individual Tokens From a String The Scanner class provides an easy way to "break up" a string into shorter, whitespace-delimited strings called tokens. (Whitespace means spaces, tabs, and newlines) Among other things, this allows us to enter multiple input values, separated by spaces, when reading user input using JOptionPane.showInputDialog() We create a Scanner object associated with the input string and call Scanner methods to extract each token. We can then process each token separately Example: See ScannerDemo.java on the COP 2210 web site   45=>?CHU^opqruǶobRG?hG^OJQJh ~h ~OJQJhc=h ~5CJOJQJaJh.r5CJOJQJaJhc=hc=5CJOJQJaJhc=OJQJh%yOJQJh%y56CJ OJQJhG^56CJ OJQJh.r56CJ OJQJ!jh%y@OJQJUh%y@OJQJjh%y@OJQJUh[*A@OJQJhG^@OJQJh%y@OJQJ@TUpq3 4   ||wAgdG^$ & F  `08*$^a$gd[*A$ `08*$a$gd[*A$ & F  `08*$^a$gdS2 `0h8*$gd ~$ `0h8*$a$$*$a$   ! , 2 3 4 5 8 ? F W [ l     K V Y ^ { Ž{o{{cŵ[h%yOJQJh[*Ah[*A6OJQJh ~h ~6OJQJh ~OJQJhG^CJOJQJaJhG^h[*ACJOJQJaJh.rh[*AOJQJh^h[*A6OJQJh[*AOJQJh.rOJQJh[*Ah[*ACJOJQJaJh ~h ~OJQJhG^hG^6OJQJhG^OJQJh[*AhG^6OJQJ! _ ` ( i $ & F  `0h8*$^a$gd.r$ `0h8*$^`a$gdS2$ & F `080*$`0a$gdOc$ `08h*$^ha$gdS2Agd[*A$ & F  `08*$^a$gdS2   / 3 ; D E @ ^ _ `    ' R [ i p ɹɮ{oo{gh#?OJQJh;h.r6OJQJh;OJQJh.rh.r6OJQJh.rOJQJh.rh.rOJQJh ~CJOJQJaJh ~h^OJQJhdWOJQJh[*AOJQJhOcOJQJh[*Ah[*A6OJQJh^h^6OJQJh^OJQJh^h^CJOJQJaJ(( 34RuXX$  `0h8h*$^ha$gdS2$ & F `0h*$^a$gdS2$ & F `0h*$`a$g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:hHhH5CJOJQJh05CJOJQJhH6CJOJQJaJh0hA9NCJOJQJaJh0hS2CJOJQJaJh0hS26CJOJQJaJh0hdW6CJOJQJaJh0hdWCJOJQJaJhCJOJQJaJhA9NCJOJQJaJhh6CJOJQJaJhh5CJOJQJaJhhCJOJQJaJhcTCJOJQJaJ67ST~~j$ & F h^a$gd;$ `0h0*$^`0a$gd;$ `0hh*$^ha$gd0$ & F `0h*$a$gd0$ `0h8h*$^ha$gdS2$ `0h8*$a$gdH :<?@DHIKQRSTXb07AaiʽʽyyyyiiiYh0h06CJOJQJaJh0hH6CJOJQJaJhHhH6CJOJQJaJhrq^hHCJOJQJaJhHhHCJOJQJaJhH5CJOJQJh05CJOJQJh056CJOJQJhHh056CJOJQJh0CJOJQJaJh0h05CJOJQJhHCJOJQJaJ"$6ABSVWʻʬueueueuYuJueueuJueh8Th;CJOJQJaJh;CJOJQJaJh8Th;6CJOJQJaJh8Th;CJOJQJaJh;h;5OJQJaJh-5CJOJQJaJh;5CJOJQJaJhhHCJOJQJaJh0h0CJOJQJaJh0hHCJOJQJaJh0CJOJQJaJhrq^h0CJOJQJaJhHCJOJQJaJVW&()+,-.013456d$ & F h*$^a$gdrq^ $^a$gdrq^$a$gd;$ & F h^a$gd;$ ^`a$gd; %&')*56ǷǪhEjhEUhrq^h;OJQJaJhrq^hrq^6CJOJQJaJhrq^hrq^CJOJQJaJhrq^hrq^CJOJQJaJh;CJOJQJaJh8Th;CJOJQJaJ @....()()))()00P8$:p0BP/ =!"#$% DpTD phoenix6G^B 666666666vvvvvvvvv66666686666666666666666666666666666666666666666666666666hH6666666666666666666666666666666666666666666666666666666666666666662 0@P`p2( 0@P`p 0@P`p 0@P`p 0@P`p 0@P`p 0@P`p8XV~_HmH nH sH tH H`H Normal1$CJOJQJ_HmH sH tH DA`D Default Paragraph FontViV  Table Normal :V 44 la (k (No List 4+4  Endnote Text>*> Endnote ReferenceH*66  Footnote Text@&!@ Footnote ReferenceH*,1, Document 86A6 Document 4 56CJ,Q, Document 6,a, Document 5HqH Document 2CJOJQJkHmH sH u,, Document 7.. Bibliogrphy.. Right Par 1.. Right Par 2HH Document 3CJOJQJkHmH sH u.. Right Par 3.. Right Par 4.. Right Par 5.. Right Par 6.. Right Par 7.!. Right Par 8F1F Tech InitCJOJQJkHmH sH u`/B` Document 1$$$ 0*$1$CJOJQJ_HmH sH tH .Q. Technical 5.a. Technical 6JqJ Technical 2CJOJQJkHmH sH uJJ Technical 3CJOJQJkHmH sH u.. Technical 4JJ Technical 1CJOJQJkHmH sH u.. Technical 7.. Technical 8(( Doc Init.. Bullet ListNN TOC 1)/ $ 0*$]^`0JJ TOC 2%0 $ 0*$]^`0JJ TOC 3%1 $ p0*$]^p`0JJ TOC 4%2 $ @ 0*$]^@ `0JJ TOC 5%3 $ 0*$]^`0BB TOC 64 $0*$^`0:: TOC 750*$^`0BB TOC 86 $0*$^`0BB TOC 97 $ 0*$^`0N N Index 1%8 $ `*$]^``N N Index 2%9 $ 0*$]^`0>.>  TOA Heading :*$ $*"* Caption;:/: _Equation Caption^B^ Body Text%=$ `0h8*$a$ @OJQJ0U0 Hyperlink>*B*@V@ FollowedHyperlink>*B* DZD w Plain Text@1$CJOJQJ^J@@@ G^ List Paragraph A^PK![Content_Types].xmlj0Eжr(΢Iw},-j4 wP-t#bΙ{UTU^hd}㨫)*1P' ^W0)T9<l#$yi};~@(Hu* Dנz/0ǰ $ X3aZ,D0j~3߶b~i>3\`?/[G\!-Rk.sԻ..a濭?PK!֧6 _rels/.relsj0 }Q%v/C/}(h"O = C?hv=Ʌ%[xp{۵_Pѣ<1H0ORBdJE4b$q_6LR7`0̞O,En7Lib/SeеPK!kytheme/theme/themeManager.xml M @}w7c(EbˮCAǠҟ7՛K Y, e.|,H,lxɴIsQ}#Ր ֵ+!,^$j=GW)E+& 8PK!Ptheme/theme/theme1.xmlYOo6w toc'vuر-MniP@I}úama[إ4:lЯGRX^6؊>$ !)O^rC$y@/yH*񄴽)޵߻UDb`}"qۋJחX^)I`nEp)liV[]1M<OP6r=zgbIguSebORD۫qu gZo~ٺlAplxpT0+[}`jzAV2Fi@qv֬5\|ʜ̭NleXdsjcs7f W+Ն7`g ȘJj|h(KD- dXiJ؇(x$( :;˹! I_TS 1?E??ZBΪmU/?~xY'y5g&΋/ɋ>GMGeD3Vq%'#q$8K)fw9:ĵ x}rxwr:\TZaG*y8IjbRc|XŻǿI u3KGnD1NIBs RuK>V.EL+M2#'fi ~V vl{u8zH *:(W☕ ~JTe\O*tHGHY}KNP*ݾ˦TѼ9/#A7qZ$*c?qUnwN%Oi4 =3ڗP 1Pm \\9Mؓ2aD];Yt\[x]}Wr|]g- eW )6-rCSj id DЇAΜIqbJ#x꺃 6k#ASh&ʌt(Q%p%m&]caSl=X\P1Mh9MVdDAaVB[݈fJíP|8 քAV^f Hn- "d>znNJ ة>b&2vKyϼD:,AGm\nziÙ.uχYC6OMf3or$5NHT[XF64T,ќM0E)`#5XY`פ;%1U٥m;R>QD DcpU'&LE/pm%]8firS4d 7y\`JnίI R3U~7+׸#m qBiDi*L69mY&iHE=(K&N!V.KeLDĕ{D vEꦚdeNƟe(MN9ߜR6&3(a/DUz<{ˊYȳV)9Z[4^n5!J?Q3eBoCM m<.vpIYfZY_p[=al-Y}Nc͙ŋ4vfavl'SA8|*u{-ߟ0%M07%<ҍPK! ѐ'theme/theme/_rels/themeManager.xml.relsM 0wooӺ&݈Э5 6?$Q ,.aic21h:qm@RN;d`o7gK(M&$R(.1r'JЊT8V"AȻHu}|$b{P8g/]QAsم(#L[PK-![Content_Types].xmlPK-!֧6 +_rels/.relsPK-!kytheme/theme/themeManager.xmlPK-!Ptheme/theme/theme1.xmlPK-! ѐ' theme/theme/_rels/themeManager.xml.relsPK] 6>  Bq:6  ( R66 8@0(  B S  ?TO@7C7?*urn:schemas-microsoft-com:office:smarttags stockticker #!,3;RY1:fmuO X  G N S ] @ H X ` 6Q&()+,-.0137^aFJRZ7=)1^c9 ? &()+,-.013733333333 6 6 %&&()),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JQJo(hHh88^8`OJQJ^Jo(hHoh^`OJQJo(hHh  ^ `OJQJo(hHh  ^ `OJQJ^Jo(hHohxx^x`OJQJo(hHhHH^H`OJQJo(hHh^`OJQJ^Jo(hHoh^`OJQJo(hHh^`CJ0OJQJ^Jo(hHFh^`OJQJ^Jo(hHohp^p`OJQJo(hHh@ ^@ `OJQJo(hHh^`OJQJ^Jo(hHoh^`OJQJo(hHh^`OJQJo(hHh^`OJQJ^Jo(hHohP^P`OJQJo(hHp!  ^ `CJ0OJQJ^JaJo(hHFp^`CJ0OJQJaJ0o(hHFppp^p`OJQJo(hHp@ @ ^@ `OJQJo(hHp^`OJQJ^Jo(hHop^`OJQJo(hHp^`OJQJo(hHp^`OJQJ^Jo(hHopPP^P`OJQJo(hHd44^4`CJ0OJQJaJ0o(hHFd^`OJQJ^Jo(hHod  ^ `OJQJo(hHd  ^ `OJQJo(hHdxx^x`OJQJ^Jo(hHodHH^H`OJQJo(hHd^`OJQJo(hHd^`OJQJ^Jo(hHod^`OJQJo(hHhh^h`CJ OJQJo(ld44^4`CJ0OJQJaJ0o(hHFd^`OJQJ^Jo(hHod  ^ `OJQJo(hHd  ^ `OJQJo(hHdxx^x`OJQJ^Jo(hHodHH^H`OJQJo(hHd^`OJQJo(hHd^`OJQJ^Jo(hHod^`OJQJo(hHhh^h`CJ OJQJo(l 0^`0OJQJo(nhh^h`CJ OJQJo(lhh^h`CJ OJQJo(l 0^`0OJQJo(n0h^h`5679;<B*CJ8H*OJQJS*TXo(+.By'hg87UN :IE51/~Ul.xd>(KRw^/ dW03V %&(@6@UnknownG* Times New Roman5Symbol3. * Arial?= * Courier New71 Courier;Wingdings_Monotype SortsZapfDingbatsA BCambria Math"9A)$!$!i&B B $)Px24 3Q)PHX?%y2!xxIntroduction to Programming M.C. ShawFIU-SCSx                      Oh+'0 , L X d p|Introduction to Programming M.C. Shaw Normal.dotmFIU-SCS2Microsoft Office Word@@.1|Rh@`@`B ՜.+,0$ px  Hotel Degregorio8 Introduction to Programming Title !"#$%&')*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRTUVWXYZ\]^_`abeRoot Entry FlgData  1Table(TWordDocumentB?SummaryInformation(SDocumentSummaryInformation8[CompObjy  F'Microsoft Office Word 97-2003 Document MSWordDocWord.Document.89q