StringDemo - / System.out.println("The character in...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
// File: // Author: Amber Settle // Course: CSC 211 // Purpose: This application demonstrates the creation of an object // and the invocation of several of its methods / import java.util.Scanner; i public class StringDemo { // Create a String object, and call some of its methods / public static void main(String[] args) { String sentence = new String("This program uses the String class."); S System.out.println("The String is \"" + sentence + "\"\n"); S System.out.println("The length of the String is " + sentence.length() + "\n"); s System.out.println("The character in position 0 of the String is '" + sentence.charAt(0) + "'\n"); s // How would we create a statement to find the character // in the last position of the String, regardless of what // value is contained in the String?
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: / System.out.println("The character in the last position of the " + "string is '" + sentence.charAt(sentence.length()-1) + "'"); + System.out.println("'o' first appears in position " + sentence.indexOf('o') + "\n"); s System.out.println("\"uses\" first appears in position " + sentence.indexOf("uses") + "\n"); s System.out.println("The substring from 5 to 12 is '" + sentence.substring(5, 12) + "'\n"); s // Demonstrate how to use the charAt method to obtain a single // character of input from the user System.out.println(); S Scanner chScan = new Scanner(; S System.out.print("Enter the letter grade: "); String inputStr =; char grade = inputStr.charAt(0); c System.out.println("The grade entered is " + grade + "\n"); } }...
View Full Document

This note was uploaded on 03/05/2011 for the course CSCI 384 taught by Professor Drchan during the Spring '11 term at CA Health Sciences.

Ask a homework question - tutors are online