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

JavaJava
6
חלקPart 1: יסודותFoundations כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

Scanner וקלטScanner & input

קריאת קלט עם Scanner, nextLine מול nextInt והמלכודת שביניהם, בדיקת קלט ותפריט טקסט ראשון.

Reading input with Scanner, nextLine vs nextInt and the trap between them, validating input and a first text menu.

או Ctrl+Por Ctrl+P

הנקודות החשובותKey points

  • קלט: מה שהמשתמש מקליד לתוכנית בזמן שהיא רצה.input: What the user types into the program while it runs.
  • Scanner: מחלקה שקוראת קלט:Scanner: A class that reads input:
  • מייבאים את Scanner, יוצרים אחד על System.in, וקוראים.Import Scanner, create one on System.in, and read.
  • התוכנית מחכה עד שמקלידים ולוחצים Enter.The program waits until you type and press Enter.
  • nextInt קורא רק את המספר ומשאיר את ה-Enter בצד.nextInt reads only the number and leaves the Enter behind.
  • ה-nextLine שאחריו מקבל שורה ריקה!The nextLine after it gets an empty line!
  • hasNextInt() שואל אם המילה הבאה היא מספר, בלי לקרוא אותה.hasNextInt() asks whether the next token is a number without reading it.
  • אם לא, קוראים אותה עם next() וזורקים.If not, read it with next() and drop it.
  • לולאה שקוראת פקודה, switch שמחליט מה לעשות, ומשתנה שאומר מתי לצאת.A loop that reads a command, a switch that decides what to do, and a variable that says when to quit.

מונחיםTerms

  • קלטinput: מה שהמשתמש מקליד לתוכנית בזמן שהיא רצה. בבדיקות כאן, הקלט מוקלד מראש.What the user types into the program while it runs. In the checks here, the input is typed in advance.
  • ScannerScanner: מחלקה שקוראת קלט: Scanner sc = new Scanner(System.in);, ואז sc.nextLine(), sc.nextInt() וכו'.A class that reads input: Scanner sc = new Scanner(System.in);, then sc.nextLine(), sc.nextInt() and so on.
  • מילה (token)token: חתיכת קלט בין רווחים או שורות. nextInt ו-next קוראים מילה אחת, ו-nextLine קורא שורה שלמה.A piece of input between spaces or lines. nextInt and next read one token; nextLine reads a whole line.
  • מלכודת nextIntthe nextInt trap: nextInt קורא את המספר אבל משאיר את ה-Enter. ה-nextLine הבא קורא את השארית הריקה. הפתרון: nextLine() נוסף אחרי nextInt.nextInt reads the number but leaves the Enter behind. The next nextLine reads that empty leftover. The fix: an extra nextLine() after nextInt.
  • בדיקת קלטinput validation: בודקים שהקלט הגיוני לפני שמשתמשים בו: hasNextInt() לפני nextInt(), ובדיקת טווח.Checking input makes sense before using it: hasNextInt() before nextInt(), and a range check.
  • importimport: שורה בראש הקובץ שמביאה מחלקה מספרייה: import java.util.Scanner;.A line at the top of the file that brings in a library class: import java.util.Scanner;.
  • קלטinput: מה שהמשתמש מקליד לתוכנית בזמן שהיא רצה. בבדיקות כאן, הקלט מוקלד מראש.What the user types into the program while it runs. In the checks here, the input is typed in advance.
  • ScannerScanner: מחלקה שקוראת קלט: Scanner sc = new Scanner(System.in);, ואז sc.nextLine(), sc.nextInt() וכו'.A class that reads input: Scanner sc = new Scanner(System.in);, then sc.nextLine(), sc.nextInt() and so on.
  • מילה (token)token: חתיכת קלט בין רווחים או שורות. nextInt ו-next קוראים מילה אחת, ו-nextLine קורא שורה שלמה.A piece of input between spaces or lines. nextInt and next read one token; nextLine reads a whole line.
  • מלכודת nextIntthe nextInt trap: nextInt קורא את המספר אבל משאיר את ה-Enter. ה-nextLine הבא קורא את השארית הריקה. הפתרון: nextLine() נוסף אחרי nextInt.nextInt reads the number but leaves the Enter behind. The next nextLine reads that empty leftover. The fix: an extra nextLine() after nextInt.
  • בדיקת קלטinput validation: בודקים שהקלט הגיוני לפני שמשתמשים בו: hasNextInt() לפני nextInt(), ובדיקת טווח.Checking input makes sense before using it: hasNextInt() before nextInt(), and a range check.
  • importimport: שורה בראש הקובץ שמביאה מחלקה מספרייה: import java.util.Scanner;.A line at the top of the file that brings in a library class: import java.util.Scanner;.

Cheatsheet

קריאת שורה ומספרReading a line and a number
Scanner sc = new Scanner(System.in);
System.out.print("Name: ");
String name = sc.nextLine();
System.out.print("Age: ");
int age = sc.nextInt();
System.out.println(name + " will be " + (age + 1) + " next year");
המלכודת של nextIntThe nextInt trap
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
sc.nextLine();
String city = sc.nextLine();
System.out.println(age + " from " + city);
בודקים לפני שקוראיםCheck before reading
Scanner sc = new Scanner(System.in);
while (!sc.hasNextInt()) {
    String bad = sc.next();
    System.out.println(bad + " is not a number");
}
int n = sc.nextInt();
System.out.println("Got " + n);
תפריט שחוזרA menu that repeats
Scanner sc = new Scanner(System.in);
boolean running = true;
while (running) {
    String cmd = sc.nextLine();
    switch (cmd) {
        case "hi" -> System.out.println("Hello!");
        case "quit" -> running = false;
        default -> System.out.println("Unknown: " + cmd);
    }
}

שגיאות שפגשנוErrors we met

שגיאהErrorמה זה אומרWhat it meansמה עושיםWhat to do

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