answer.
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]
1 year 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]1 year 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
In cell M2, enter a formula using a nested IF function as follows to determine first if a student has already been elected to of
aleksley [76]

Answer:

Following are the code to this question:

code:

=IF(EXACT(I2,"Yes"),"Elected",IF(EXACT(K2,"Yes"),"Yes","No"))

Explanation:

In the given the data is not defined so we explain only  the above code, but before that, we briefly define working of if the function that can be defined as follows:

  • The If() function, is used only when one of the logical functions and its value must return the value true. or we can say it only return a true value.
  • In the above function, a column "Elected" is used that uses other column values to check this column value is equal if this condition is true it will return "yes" value.

8 0
1 year ago
Write a program that takes a single integer input from the user and stores it in a variable. Your program should increase the va
Allushta [10]

Answer:

Question is answered using python:

num = int(input("User Input: "))

for i in range(3):

    num = num+1

    print("Number is now "+str(num))

for i in range(3):

    num = num-1

    print("Number is now "+str(num))

Explanation:

This line prompts user for input

num = int(input("User Input: "))

The following iterates from 1 to 3

for i in range(3):

This increments user input each time

    num = num+1

This prints the value of num after increment

    print("Number is now "+str(num))

The following iterates from 1 to 3

for i in range(3):

This decrements the value of num

    num = num-1

This prints the value of num after decrement

    print("Number is now "+str(num))

8 0
1 year ago
1. Do you consider Facebook, MySpace, and LinkedIn forms of disruptive or sustaining technology? Why?
Mrrafil [7]
It mainly just depends on if you "misuse" them.
8 0
1 year ago
Secure Wi-Fi networks and VPNs use _____ to secure data transferred over a network.
Aleksandr-060686 [28]

VPNs and Wifi networks use tunneling to send data privately over a network

4 0
1 year ago
Ishaan is confused between the terms webpage and website help him in understanding the difference between both​
alexgriva [62]

Answer:

A webpage is a class website and a website is a link where you can find useful information.

Explanation:

Please follow

7 0
2 years ago
Other questions:
  • In the game Beehive, you play the role of a worker bee who must watch over her hive. Your duties include (among others) directin
    12·2 answers
  • . Write a recursive function names factorial to compute the factorial of the parameter. Also write the main function, where you
    15·1 answer
  • Write a program to determine all pairs of positive integers, (a, b), such that a < b < 1000 and [a2 + b2 + 1)/(ab) is an i
    13·1 answer
  • Database management systems are expected to handle binary relationships but not unary and ternary relationships.'
    7·1 answer
  • dam is writing a program that: 1) has the user guess a number, and 2) tells the user how many guesses it took to get the correct
    9·1 answer
  • You'd like to change the minimum password length policy in the Default Domain Policy group policy preference (GPO). What's the b
    10·1 answer
  • Pick a 3D game you enjoy or know about. Pick a scene from that game that has at least one character in it and describe the scene
    9·1 answer
  • Consider the packets exchanged in TCP connection setup between Host A and Host B. Assume that Host A's initial sequence number i
    7·1 answer
  • In below freezing conditions, keep your fuel level at least _________ full to keep moisture from freezing in your gas line.
    13·1 answer
  • In this exercise you will debug the code which has been provided in the starter file. The code is intended to take two strings,
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!