Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The code as shown in the picture, please determine which line of the code causes this error to occur, and how to fix it. (Simpler

The code as shown in the picture, please determine which line of the code causes this error to occur, and how to fix it. (Simpler way please)
And please describe the behavior of the code when it was executed, like what it does
Thank you so much
image text in transcribed
image text in transcribed
image text in transcribed
Project 02 Extra Credit Your code does NOT need to handle the case where you enter a substring that does not exist in the string (for example, trying to find the substring "kryptonite" in the string "The quick brown fox jumped over the lazy dog"). For up to 2 points of extra credit on this assignment: 1. Run your code and enter a substring that does not exist in the longer string. Using the errors reported by Eclipse, determine which line of your code causes this error 2. In the comments of your program, add a block under your name, date and other identifying information labelled "EXTRA CREDIT". Under this line, provide one to two to occur sentences in your own words that describe the behavior of your code when you execute it (do NOT just cut and paste the output explain what it does in plain English). 3. After this explanation, indicate which line of your code causes this error and give a single sentence explanation of what you would need to do to be able to fix this problem. (NOTE: you may not know enough Java yet to be able to fix this problem yet, so you do not need to actually fix the problem. Just indicate in your own words what you would need to be able to do to fix the problem) Enter a long string: The quick brown fox jumped over the lazy dog Enter a substring: kryptonite Length of your string: 44 Exception in thread "main" Length of your substring: 10 Starting position of your substring: -1 java lang.StringIndexOutofBoundsException: begin , end -1, length 44 at java.base/java. lang.String.checkBoundsBeginEnd(String java:3319) at java.base/java. lang.String.substring(String.java: 1874) at osu.cse1223. Project02.main(Project02.java:28) Prolect2osu.cse1223 Projecto2main(StringD): void 2% uthor Xinxin Sun 6 age osu.cse1223; 8 rt java.utit.Scanner 10 ic class Project2 { public static void main(String args) 13 14 1/ takes user inputs as strings, turns them into longstring and substring. Scanner keyboard new Scanner(System.in) 15 16 System.out.print("Enter a long string: ") 17 18 19 20 21 String longstring = keyboard.nextLine(); System.out.print("Enter a substring: "); String substringkeyboard.nextLine); //Calculate the length of the lonastring and substring. System.out.println("Length of your string: "longString. length)) System.out.print n("Length of your substring: subString.length)): 23 24 25 26 27 28 29 30 31 //use indexOf to find, the pestion of substring. int a longString.indexOf (subString): System.out.println("Starting position of your substring: "a) System.out.print ln("String before your substring:longString.substring(8, a)): System.out.printin("String after your substring: LongString.substring(a+subString. tength), longString System.out.print("Enter a postion between and longString. length)+": //Replace the original string with new string int b keyboard.nextInt0 34 char c longString.charAt(b): System.out.println("The character at positionbisc); System.out.print("Enter a replacement string: " String smalistringkeyboard.next(O String middleStringlongString.replaceAlt(subString, smallString): System.out.println"You new string is: "middleString); 36 37 8 40 h 41 42 43

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What four statements are contained in most annual reports?

Answered: 1 week ago