Question
Java Fundamentals Lab Objectives Use string methods to manipulate string data Communicate with the user by using the Scanner class or dialog boxes Introduction This
Java Fundamentals
Lab Objectives
Use string methods to manipulate string data
Communicate with the user by using the Scanner class or dialog boxes
Introduction
This lab introduces communicating with the user by investigating the lines of code that we need to add in order to use the Scanner class. We will also learn the method call needed for output.
The String class is introduced and we will use some of the available methods to prepare you for string processing.
Task #1 Using the Scanner Class for User Input
Download the file Lab2b.zip from Blackboard. Extract it, and open the project folder in Netbeans.
Add an import statement above the class declaration to make the Scanner class available to your program.
In the main method, create a Scanner object and connect it to the System.in object.
Declare a String variable for firstName, lastName, and fullName. Declare a char variable for firstInitial.
Prompt the user to enter his/her first name.
Read the name from the keyboard using the nextLine method, and store it into a variable called firstName (you will need to declare any variables you use).
Prompt the user to enter his/her last name.
Read the name from the keyboard and store it in a variable called lastName.
Concatenate the firstName and lastName with a space between them and store the result in a variable called fullName, using the + sign.
Print out the fullName.
Compile, debug, and run, using your name as test data.
Hint: import java.util.Scanner; ----- Scanner keyboard = new Scanner(System.in); String varA = keyboard.nextLine( );
Task #2 Working with Strings
1. Use the charAt method to get the first character in firstName and store it in a variable called firstInitial
Example
String name = Mike;
char letter = name.charAt(0);
Print out the users first initial.
Use the toUpperCase method to change the fullName to all capitals and store it back into the fullName variable
Example
String name = Mike; name = name.toUpperCase();
Add a line that prints out the value of fullName and how many characters (including the space) are in the string stored in fullName (use the method length to obtain that information). Heres an example of how to use the .length method:
int len = variable.length( );
Compile, debug, and run. The new output should have your initials and your full name in all capital letters, and the length of your full name.
This is what I have so far:
package lab2b; import java.util.Scanner;
/** * * @author Charters */
//LAB 2B //TASK #1 Add import statement here to use the Scanner class //TASK #1 (Alternate) Add import statment to use JOptionPane class
public class Lab2b {
public static void main(String[] args) { String firstname; String lastname; Scanner keyboard = new Scanner (System.in); System.out.println("What is your first name?"); firstname = keyboard.nextLine(); System.out.println("what is your last name?"); lastname = keyboard.nextLine(); char start = firstname.charAt(0); System.out.println(start); fullname = firstname + " " + lastname; System.out.println("Your fullname is " + fullname.toUpperCase()); // int len = fullname.length(); System.out.println("The length of " + fullname + " is : " + len); //declare additional variables here(i.e. firstName) //TASK #1 Create a Scanner object here (not used for alternate)
// ADD LINES FOR TASK #1 HERE // prompt the user for first name
// read the user's first name
// prompt the user for last name
// read the user's last name
// concatenate the user's first and last names
// print out the user's full name
System.out.println(); // to leave a blank line
// ADD LINES FOR TASK #2 HERE // get the first character from the user's first name
// print out the user's first initial
// convert the user's full name to all capital letters
// print out the user's full name in all capital letters
// and number of characters in it System.out.println(); // to leave a blank line
} }
I know I'm missing something. Need help finishing this.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started