Question

In: Computer Science

The files provided in the code editor to the right contain syntax and/or logic errors. In...

The files provided in the code editor to the right contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly.

Please Fix code and make Copy/Paste avaliable

// Application allows user to enter a series of words

// and displays them in reverse order

import java.util.*;

public class DebugEight4

{

   public static void main(String[] args)

   {

      Scanner input = new Scanner(System.in);

      int x = 0, y = 0;

      String array[] = new String[100];

      String entry;

      final String STOP = "STOP";

      StringBuffer message = new StringBuffer("The words in reverse order are\n");

     

      System.out.println("Enter any word\n" +

      "Enter  + STOP +  when you want to stop");

      entry = input.next();

      while(!(entry.equals(STOP)))

      {

         array[x] = entry;

         ++x;

         System.out.println("Enter another word\n" +

         "Enter " + STOP + " when you want to stop");

         entry = input.next();

      }

      for(y = x - 1; y > 0; ++y)

      {

         message.append(array[y]);

         message.append("\n");

      }

      System.out.println(message);

}

}

Solutions

Expert Solution

Explanation:

Two logical errors were there in the for loop, written at the last.

1) y should start from x-1 and keep decrementing by 1 every time, it was getting incrementing instead.

2) In the condition of the for loop, index 0 should also be covered, so, y>=0 is the right condition

Correct code:

import java.util.*;

public class DebugEight4

{

public static void main(String[] args)

{

Scanner input = new Scanner(System.in);

int x = 0, y = 0;

String array[] = new String[100];

String entry;

final String STOP = "STOP";

StringBuffer message = new StringBuffer("The words in reverse order are\n");

System.out.println("Enter any word\n" +

"Enter + STOP + when you want to stop");

entry = input.next();

while(!(entry.equals(STOP)))

{

array[x] = entry;

++x;

System.out.println("Enter another word\n" +

"Enter " + STOP + " when you want to stop");

entry = input.next();

}

for(y = x - 1; y >= 0; --y)

{

message.append(array[y]);

message.append("\n");

}

System.out.println(message);

}

}

Output:

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!
PLEASE COMMENT IF YOU NEED ANY HELP!


Related Solutions

The files provided in the code editor to the right contain syntax and/or logic errors. In...
The files provided in the code editor to the right contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. DebugBox.java public class DebugBox { private int width; private int length; private int height; public DebugBox() { length = 1; width = 1; height = 1; } public DebugBox(int width, int length, height) { width = width; length = length; height =...
The following code segment which uses pointers may contain logic and syntax errors. Find and correct...
The following code segment which uses pointers may contain logic and syntax errors. Find and correct them. a) Returns the sum of all the elements in summands int sum(int* values) { int sum = 0; for (int i = 0; i < sizeof(values); i++) sum += *(values + i); return sum; } b) Overwrites an input string src with "61C is awesome!" if there's room. Does nothing if there is not. Assume that length correctly represents the length of src....
Each of the following files in the Chapter15 folder of your downloadable student files has syntax and/or logic errors.
Each of the following files in the Chapter15 folder of your downloadable student files has syntax and/or logic errors. In each case, determine the problem and fix the program. After you correct the errors, save each file using the same filename preceded with Fix. For example, DebugFifteen1.java will become FixDebugFifteen1.java. a. DebugFifteen1.java b. DebugFifteen2.java c. DebugFifteen3.java d. DebugFifteen4.java    
identify the syntax errors in the following code:             public class Hello {                    &
identify the syntax errors in the following code:             public class Hello {                         private static int main(String [] args) {                                     string Msg=”Hello, Wrld!;                                     Sytem.out.println(msg+ “Ken")
To earn full credit, program must be free of syntax, run-time, and logic errors; include program...
To earn full credit, program must be free of syntax, run-time, and logic errors; include program comments; use reasonable readable variable names and prompts. To include variables in the input prompt, you must use concatenation character (+). For example: assume you already asked for input of employee's first name and last name (they are stored into variables FirstName and LastName, then use this prompt to ask for employee's weekly hours which includes the employee's full name in the input statement....
***The code is provided below*** When trying to compile the code below, I'm receiving three errors....
***The code is provided below*** When trying to compile the code below, I'm receiving three errors. Can I get some assistance on correcting the issues? I removed the code because I thought I corrected my problem. I used #define to get rid of the CRT errors, and included an int at main(). The code compiles but still does not run properly. When entering the insertion prompt for the call details, after entering the phone number, the program just continuously runs,...
There are two errors in this code. Identify the errors and give the correct code that...
There are two errors in this code. Identify the errors and give the correct code that will make the program to display the following output: Rectangle: height 2.0 width 4.0 Area of the Rectangle is 8.0 ----- public interface Shape { public double getArea(); } class Rectangle implements Shape { double height; double width; public Rectangle(double height, double width) { this.height=height; this.width=width; } public double getArea() { return height*width; } public String toString() { return "Rectangle: height "+height+" width "+width;...
Quality assurance department of software house reveals that 6.5% of programming code contain logical errors. Quality...
Quality assurance department of software house reveals that 6.5% of programming code contain logical errors. Quality check software is developed to analyze code and in a test, identifies the error in 90% of the code with errors and 5% of those without. If code is marked by the program as possibly having a logical error, what is the probability that the code DOES have an error?
The number of errors in each of 300 files has a Poisson distribution with 1.4 errors...
The number of errors in each of 300 files has a Poisson distribution with 1.4 errors per file on average. Assume the errors in different files are independent. Use the Central Limit Theorem to approximate the probability that the total number of errors is at least 400. (Use a calculator.)
The number of syntax errors in a programming assignment for 20 randomly selected students are as...
The number of syntax errors in a programming assignment for 20 randomly selected students are as follows: 21, 5, 44, 44, 22, 49, 2, 48, 17, 27, 21, 34, 24, 12, 43, 12, 30, 10, 15, 15. a. Construct an ordered stem-and-leaf display for this data. b. Find the sample variance, S2 , and the sample standard deviation, S, using the short-cut method. c. Find the sample lower quartile Q1 , median Q2 and Upper-Quartile Q3 . d. Construct a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT