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
A blue NFiPA label indicates: A) Health Hazard B) Special information C) Flammability D) Reactivity
elena55 [62]
Your answer would be A: Health Hazard
8 0
2 years ago
Read 2 more answers
The term ________ refers to the use of a single unifying device that handles media, internet, entertainment, and telephone needs
dezoksy [38]
The correct answer that would best complete the given statement above would be DIGITAL CONVERGENCE. The term Digital Convergence <span>refers to the use of a single unifying device that handles media, internet, entertainment, and telephone needs. Thanks for posting your question. Hope this helps. </span>
5 0
3 years ago
In a program you need to store identification numbers of 5 employees and their weekly gross pay.
Allushta [10]

Solution :

a). The two arrays that are used in parallel in order to store the identification of two numbers of 5 employees and their weekly gross payments is  :

  int id_array[5];

  double gross_pay[5];

b). The loop that uses the arrays for printing the identification number and the weekly gross payment  is given below :

 for(int i=0; i<5; i++)

  {

  count <<id_array[i] <<" " << gross_pay[i] << end;

  }

4 0
2 years ago
SmartArt is a Microsoft Office tool for creating what?
Rufina [12.5K]

Explanation:

Create a SmartArt graphic to quickly and easily make a visual representation of your information. You can choose from among many different layouts, to effectively communicate your message or ideas. SmartArt graphics can be created in Excel, Outlook, PowerPoint, and Word, and they can be used throughout Office

and if it correct than give

4 0
2 years ago
Read 2 more answers
Changing the configuration of a database falls under which category of databaseâ administration?
harkovskaia [24]
Changing the configuration of a database falls under which category of databaseâ administration?

d. operation
8 0
2 years ago
Other questions:
  • 1) why is software engineering considered engineering and not manufacturing?
    9·1 answer
  • Which of the following is a unique feature of Object-oriented programming?
    6·2 answers
  • Insert a function in cell g5 to calculate the first student's monthly payment, using appropriate relative and absolute cell refe
    13·1 answer
  • The "Rudolph Rule" is best described by which of the following?
    9·1 answer
  • The _____ handles the instructions for your computer to start up before the operating system is loaded.
    10·1 answer
  • Create a function called "strip_r_o" that takes in a STRING and strips all the Rs and Os from the string. Also use a FOR loop in
    7·1 answer
  • The acronym pies is used to describe improvised explosive devices (ied) components. pies stands for:
    13·2 answers
  • I WILL GIVE BRAINLIEST TO WHO ANSWERS FIRST AND CORRECTLY.
    8·1 answer
  • 20 POINTS! Which music making software is better? Ableton Live or Logic Pro? Name the advantages and disadvantages of each one!
    7·2 answers
  • In the following shell scripting extract, the initial values of variables s, c and p are 0, 1, 1 respectively. What will be the
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!