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
___________ is a task pane used to correct grammar errors; opens when you click the Spelling & Grammar button in the Proofin
BaLLatris [955]

Answer:

Spelling Task Pane

Explanation:

According to my research on Microsoft Office Studio, I can say that based on the information provided within the question the feature being mentioned in the question is called the Spelling Task Pane. By selecting this pane word will offer various grammar and spelling assistance, such as correcting words and offering one or more suggestions.

I hope this answered your question. If you have any more questions feel free to ask away at Brainly.

7 0
4 years ago
Read 2 more answers
SOMEONE PLEASE HELP ME OUT WITH THIS!!!!!!
mario62 [17]

Answer:

C.

Explanation:

8 0
3 years ago
Read 2 more answers
A slide can have which of the following?
Margarita [4]

Answer:

D all of them mofos

Explanation:

7 0
3 years ago
Identify the statement below that is not true of the 2013 COSO Internal Control updated framework. a. It provides users with mor
gladu [14]

Answer:

C. It adds many new examples to clarify the framework concepts.

Explanation:

COSO is short for Committee of Sponsoring Organizations of the TreadWay Commission.The original framework released by COSO was the Internal Control-Integrated Framework, published in the year 1992. The purpose of this framework is to help in establishing effective internal control in businesses.

The updated framework was published in the year 2013. The purpose of this updated frame work includes;

  • helping businesses in establishing an effective and efficient internal control system that would help them in the implementation and documentation of issues affecting their businesses.

  • It also provides users with more precise guidelines which does not take away from the original framework, but rather builds on it.
6 0
3 years ago
Two people, Alice and Bob are using the key sharing algorithm. They have chosen a clock size of 17 and a base of 5. Alice's priv
hjlf

Answer:

fsfsf

Explanation:

6 0
3 years ago
Other questions:
  • Optimization Score is made up of over 50 recommendations to optimize Search campaigns.A. TrueB. False
    11·1 answer
  • Which of the following types of memory extends the actual physical memory of a computer by copying blocks of data from the memor
    11·1 answer
  • The following code processes a file containing five positive numbers. What will the variable $result contain after the code is e
    13·1 answer
  • Henry is nervous speaking in public, and it shows every time he presents his ideas to his coworkers and manager. One of his team
    6·2 answers
  • All of the following are ways to save money on transportation except :
    7·1 answer
  • What file system allows you to continue to add files to a CD or DVD disc after the initial burn, as long as there is room on the
    14·1 answer
  • 6.Which of the following statements illustrates a characteristic of a person's attitudes
    14·2 answers
  • Failing to include a complete ____ on each side of an && operator in an if statement is a common error in Java programmi
    6·2 answers
  • What do u think a creative app must have? <br><br> Please answer the question ASAP!!
    5·1 answer
  • PLEASE HELP!! I just joined this school and it was a online day but I didn’t have time to study because I’m new please help me i
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!