zyBook 2.15 Strings

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What does the following statement get into the indicated variable, for the given input? firstString = scnr.nextLine(); secondString = scnr.nextLine(); Walk awayfirstString gets _____ . (Note the leading spaces before Walk). Walk away Walk away

Walk away

What is string literal?

a character sequence surrounded with double quotes, as in "Hello"

What is a whitespace character?

a character used to represent horizontal and vertical spaces in text, and includes spaces, tabs, and newline characters. Ex: "Oh my goodness!" has two whitespace characters, one between h and m, the other between y and g.

What is a string?

a sequence of characters

For the given input, indicate what string will be put into userString by: userString = scnr.next(); abc

abc

For the given input, indicate what string will be put into secondString by: firstString = scnr.next(); secondString = scnr.next(); We all know

all

What is the difference between next() and nextLine()?

next() skips intial whitespace, then gets characters until the next whitespace is seen ( leaving that whitespace in the input.) scnr.nextLine() gets all remaining text on the current input line, up to the newline character(which is removed from input but not put in stringVar).

Given the following input: Every one is great. That's right. What does the following get into str2?str1 = scnr.next(); str2 = scnr.nextLine(); Every one one one(has a leading space)

one(has a leading space), the first statement left a space in the input. nextLine() starts with what's left in the input, so starts with that space, continuing to the newline.

Given the following input: Every one is great. That's right. What does the following get into str2?str1 = scnr.next(); str2 = scnr.next(); Every one Every one

one, The first statement gets "Every" stopping at the space after y. The second statement skips that leading space, gets "one", and stops at the newline.

Finish the statement Below shows the basic approach to get a string from input into variable userString. The approach automatically skips initial whitespace, then gets characters until the next whitespace is seen (leaving that whitespace in the input).

userString = scnr.next();

What is the difference between string and char variable types?

String is for a character sequence, char is for a specific character

For the given input, indicate what string will be put into secondString by: firstString = scnr.next(); secondString = scnr.next(); oh my!

my!

Write a statement that assigns userName with "Sarah".

userName = "Sarah";

Given the following input: Every one is great. That's right. What does the following get into str2?str0 = scnr.next(); str1 = scnr.next(); str2 = scnr.nextLine(); one (blank) is great.

(blank), The second statement puts "one" into str1, leaving the newline in the input. nextLine() doesn't skip leading whitespace, so gets the line up to that newline. Thus str2 will be blank. This is the tricky part of combining next() with nextLine().

For the given input, indicate what string will be put into secondString by: firstString = scnr.next(); secondString = scnr.next(); Oh... ...no!

...no!, it skips the initial whitespace characters

How do you use a scanner to read a sentence?

By using the method scnr.nextLine(); this method gets all the remaining text on the current input line

Finish the statement A String variable is a reference type (discussed in depth elsewhere) variable that refers to a String object. An object consists of some internal data items plus operations that can be performed on that data.

Ex: String movieTitle = "Frozen"; declares a String reference variable named movieTitle that refers to a String object. That String object stores the string "Frozen".

Finish the statement A programmer can assign a String variable with a string literal, which creates a new String object.

Ex: movieTitle = "The Martian"; creates a new String object with the string "The Martian", and assigns the String object's reference to the variable movieTitle.

Finish the statement Assigning one String variable to another String variable causes both variables to refer to the same String, and does not create a new String.

Ex: movieTitle = favoriteMovie; assigns favoriteMovie's reference to movieTitle. Both movieTitle and favoriteMovie refer to the same String object.

What does the following statement get into the indicated variable, for the given input? firstString = scnr.nextLine(); secondString = scnr.nextLine(); Hello there! Welcome. firstString gets _____ .

Hello there!

For the given input, indicate what string will be put into userString by: userString = scnr.next(); Hello! I'm tired.

Hello!

For the given input, indicate what string will be put into userString by: userString = scnr.next(); Hi there.

Hi

Suppose string str1 is initially "Hello" and str2 is "Hi".After str1 = str2; and then str2 = "Bye";, what is str1?Omit the quotes.

Hi

Suppose string str1 is initially "Hello" and str2 is "Hi".After str1 = str2;, what is str1?Omit the quotes.

Hi

Type one statement that declares a string named smallestPlanet, initialized with "Mercury".

String smallestPlanet = "Mercury";

Declare a string variable userName.

String userName;


Kaugnay na mga set ng pag-aaral

HESI RN Case Study Diabetes Type 1

View Set

Unit 4: National Brokerage: Sale & Lease Contracts:Quiz

View Set

SmartBook Questions Ch. 1 Lecture

View Set

Real Estate Principles Study Guide

View Set

Nursing health assessment and promotion: Chapter 11: Skin, Hair, and Nails

View Set

Chapter 14. Nervous System: Spinal Cord and Spinal Nerves

View Set

Словник психологічних термінів (А)

View Set