All pastes #406724 Raw Edit

Cool123

public java v1 · immutable
·published 2007-03-22 17:21 UTC
rendered paste body
/* Imtiaz Mangerah    GR 11      * * Stanger Secondary School      * * March Assignment - Question 4 */import java.util.*;public class assignmentQ4{	static Scanner in = new Scanner(System.in);	public static void main(String[] args)	{		// Declerations		String input = "";		int len, mid;		System.out.println("Enter exit to quit");		while (input.equals(""))  // Avoids StringIndexOutOfBoundsException by forcing input of at least 1 character		{			System.out.print("\na) Enter any sentence: ");			input = in.nextLine();  // Get input		}		while (!input.equalsIgnoreCase("exit")) // Loop while input is not 'exit'		{			len = input.length(); // Gets length of input			mid = len/2;  // Finds the middle number as integer			// The output of the program is below.  Note we use single quotes (') to make output neater or indicate a white space.			// Below prints the first and last number using the length and charAt			System.out.println("b) First letter: '" + input.charAt(0) + "' and Last Letter: '" + input.charAt(len-1)+ "'");			// Below we reverse the string by using a backward for loop and printing the result one character at a time			System.out.print("c) String Reversed: '");			for (int i = (len-1); i >= 0; i--)				System.out.print(input.charAt(i));			/* Below prints 1 char (middle of string) if it is odd or 2 chars in the middle if even.			 * The "'\n" closes the previous output and starts on a new line. */			if (len%2==1)				System.out.println("'\nd) Middle Character: '" + input.charAt(mid)+"'");			else				System.out.println("'\nd) Middle Characters: '" + input.charAt(mid-1) + input.charAt(mid)+"'");			// Below we check if each char in string is a vowel by using a for loop for each char and a nested if statement			System.out.print("e) Vowels: '");			for (int i = 0; i != (len-1); i++)			{				String check = "" + input.charAt(i);				if (check.equalsIgnoreCase("a") || check.equalsIgnoreCase("e") || check.equalsIgnoreCase("i") || check.equalsIgnoreCase("o") || check.equalsIgnoreCase("u"))					System.out.print(input.charAt(i));			}			// Below we print out the string with ' ' replaced with '*'.  The "'\n" closes the previous output and starts on a new line.			System.out.println("'\nf) " + input.replace(" ", "*")); // We dont have to store the new string because we print it directly.			input = "";  // Reset string			while (input.equals(""))  // Avoids StringIndexOutOfBoundsException by forcing input of at least 1 character			{				System.out.print("\na) Enter any sentence: ");				input = in.nextLine();  // Get input			}		}	}}