נבנה על ידיBuilt by Ariel Yohimovich

חזרה למפגשBack
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.

מה נעשה היוםToday's plan

  • מונחיםTerms
  • קריאת שורה ומספרReading a line and a number
  • המלכודת של nextIntThe nextInt trap
  • בודקים לפני שקוראיםCheck before reading
  • תפריט שחוזרA menu that repeats
  • פרויקט מדףShelf project
נושאTopic 1

מונחיםTerms

המונחים של המפגש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:
נושא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).
נושא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.
נושא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.
נושא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.
נושאTopic 6

פרויקט מדףShelf project

הצעד של היום בפרויקט מדףToday's step in the Shelf project

  • מדף, צעד 6: תפריטShelf, step 6: a menu

קלט: מה שהמשתמש מקליד לתוכנית בזמן שהיא רצה.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:

מייבאים את 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).

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.

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.

לולאה שקוראת פקודה, 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

מה צריך בראש הקובץ כדי להשתמש ב-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;

import java.util.Scanner;import java.util.Scanner;

הקלט הוא 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

מחרוזת ריקהAn empty string

מה יקרה עם 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

נזרקת InputMismatchExceptionAn InputMismatchException is thrown

ברכה אישיתA personal greeting

  • קראו שם עם nextLine והדפיסו Hello, <name>!.Read a name with nextLine and print Hello, <name>!.

מחשבון חיבורAn adding calculator

  • קראו שני מספרים שלמים והדפיסו 7 + 5 = 12 (בבדיקה מוקלדים 7 ו-5).Read two whole numbers and print 7 + 5 = 12 (the check types 7 and 5).

קבוצת גיל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).

צייד באגים: השם שנעלםBug hunter: the vanishing name

  • התוכנית קוראת גיל ואז שם, אבל מדפיסה is 30 בלי השם.The program reads an age then a name, but prints is 30 with no name.

נחשו את המספרGuess the number

  • המספר הסודי הוא 7.The secret is 7.

מדף, צעד 6: תפריטShelf, step 6: a menu

  • תפריט שקורא שורות:A menu reading lines:

ממוצע עד אפסAverage until zero

  • קראו מספרים עד שמגיע 0, והדפיסו את הממוצע שלהם (בלי ה-0):Read numbers until a 0 arrives, and print their average (without the 0):

מה למדנו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

עכשיו לתרגולNow, practice

6 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית6 exercises and 2 challenges, auto-checked
הערות מרצהSpeaker notes

נבנה על ידיBuilt by Ariel Yohimovich