נבנה על ידיBuilt by Ariel Yohimovich
JavaJava
Scanner וקלטScanner & input
מפגש 6: קריאת קלט עם Scanner, nextLine מול nextInt והמלכודת שביניהם, בדיקת קלט ותפריט טקסט ראשון.Session 6: Reading input with Scanner, nextLine vs nextInt and the trap between them, validating input and a first text menu.
קריאת קלט עם Scanner, nextLine מול nextInt והמלכודת שביניהם, בדיקת קלט ותפריט טקסט ראשון. בסוף המפגש מוסיפים צעד לפרויקט המלווה.Reading input with Scanner, nextLine vs nextInt and the trap between them, validating input and a first text menu. At the end we add a step to the companion project.מה נעשה היוםToday's plan
- מונחיםTerms
- קריאת שורה ומספרReading a line and a number
- המלכודת של nextIntThe nextInt trap
- בודקים לפני שקוראיםCheck before reading
- תפריט שחוזרA menu that repeats
- פרויקט מדףShelf project
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.המונחים של המפגשThis session's terms
- קלט: מה שהמשתמש מקליד לתוכנית בזמן שהיא רצה.input: What the user types into the program while it runs.
- Scanner: מחלקה שקוראת קלט:Scanner: A class that reads input:
- מילה (token): חתיכת קלט בין רווחים או שורות.token: A piece of input between spaces or lines.
- מלכודת nextInt:
nextInt קורא את המספר אבל משאיר את ה-Enter.the nextInt trap: nextInt reads the number but leaves the Enter behind. - בדיקת קלט: בודקים שהקלט הגיוני לפני שמשתמשים בו:input validation: Checking input makes sense before using it:
- import: שורה בראש הקובץ שמביאה מחלקה מספרייה:import: A line at the top of the file that brings in a library class:
לפני הקוד, המילים. כל מונח כאן מקושר גם במילון של הקורס.Before the code, the words. Every term here is also linked in the course glossary.נושאTopic 2
קריאת שורה ומספרReading a line and a number
קריאת שורה ומספרReading a line and a number
- מייבאים את Scanner, יוצרים אחד על
System.in, וקוראים.Import Scanner, create one on System.in, and read. - התוכנית מחכה עד שמקלידים ולוחצים Enter.The program waits until you type and press Enter.
- כאן הקלט הוקלד מראש (מודגש בפלט).Here the input was typed in advance (shown in the output).
מייבאים את Scanner, יוצרים אחד על System.in, וקוראים. התוכנית מחכה עד שמקלידים ולוחצים Enter. כאן הקלט הוקלד מראש (מודגש בפלט).Import Scanner, create one on System.in, and read. The program waits until you type and press Enter. Here the input was typed in advance (shown in the output).נושאTopic 3
המלכודת של nextIntThe nextInt trap
המלכודת של nextIntThe nextInt trap
nextInt קורא רק את המספר ומשאיר את ה-Enter בצד.nextInt reads only the number and leaves the Enter behind.- ה-
nextLine שאחריו מקבל שורה ריקה!The nextLine after it gets an empty line! - מוסיפים
sc.nextLine(); אחרי nextInt כדי "לבלוע" את ה-Enter.Add sc.nextLine(); after nextInt to "swallow" the Enter.
nextInt קורא רק את המספר ומשאיר את ה-Enter בצד. ה-nextLine שאחריו מקבל שורה ריקה! מוסיפים sc.nextLine(); אחרי nextInt כדי "לבלוע" את ה-Enter.nextInt reads only the number and leaves the Enter behind. The nextLine after it gets an empty line! Add sc.nextLine(); after nextInt to "swallow" the Enter.נושאTopic 4
בודקים לפני שקוראיםCheck before reading
בודקים לפני שקוראיםCheck before reading
hasNextInt() שואל אם המילה הבאה היא מספר, בלי לקרוא אותה.hasNextInt() asks whether the next token is a number without reading it.- אם לא, קוראים אותה עם
next() וזורקים.If not, read it with next() and drop it. - ככה התוכנית לא קורסת על
abc.That way the program doesn't crash on abc.
hasNextInt() שואל אם המילה הבאה היא מספר, בלי לקרוא אותה. אם לא, קוראים אותה עם next() וזורקים. ככה התוכנית לא קורסת על abc.hasNextInt() asks whether the next token is a number without reading it. If not, read it with next() and drop it. That way the program doesn't crash on abc.נושאTopic 5
תפריט שחוזרA menu that repeats
תפריט שחוזרA menu that repeats
- לולאה שקוראת פקודה,
switch שמחליט מה לעשות, ומשתנה שאומר מתי לצאת.A loop that reads a command, a switch that decides what to do, and a variable that says when to quit.
לולאה שקוראת פקודה, switch שמחליט מה לעשות, ומשתנה שאומר מתי לצאת.A loop that reads a command, a switch that decides what to do, and a variable that says when to quit.נושאTopic 6
פרויקט מדףShelf project
הצעד של היום בפרויקט מדףToday's step in the Shelf project
- מדף, צעד 6: תפריטShelf, step 6: a menu
תפריט שקורא שורות: add <title> מוסיף ספר, list מדפיס Books (2): Dune, Emma, quit יוצא ומדפיס Bye. שמרו את הרשימה ב-StringBuilder ומונה. (בבדיקה: add Dune, add Emma, list, quit.)A menu reading lines: add <title> adds a book, list prints Books (2): Dune, Emma, quit leaves and prints Bye. Keep the list in a StringBuilder and a counter. (The check: add Dune, add Emma, list, quit.)קלט: מה שהמשתמש מקליד לתוכנית בזמן שהיא רצה.input: What the user types into the program while it runs.
המונחים של המפגשThis session's termsScanner: מחלקה שקוראת קלט:Scanner: A class that reads input:
המונחים של המפגשThis session's termsמילה (token): חתיכת קלט בין רווחים או שורות.token: A piece of input between spaces or lines.
המונחים של המפגשThis session's termsמלכודת nextInt: nextInt קורא את המספר אבל משאיר את ה-Enter.the nextInt trap: nextInt reads the number but leaves the Enter behind.
המונחים של המפגשThis session's termsבדיקת קלט: בודקים שהקלט הגיוני לפני שמשתמשים בו:input validation: Checking input makes sense before using it:
המונחים של המפגשThis session's termsimport: שורה בראש הקובץ שמביאה מחלקה מספרייה:import: A line at the top of the file that brings in a library class:
המונחים של המפגשThis session's termsמייבאים את Scanner, יוצרים אחד על System.in, וקוראים.Import Scanner, create one on System.in, and read.
קריאת שורה ומספרReading a line and a numberהתוכנית מחכה עד שמקלידים ולוחצים Enter.The program waits until you type and press Enter.
קריאת שורה ומספרReading a line and a numberכאן הקלט הוקלד מראש (מודגש בפלט).Here the input was typed in advance (shown in the output).
קריאת שורה ומספרReading a line and a numbernextInt קורא רק את המספר ומשאיר את ה-Enter בצד.nextInt reads only the number and leaves the Enter behind.
המלכודת של nextIntThe nextInt trapה-nextLine שאחריו מקבל שורה ריקה!The nextLine after it gets an empty line!
המלכודת של nextIntThe nextInt trapמוסיפים sc.nextLine(); אחרי nextInt כדי "לבלוע" את ה-Enter.Add sc.nextLine(); after nextInt to "swallow" the Enter.
המלכודת של nextIntThe nextInt traphasNextInt() שואל אם המילה הבאה היא מספר, בלי לקרוא אותה.hasNextInt() asks whether the next token is a number without reading it.
בודקים לפני שקוראיםCheck before readingאם לא, קוראים אותה עם next() וזורקים.If not, read it with next() and drop it.
בודקים לפני שקוראיםCheck before readingככה התוכנית לא קורסת על abc.That way the program doesn't crash on abc.
בודקים לפני שקוראיםCheck before readingלולאה שקוראת פקודה, switch שמחליט מה לעשות, ומשתנה שאומר מתי לצאת.A loop that reads a command, a switch that decides what to do, and a variable that says when to quit.
תפריט שחוזרA menu that repeatsמדף, צעד 6: תפריטShelf, step 6: a menu
הצעד של היום בפרויקט מדףToday's step in the Shelf projectמה צריך בראש הקובץ כדי להשתמש ב-Scanner?What does the top of the file need to use Scanner?
import java.util.Scanner;import java.util.Scanner;include Scannerinclude Scanner- כלוםNothing
import System.in;import System.in;
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.import java.util.Scanner;import java.util.Scanner;
נכון: Scanner גר בחבילה java.util.Right: Scanner lives in the java.util package.הקלט הוא 30 ואז Haifa. הקוד: int a = sc.nextInt(); String c = sc.nextLine();. מה ב-c?The input is 30 then Haifa. The code: int a = sc.nextInt(); String c = sc.nextLine();. What's in c?
HaifaHaifa- מחרוזת ריקהAn empty string
3030- שגיאהAn error
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.מחרוזת ריקהAn empty string
נכון: nextLine קרא את מה שנשאר אחרי 30 עד ה-Enter: כלום.Right: nextLine read what was left after 30 up to the Enter: nothing.מה יקרה עם sc.nextInt() כשהמשתמש מקליד abc?What happens with sc.nextInt() when the user types abc?
- מוחזר 0It returns 0
- נזרקת
InputMismatchExceptionAn InputMismatchException is thrown - התוכנית מחכה לנצחThe program waits forever
- מוחזר -1It returns -1
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.נזרקת InputMismatchExceptionAn InputMismatchException is thrown
נכון. לכן בודקים קודם עם hasNextInt().Right. That's why you check first with hasNextInt().ברכה אישיתA personal greeting
- קראו שם עם
nextLine והדפיסו Hello, <name>!.Read a name with nextLine and print Hello, <name>!.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מחשבון חיבורAn adding calculator
- קראו שני מספרים שלמים והדפיסו
7 + 5 = 12 (בבדיקה מוקלדים 7 ו-5).Read two whole numbers and print 7 + 5 = 12 (the check types 7 and 5).
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.קבוצת גילAge group
- קראו גיל והדפיסו
child (מתחת ל-13), teen (13–17) או adult (18 ומעלה).Read an age and print child (under 13), teen (13–17) or adult (18 and over).
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.צייד באגים: השם שנעלםBug hunter: the vanishing name
- התוכנית קוראת גיל ואז שם, אבל מדפיסה
is 30 בלי השם.The program reads an age then a name, but prints is 30 with no name.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.נחשו את המספרGuess the number
- המספר הסודי הוא 7.The secret is 7.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מדף, צעד 6: תפריטShelf, step 6: a menu
- תפריט שקורא שורות:A menu reading lines:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.ממוצע עד אפסAverage until zero
- קראו מספרים עד שמגיע 0, והדפיסו את הממוצע שלהם (בלי ה-0):Read numbers until a 0 arrives, and print their average (without the 0):
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מה למדנוWhat we learned
- קלט: מה שהמשתמש מקליד לתוכנית בזמן שהיא רצה.input: What the user types into the program while it runs.
- מייבאים את Scanner, יוצרים אחד על
System.in, וקוראים.Import Scanner, create one on System.in, and read. nextInt קורא רק את המספר ומשאיר את ה-Enter בצד.nextInt reads only the number and leaves the Enter behind.hasNextInt() שואל אם המילה הבאה היא מספר, בלי לקרוא אותה.hasNextInt() asks whether the next token is a number without reading it.- לולאה שקוראת פקודה,
switch שמחליט מה לעשות, ומשתנה שאומר מתי לצאת.A loop that reads a command, a switch that decides what to do, and a variable that says when to quit. - מדף, צעד 6: תפריטShelf, step 6: a menu
כל אחד אומר דבר אחד שלמד היום.Everyone says one thing they learned today.עכשיו לתרגולNow, practice
6 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית6 exercises and 2 challenges, auto-checked
מפנים לעמוד התרגול.Point to the practice page.