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
Mariana [72]
3 years ago
7

In this exercise you will debug the code which has been provided in the starter file. The code is intended to take two strings,

s1 and s2 followed by a whole number, n, as inputs from the user, then print a string made up of the first n letters of s1 and the last n letters of s2. Your job is to fix the errors in the code so it performs as expected (see the sample run for an example).
Sample run
Enter first string
sausage
Enter second string
races
Enter number of letters from each word
3
sauces
Note: you are not expected to make your code work when n is bigger than the length of either string.
1 import java.util.Scanner;
2
3 public class 02_14_Activity_one {
4 public static void main(String[] args) {
5
6 Scanner scan = Scanner(System.in);
7
8 //Get first string
9 System.out.println("Enter first string");
10 String s1 = nextLine(); 1
11
12 //Get second string
13 System.out.println("Enter second string");
14 String s2 = Scanner.nextLine();
15
16 //Get number of letters to use from each string
17 System.out.println("Enter number of letters from each word");
18 String n = scan.nextLine();
19
20 //Print start of first string and end of second string
21 System.out.println(s1.substring(1,n-1) + s2.substring(s1.length()-n));
22
23
24 }

Computers and Technology
1 answer:
vampirchik [111]3 years ago
7 0

Answer:

Here is the corrected program:

import java.util.Scanner; //to accept input from user

public class 02_14_Activity_one { //class name

    public static void main(String[] args) { //start of main method

    Scanner scan = new Scanner(System.in); //creates Scanner class object

  System.out.println("Enter first string"); //prompts user to enter first string

          String s1 = scan.nextLine(); //reads input string from user

  System.out.println("Enter second string"); //prompts user to enter second string

          String s2 = scan.nextLine(); //reads second input string from user

    System.out.println("Enter number of letters from each word"); //enter n

          int n = scan.nextInt(); //reads value of integer n from user

          System.out.println(s1.substring(0,n) + s2.substring(s2.length() - n ));

         } } //uses substring method to print a string made up of the first n letters of s1 and the last n letters of s2.

Explanation:

The errors were:

1.

Scanner scan = Scanner(System.in);

Here new keyword is missing to create object scan of Scanner class.

Corrected statement:

 Scanner scan = new Scanner(System.in);

2.

String s1 = nextLine();  

Here object scan is missing to call nextLine() method of class Scanner

Corrected statement:

String s1 = scan.nextLine();

3.

String s2 = Scanner.nextLine();

Here class is used instead of its object scan to access the method nextLine

Corrected statement:

String s2 = scan.nextLine();

4.

String n = scan.nextLine();

Here n is of type String but n is a whole number so it should be of type int. Also the method nextInt will be used to scan and accept an integer value

Corrected statement:

int n = scan.nextInt();

5.

System.out.println(s1.substring(1,n-1) + s2.substring(s1.length()-n));

This statement is also not correct

Corrected statement:

System.out.println(s1.substring(0,n) + s2.substring(s2.length() - n ));

This works as follows:

s1.substring(0,n) uses substring method to return a new string that is a substring of this s1. The substring begins at the 0th index of s1 and extends to the character at index n.

s2.substring(s2.length() - n ) uses substring method to return a new string that is a substring of this s2. The substring then uses length() method to get the length of s2 and subtracts integer n from it and thus returns the last n characters of s2.

The screenshot of program along with its output is attached.

You might be interested in
Why people like adventures <br><br>​
OLEGan [10]

A lot of people do love going on different kinds of adventures. People do  like adventures because;

  • It helps to improve their physical health and it also hinders/ treat different scope or range of health issues.

  • It helps to make the human brain bigger that is, adventures such as hiking or walking helps to grow the brains.

  • People go on adventures so as to build or raise their tolerance level such as Tolerance for uncertainty.

  • Adventures helps to improve one's reflectiveness and mental skill
  • It gives you new ideas, feeds your dreams and also helps to builds your confidence.

Adventure is simply defined as the act of been involved in an unusual or exciting experience or any activity. It can be hazardous in nature.

Learn more about adventure from

brainly.com/question/25950911

3 0
2 years ago
It's safe to download files from the internet if u perform regular Windows security updates, that is all u need to protect your
Vlad1618 [11]

Answer

TRUE

Explanation

A computer virus is a self-replicating program which are malicious and are designed to infect and gain control over a computer without the owner's knowledge.

To protect your computer from viruses especially when downloading files from the internet, it is advisable for one to perform regular windows security updates. This is because because windows have malware definitions to its windows defender and security essential utilities.

7 0
3 years ago
Read 2 more answers
How do I make a stepper motor turn clockwise?
oksano4ka [1.4K]
Just reverse the connections to one winding and the motor will go the other way, I believe.
5 0
3 years ago
Why do we buy new asset​
Shalnov [3]

Your question is too vauge, what do you mean exactly in what terms because you aimed this to to computer and technology, In business terms asset is something that brings in money or brings a positive impact. A form of asset can be stocks, crypto, NFTs, real estate... If you own one of these you own an asset, its as simple as that.

3 0
2 years ago
A swimmer covers 180m in 20 seconds. How many metres does she swim in one second?​
dezoksy [38]

Answer:

9 meters per second

Explanation:

180/20= 9

4 0
2 years ago
Other questions:
  • Which of the following information is okay to share on social networking site?
    11·2 answers
  • What is the internet?
    5·2 answers
  • What is meant by encapsulating semaphores? Bring out the need for it
    12·1 answer
  • 5. Which of the following views is used to run a PowerPoint presentation?
    11·2 answers
  • 2.
    11·1 answer
  • When does the following while-loop stop running?
    10·1 answer
  • Write a program that declares a two-dimensional array named myFancyArray of the type double. Initialize the array to the followi
    12·1 answer
  • Company-wide systems that connect one or more local area networks (LANs) or wide area networks (WANs) are called _____.
    6·1 answer
  • Which new development in malware caused sandbox technology to automate and introduce artificial intelligence learning
    14·1 answer
  • What is the difference between an html opening tag and a closing tag?.
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!