Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java User Input and String Methods - CSIS 110 Lecture 16 - Prof. Brian F. Hanks, Study notes of Javascript programming

User input using java's scanner class and discusses various string methods. Students will learn how to read user input as int, double, boolean, and strings, as well as extract characters and substrings from strings using valid indices. The document also includes examples and lab exercises.

Typology: Study notes

Pre 2010

Uploaded on 08/05/2009

koofers-user-tf7
koofers-user-tf7 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSIS 110 - Lecture 16
User Input
Java provides a Library class that we can use to get user input from a text window. This
class is called Scanner.
Before we can use the Scanner class, we need to import it (java.util.Scanner).
We can construct a new Scanner object and associate it with the keyboard:
Scanner stdin = new Scanner( System.in );
(stdin – standard input device)
The Scanner class contains the following methods:
String nextLine() – read from the Scanner up to the return key
Int nextInt() – read an int from the Scanner. An error occurs if the next item is not an int.
double nextDouble()
boolean nextBoolean()
boolean hasNext() – returns true if there is data available in the Scanner.
Let's try to write some code that gets an int from the user.
The String class
We've seen lots of Strings. The String class includes lots of methods. What methods have
we already seen in this class?
length()
equals()
Here are some more:
charAt( int index ) – return the character at the given index in the String. What
is the valid range of indices? Examples: String s = "Hello world!". How do I
extract the first character? The fourth character? The last character?
indexOf( char c ) – return the index of the first occurrence of the character in the
String. What is s.indexOf( 'w' )? How about s.indexOf( 'l' ); How
about s.indexOf( 'b' );
How about s.indexOf( 'L' );
lastIndexOf( char c ) – return the index of the last occurrence of the character
pf2

Partial preview of the text

Download Java User Input and String Methods - CSIS 110 Lecture 16 - Prof. Brian F. Hanks and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 - Lecture 16

User Input Java provides a Library class that we can use to get user input from a text window. This class is called Scanner. Before we can use the Scanner class, we need to import it (java.util.Scanner). We can construct a new Scanner object and associate it with the keyboard: Scanner stdin = new Scanner( System.in ); (stdin – standard input device) The Scanner class contains the following methods: String nextLine() – read from the Scanner up to the return key Int nextInt() – read an int from the Scanner. An error occurs if the next item is not an int. double nextDouble() boolean nextBoolean() boolean hasNext() – returns true if there is data available in the Scanner. Let's try to write some code that gets an int from the user. The String class We've seen lots of Strings. The String class includes lots of methods. What methods have we already seen in this class? length() equals() Here are some more: charAt( int index ) – return the character at the given index in the String. What is the valid range of indices? Examples: String s = "Hello world!". How do I extract the first character? The fourth character? The last character? indexOf( char c ) – return the index of the first occurrence of the character in the String. What is s.indexOf( 'w' )? How about s.indexOf( 'l' ); How about s.indexOf( 'b' ); How about s.indexOf( 'L' ); lastIndexOf( char c ) – return the index of the last occurrence of the character

in the String toUpperCase() – return a new String that is the original String converted to UPPER. Important – String methods don't modify the original String – they return new Strings. toLowerCase() – return a new String that is the original String converted to LOWER substring( int startPos, int endPos ) – return the String that consists of the characters in the original String from startPos to endPos – 1. The new String does NOT contain the character at endPos. Way to remember this: the length of the new String is the difference between endPos and startPos. substring( int startPos ) – return the substring of the original String starting at startPos and continuing to the end of the original String. trim() – return a new String which has whitespace trimmed off the ends of the original String. This doesn't remove all whitespace from a String – only at the ends. Let's try some examples: Count the number of 'A's in a String Print every other character in a String Print a String in reverse order Lab Exercise In genetics, DNA is represented using the symbols A, G, T, and C. Write a method that asks the user to enter a DNA sequence. Your method should examine the String, and print one of two messages: "That String represents a DNA sequence" "That is not a DNA sequence. The character at position is not one of A, G, T, or C." Your method may return after it finds the first invalid character.