Contents

  1. ./ConsoleUsingScannerDemo.java
  2. ./ScannerDemoRectangle.java
  3. ./UsingStringAPI.java

./ConsoleUsingScannerDemo.java 1/3

[
top][prev][next]
import java.util.Scanner;

/**
 * A program that demonstrates reading in various information
 * from the console.
 * 
 * @author Sara Sprenkle
 */
public class ConsoleUsingScannerDemo {

	/**
	 * @param args
	 *            not used in this program
	 */
	public static void main(String[] args) {

		long tempLong;

		// Create the Scanner object, passing in the console input System.in
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Directions:");
        System.out.println("\tEnter an integer followed by a space and then some more characters.");
        System.out.println("\tThen, enter a big integer (a long).");

        // read in an integer and a String
        int i = scanner.nextInt();
        String restOfLine = scanner.nextLine();
        
        // read in a long
        tempLong = scanner.nextLong(); 
        
        scanner.close();
        
        System.out.println("Entered ");
        System.out.println("\tinteger: " + i);
        System.out.println("\trest of line: " + restOfLine);
        System.out.println("\tlong: " + tempLong);

   }
}

./ScannerDemoRectangle.java 2/3

[
top][prev][next]
import java.util.Scanner;

/**
 * A program that demonstrates reading in from the console, using calculating
 * the area of a rectangle as the example.
 * 
 * @author Sara Sprenkle
 */
public class ScannerDemoRectangle {

	/**
	 * @param args
	 *            not used in this program
	 */
	public static void main(String[] args) {

		// Create the Scanner object, passing in the console input System.in
		Scanner scanner = new Scanner(System.in);

		//scanner.useDelimiter("\n"); // breaks up by lines, useful for console I/O

		System.out.print("Please enter the width of a rectangle (an int): ");
		int width = scanner.nextInt();

		System.out.print("Please enter the height of a rectangle (a double): ");
		double length = scanner.nextDouble();

		System.out.println("The area of your rectangle is " + length * width + ".");
	}
}

./UsingStringAPI.java 3/3

[
top][prev][next]
/**
 * This class demonstrates using the String API
 *
 * @author Sara Sprenkle
 */
public class UsingStringAPI { 
  
   /**
    * Called when user runs 
    *  java UsingStringAPI
    */
   public static void main(String[] args) {
       String testString = "Demonstrate Strings";
       
       int length = testString.length();
       System.out.println("The length of " + testString + " is " + length);

       char character1;
       char character2 = testString.charAt(3);
       character1 = testString.charAt(testString.length()-2);
        
       System.out.println(character1 + " " + character2);
       
       String language = "Java!";
       String subStr = language.substring(1);
       String subStr2 = language.substring(2, 4);
       
       System.out.println(subStr);
       System.out.println(subStr2);
       
       String niceGreeting = "Hello";
       String firstName = "Clark";
       String lastName = "Kent";
       String blankSpace = " ";

       String greeting = niceGreeting + "," +
            blankSpace + firstName + 
            blankSpace + lastName;

       System.out.println(greeting);

   }

}

Generated by GNU Enscript 1.6.5.90.