Siena College Computer Science Outreach Project
Introduction
This project shares lectures and activities with students at two-year colleges. Our goal is to help prepare students for four year colleges and to learn more about students taking Computer Science courses at two-year colleges.
Lecture: String Manipulation & If Statements
The 8 minute video below a segment from a lecture in an Intro to Java Programming course at Siena College. Below are sample questions similar to the ones we might ask on a quiz or test. The video below answers all these questions as well as the ones you will be asked in your class. The code presented in the video is linked below the video.
Sample Questions:
- What import statement must be included to use the
Scanner
class?
Answer:import java.util.Scanner;
- Write the code to create a new
Scanner
object calledin
that can read input from the keyboard, i.e., System.in?
Answer:Scanner in = new Scanner(System.in);
- Write the code to use a
Scanner
object calledin
to read a line of text from the keyboard and assign it to aString
calledname
?
Answer:String name = in.nextLine();
- Write the code to search a
String
calledname
for a blank space (" ") and return the index or position of the space?
Answer:name.indexOf(" ");
- What value is returned if the space is not found in
name
?
Answer:-1
- Write an
if
statement that will print an error message ifindexOfSpace
equals -1?
Answer:if (indexOfSpace == -1) { System.out.println("The entered name does not have a space"); }
Video
Resources
import java.util.Scanner; public class NameSwapper { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter first and last name with a space in between: "); String name = in.nextLine(); System.out.println("Entered name: " + name); int indexOfSpace = name.indexOf(" "); System.out.println("Index of space: " + indexOfSpace); if (indexOfSpace == -1) { System.out.println("The entered name does not have a space"); } else { String first = name.substring(0, indexOfSpace); String last = name.substring(indexOfSpace+1, name.length()); System.out.println("First name: " + first); System.out.println("Last name: " + last); } } }
Quiz, Activity & Final Survey
Will be given in class.