

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 2
This page cannot be seen from the preview
Don't miss anything!
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