1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Nana76 [90]
2 years ago
10

Write code to take a String input from the user, then print the first and last letters of the string on one line. Sample run: En

ter a string: surcharge se Hint - we saw in the lesson how to get the first letter of a string using the substring method. Think how you could use the .length() String method to find the index of the last character, and how you could use this index in the substring method to get the last character.
Computers and Technology
1 answer:
Ainat [17]2 years ago
3 0
<h2>Answer:</h2>

//import the Scanner class to allow for user input

import java.util.Scanner;

//Begin class definition

public class FirstAndLast{

   //Begin the main method

    public static void main(String []args){

       

       

       //****create an object of the Scanner class.

       //Call the object 'input' to receive user's input

       //from the console.

       Scanner input = new Scanner(System.in);

       

       //****create a prompt to tell the user what to do.

       System.out.println("Enter the string");

       

       //****receive the user's input

       //And store it in a String variable called 'str'

       String str =  input.next();

       

       //****get and print the first character.

       //substring(0, 1) - means get all the characters starting

       //from the lower bound (index 0) up to, but not including the upper

       //bound(index 1).

       //That technically means the first character.

       System.out.print(str.substring(0,1));

       

       //****get and print the last character

       //1. str.length() will return the number of character in the string 'str'.

       //This is also the length of the string

       //2. substring(str.length() - 1) - means get all the characters starting

       // from index that is one less than the length of the string to the last

       // index (since an upper bound is not specified).

       // This technically means the last character.

       System.out.print(str.substring(str.length()-1));

       

       

       

    }  // end of main method

   

} //end of class definition

<h2>Explanation:</h2>

The code has been written in Java and it contains comments explaining important parts of the code. Kindly go through the comments.

The source code and a sample output have also been attached to this response.

To run this program, copy the code in the source code inside a Java IDE or editor and save it as FirstAndLast.java

Download java
<span class="sg-text sg-text--link sg-text--bold sg-text--link-disabled sg-text--blue-dark"> java </span>
4c53f66d970acf88b251b2cf41bcdaf6.png
You might be interested in
Linda moderates the comments section of an online travel magazine. Which type of comments should Linda flag or delete as inappro
kvv77 [185]
Generally, an online newspaper has a code of conduct for the comments accepted. Hate speech, racism, cursing, sexual comments or bigotry is usually not allowed. She may also want to be looking for spam comments such as links or ads where someone is trying to sell something from the comment section
8 0
3 years ago
Read 2 more answers
List out the process of Assembly in the film editing process. NO LINKS. answer ASAP
kobusy [5.1K]

Answer:

just try your best

Explanation:

7 1
2 years ago
Read 3 more answers
Question of computer sciences​
BARSIC [14]

Answer:

a ans is cyber crime

b ans is hacking

c ans is cracker

d ans is computer ethics

e ans is phishing

4 0
2 years ago
Buenas, podrían ayudarme con alguna suite <br> ofimatica, para que sirve y sus características?
navik [9.2K]

Answer:

Say what?!?

Explanation:

6 0
2 years ago
Write a Program thatask the User to input the Radius and Length ofa Cylinder.
AveGali [126]

Answer:

#include <iostream>

using namespace std;

int main() {

   float radius,length,vol,area;//flioat variables to hold radius,length,volume and area.

   cout<<"Enter the radius and length of the cylinder respectively"<<endl;

   cin>>radius>>length;//taking input of radius and length..

   vol=3.14*radius*radius*length;//calculating volume..

   radius=radius*12;//converting radius from feet to inches..

   length=length*12;//converting area from feet to inches..

   area=2*3.14*radius*(length+radius);//calculating area..

   cout<<"Volume in cubic feet is ="<<vol<<endl<<"Area in square inches is ="<<area<<endl;//printing the output..

return 0;

}

Output:-

Enter the radius and length of the cylinder respectively

5 2

Volume in cubic feet is =157

Area in square inches is =31651.2

Explanation:

In the program above I have taken four float variables for storing radius,length,volume and surface area of the cylinder.Then calculating the volume and after that converting radius and length to inches and after that calculating area in square inches.

4 0
3 years ago
Other questions:
  • Flowgorithm, Design a program that prompts the user to enter a number within the range of 1 through 10. The program should displ
    8·1 answer
  • Suppose that you want to write a program that inputs customer data and displays a summary of the number of customers who owe mor
    14·1 answer
  • A group of students are collaborating on an online research project. What is an example of appropriate online behavior?
    10·2 answers
  • 6 external parts of computer
    10·2 answers
  • A linear gradient can be positioned from left to right, top to bottom, or on any angle?
    11·1 answer
  • Which is true of ASCII and Unicode?
    7·1 answer
  • (I need help also can u guys just give me some example just in case I got confuse)
    13·1 answer
  • What colorful format should you use to bring attention to an important sentence or phrase
    14·1 answer
  • Which option is the easiest way to configure macros in Access 2016?
    6·1 answer
  • Why is a Quality assurance tester needed on a software development team?
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!