2.1.breakname.gray

2.1.breakname.gray - Your Turn, Again! Write a Java program...

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

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Your Turn, Again! Write a Java program called BreakName, which asks the user for his/her name in the form First M. Last, and outputs First, M., and Last on three different lines. In other words, after the name is read from input, the program needs to break it up in the three pieces (First, M., and Last) and output those one line at a time. CSE 201 – Elementary Computer Programming 126 Step 1: Program Skeleton public class BreakName { public static void main(String args) { } } 127 CSE 201 – Elementary Computer Programming •This is the generic skeleton for all Java programs. •The name of our class is BreakName 126 127 Step 2: Initialize Scanner import java.util.Scanner; public class BreakName { public static void main(String args) { Scanner keyboard = new Scanner(System.in); Step 3: Prompt User import java.util.Scanner; public class BreakName { public static void main(String args) { Scanner keyboard = new Scanner(System.in); System.out.print("Please enter a name: "); } } 128 } } 129 CSE 201 – Elementary Computer Programming CSE 201 – Elementary Computer Programming •Since we’ll be getting input from the user, we must import the java.util.Scanner class. •We will call our Scanner keyboard. •At this point, we ask the user for their name. •Notice it’s a print, not a println. •This keeps the user’s input on the same line as the prompt. 128 129 Step 4: Read User Input import java.util.Scanner; public class BreakName { public static void main(String args) { Scanner keyboard = new Scanner(System.in); System.out.print("Please enter a name: "); String fullName = keyboard.nextLine(); Step 5: First Name import java.util.Scanner; public class BreakName { public static void main(String args) { Scanner keyboard = new Scanner(System.in); System.out.print("Please enter a name: "); String fullName = keyboard.nextLine(); int indexOfFirstSpace = fullName.indexOf(" "); String firstName = fullName.substring(0, indexOfFirstSpace); System.out.println(firstName); } } 130 } } 131 CSE 201 – Elementary Computer Programming CSE 201 – Elementary Computer Programming •We use the nextLine method to grab the user’s input. •We store this in the String fullName. •Now, we process the String in order to get the first name of the user. •For additional information, see following slides. 130 131 Step 6: Middle Initial import java.util.Scanner; public class BreakName { public static void main(String args) { Scanner keyboard = new Scanner(System.in); System.out.print("Please enter a name: "); String fullName = keyboard.nextLine(); int indexOfFirstSpace = fullName.indexOf(" "); String firstName = fullName.substring(0, indexOfFirstSpace); System.out.println(firstName); int indexOfPeriod = fullName.indexOf("."); String middleInitial = fullName.substring(indexOfFirstSpace + 1, indexOfPeriod + 1); System.out.println(middleInitial); Step 7: Last Name import java.util.Scanner; public class BreakName { public static void main(String args) { Scanner keyboard = new Scanner(System.in); System.out.print("Please enter a name: "); String fullName = keyboard.nextLine(); int indexOfFirstSpace = fullName.indexOf(" "); String firstName = fullName.substring(0, indexOfFirstSpace); System.out.println(firstName); int indexOfPeriod = fullName.indexOf("."); String middleInitial = fullName.substring(indexOfFirstSpace + 1, indexOfPeriod + 1); System.out.println(middleInitial); String lastName = fullName.substring(indexOfPeriod + 2, fullName.length()); System.out.println(lastName); } } CSE 201 – Elementary Computer Programming 133 } } 132 CSE 201 – Elementary Computer Programming •Step 2 in processing is to find the middle initial. •See following for additional information. •Finally, we process the last name. 132 133 ...
View Full Document

Ask a homework question - tutors are online