220k views
0 votes
In this exercise you will debug the code which has been provided in the starter file. The code is intended to do the following:

take a string input and store this in the variable str1
copy this string into another variable str2 using the String constructor
change str1 to the upper-case version of its current contents
print str1 on one line, then str2 on the next

1 /* Lesson 4 Coding Activity Question 2 */
2
3 import java.util.Scanner;
4
5 public class U2_L4_Activity_Two{
6 public static void main(String[] args){
7
8 Scanner = new Scanner(System.in);
9 String str1 = scan.nextLine();
10 String str2 = String(str1);
11 str1 = toUpperCase(str1);
12 System.out.println("str1");
13 System.out.println("str1");
14
15 }
16 }

2 Answers

3 votes

Final answer:

The Java program contains errors which need corrections, such as fixing the scanner instantiation, using the String constructor correctly, calling the toUpperCase method appropriately, and printing the variable values without quotes.

Step-by-step explanation:

In reviewing and debugging the code provided for a Java program, there are several corrections to be made for it to execute properly. Firstly, we need to amend the scanner declaration to instantiate a Scanner object correctly. The corrected line should create an object named scan which makes use of the new keyword:

Scanner scan = new Scanner(System.in);

Next, to copy str1 into str2, we must call the String constructor appropriately by using the keyword new:

String str2 = new String(str1);

To convert str1 to upper case, we use the method toUpperCase on the string object and not separately:

str1 = str1.toUpperCase();

Finally, to print the values of str1 and str2, we should not enclose them in quotes:

System.out.println(str1);
System.out.println(str2);

After these changes, the code should work as intended, taking a user's input, copying it, converting it to upper case, and then printing both the original and upper-case versions.

User ThomasNicholas
by
3.4k points
7 votes

Final answer:

The code requires several fixes: properly initializing the Scanner object, using the correct syntax for creating a String copy, correcting the method call for converting to upper case, and fixing the print statements to output variable values.

Step-by-step explanation:

To debug the provided Java code, certain corrections need to be made so that the program properly reads a string, creates a copy, converts to upper case, and then prints both strings. Here's the revised code with explanations:

  • Line 8 should initialize a Scanner object correctly. It is currently missing the variable name. It should be Scanner scan = new Scanner(System.in);
  • Line 10 should use the correct syntax for creating a new String object. Replace String(str1) with new String(str1).
  • Line 11 should call the toUpperCase method on the str1 object correctly. Replace toUpperCase(str1) with str1.toUpperCase().
  • Lines 12 and 13 should print the actual values of the variables str1 and str2. Remove the quotation marks around str1 so that it reads System.out.println(str1); and change the second System.out.println("str1"); to System.out.println(str2);

With these corrections, the code will function as intended: reading an input string, copying it, converting the original to upper case, and then printing both the upper case and original strings.

User Maroun
by
3.3k points