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
How do ice and water on the ground affect incoming solar radiation? They filter 22 percent of solar radiation that reaches the s
olganol [36]

Answer: 4 percent

Explanation:

8 0
3 years ago
Read 2 more answers
Jazmine just finished setting up an operating system that's designed to work between a VM guest OS and computer hardware. What i
VikaD [51]

The name of the operating system Jazmine is setting up is option A: Client  operating system.

<h3>What  is the operating system running in virtual machines?</h3>

A guest or client operating system is known to be the operating system that one can installed on a virtual machine (VM) or on any kind of partitioned disk.

Hence, the name of the operating system Jazmine is setting up is option A: Client  operating system.

Learn more about operating system from

brainly.com/question/22811693

#SPJ1

3 0
1 year ago
I'm getting an iphone xr today. what should i do 1st? Any cool fetures? i have a iphone 6 now so its a pretty big upgrade
Sonja [21]

Answer:

I wouldn't get an iphone xr my stepmom had one and the screen started having problems and the apps were glitching.

8 0
2 years ago
Read 2 more answers
What is the maximum number of VLANs that can be configured on a switch supporting the 802.1Q protocol? Why?
KATRIN_1 [288]

Answer:

Under IEEE 802.1Q, the maximum number of VLANs on a given Ethernet network is 4,094 (4,096 values provided by the 12-bit VID field minus reserved values at each end of the range, 0 and 4,095).

Explanation: found it from google

6 0
3 years ago
Read 2 more answers
In what ways can information be slanted in a news report? List at least five ways.
Sedbober [7]

Answer:

author, journalist and speaker/media trainer this is all I know so

author, journalist and speaker/media trainer

6 0
3 years ago
Read 2 more answers
Other questions:
  • What is one important feature of an aup? 1. a list of all available courses. 2. a clear out line of the consequences of violatin
    12·2 answers
  • Please help
    5·1 answer
  • Select the correct answer. Which sentence best describe an effective management strategy? A. Conceal game-related clippings prio
    12·2 answers
  • What command would you use to list the text files in your
    7·1 answer
  • Which software product release category is "generally feature complete and supposedly bug free, and ready for use by the communi
    7·1 answer
  • Karen took an assessment with 291 questions, and it described her preferred style of working, learning, leading, risk-taking and
    13·1 answer
  • Help pleaseeeeeeeeeeeee
    13·2 answers
  • This is in government
    15·1 answer
  • How would you define the rule of thirds?
    11·1 answer
  • Anyone have any website ideas I could use for my computing website project? Thank you.
    15·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!