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
What is the smallest amount of information called?
aksik [14]

Answer:

Byte

Explanation:

5 0
3 years ago
Read 2 more answers
Working together, printer A and printer B would finish the task in 24 minutes. Printer A alone would finish the task in 60 minut
sasho [114]

Answer:

The correct answer to this question is option (1).

Explanation:

Let x1 = Is number of pages that printer A can print  in page/minute.  

thus x1 + 5 = Is number of page that printer B can print in page/minute.  

As printer A finishes his work in 60 min. So the task has 60x1 pages. As printers A and B both finish that task in 24 minutes. So the equation can be given as:  

60x1 = 24x1 + 24(x1 + 5)  

 

60x1 = 24x1 + 24x1 + 120  

 

60x1=48x1 + 120

 

60x1-48x1=120

 

12x1=120  

 

x1=120/12

 

x1=10

put the value of x1 in to (60x1). Therefore, the task has 60(10) = 600 pages. the task contains 600 pages.  

So the answer to this question is option (1).

7 0
3 years ago
How can you have a safe browser experience
goldfiish [28.3K]

Most likely the answer is to delete cookies, as it cuts down on the amount of tracking sites can do to you.

6 0
2 years ago
Given a string variable named sentence that has been initialized , write an expression whose value is the the very last characte
Blizzard [7]
Strings can usually be dealt with as arrays of characters.

sentence[ -1 ]

sentence.substring( sentence.size() -1 );
8 0
3 years ago
The Mark Table of Contents Entry option is used for marking nonheading text for the table of contents. the options are manually
enyata [817]

Answer:number 1

Explanation:asian culture

5 0
2 years ago
Read 2 more answers
Other questions:
  • When you enter a two-digit year that is less than 30, Excel changes the year to _____. Question 7 options:
    7·1 answer
  • Host to IP address lookup and its reverse lookup option are very important network services for any size network. It is also how
    14·1 answer
  • When a client computer wants to connect to a service instance, what specific name type does it use to find the service?
    9·1 answer
  • A shortage of blood for transfusions for injured animals has resulted in the introduction of a synthesized product called Oxyglo
    15·1 answer
  • If you do not want to keep a change made by the autocorrect feature, you can click the ____ button on the auto correction option
    12·1 answer
  • Does this mechanism increase or decrease speed? Why?
    9·1 answer
  • Choose the term that matches the action.
    5·2 answers
  • 8.Which of the following IC was used in third generation of computers?Immersive Reader
    13·1 answer
  • Project stem test 3 answers
    14·1 answer
  • How does a Cloud-first strategy approach a client's migration to the Cloud?by removing outside ecosystem partners from the Cloud
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!