3: When Java's Scanner Class is More Reliable than the School's Weather Evaluation

AP Computer Science A Aug 18, 2023

Hello again, KISJ legends! 🚀 Remember when the school got flooded and we had to use sandbags and towels? Well, in Java, we have something called the Scanner class that's way more reliable than our school's "can we walk outside" weather evaluation. Let's dive in!

Java's Scanner Class: The Digital Deli

Just like how the deli is our only hope for decent snacks (despite the overpriced monopoly), the Scanner class is Java's way of getting some tasty user input.

Importing the Scanner Class

Before you can use the Scanner, you've got to invite it in. Think of it as giving the deli VIP access to your program:

import java.util.Scanner;

Crafting a Scanner

Creating a Scanner object is like setting up a booth in the school hallway where students can spill the beans about those advisory "activities" we all love (or not):

Scanner input = new Scanner(System.in)

Engaging with Users

The Scanner has a bunch of methods that let you ask users all sorts of questions.

Method Description
nextBoolean() Reads a boolean value from the user
nextByte() Reads a byte value from the user
nextDouble() Reads a double value from the user
nextFloat() Reads a float value from the user
nextInt() Reads an integer value from the user
nextLine() Reads a String value from the user
nextShort() Reads a short value from the user

For instance:

System.out.println("How many advisory videos have you actually watched?");
int advisoryCount = input.nextInt();
System.out.println("Really? You've watched " + advisoryCount + " videos?");

And just like we wait for the school to end, you should close the scanner when you're done:

input.close();

Strings: More Than Just Yarn

Strings in Java are like the rumors in school: they can be long, short, combined, and even sliced up.

Seeking User's Name:

Here's how you can get the latest scoop, or just someone's name:

System.out.println("What is your name?")
String name = input.nextLine();
System.out.println("Hello, " + name + "!");

Manipulating Strings:

  • Length:
  • Concatenation:
  • Substring:
  • Character Fetch: Find out the first letter of someone's name, or maybe the first letter of a secret code?

Length: Find out how long a rumor... I mean, a string is.

int length = name.length();

Concatenation: Combine two pieces of gossip into one mega rumor.

String megaRumor = "Lasy sunday, " + gossip1 + "and" + gossip2;
System.out.println(megaRumor);

Substring: Extract the juiciest part of a story. Like we live to do it with rumor, AP exams love this! Mastering it is crucial.

String phrase = "Java rocks!";
String extract = phrase.substring(5, 10); 
System.out.println(extract);
This will get "rocks"

Character Fetch: Find out the first letter of someone's name, or maybe the first letter of a secret code?

char firstLetter = name.charAt(0);

That's a wrap for today's lesson on Java's Scanner and Strings! If you've missed our previous lessons, don't worry. They're more accessible than the deli during lunchtime rush. Dive in, and remember: Java might be easier to understand than our school's weather policy. 😉 Happy coding! 🎉👩‍💻👨‍💻

Tags