Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do l get rid of this error l am having ? If you run the program you will und

ID: 3623387 • Letter: H

Question

How do l get rid of this error l am having ? If you run the program you will understand the nature of the error l am talking about

public class test1
{
public static void main ( String [] args )
{
String prepend = "00007";
int length = prepend.length();
char lastDigit;
String firstPart;
int n;

for (int i = length; i >=3; i--)
{
lastDigit = prepend.charAt(i-1);
firstPart = prepend.substring(0,i-1);
n = Math.abs(Integer.parseInt((firstPart))-2*(lastDigit-'0'));




prepend = Integer.toString(n);




System.out.println(firstPart);
System.out.println(lastDigit);
System.out.println("Prepend" + prepend);
}
}
}

Explanation / Answer

please rate - thanks

If I knew what you were doing, I would have looked at the code more closely, and given you a correction

the problem is you are replacing prepend with n

n is only 2 characters, prepend was 5 characters, so when it gets to character 3 it doesn't exist

if you explain what you are doing I'll help further

public class test1
{
public static void main ( String [] args )
{
String prepend = "00007";
int length = prepend.length();
char lastDigit;
String firstPart;
int n;

for (int i = length; i >=3; i--)
{
lastDigit = prepend.charAt(i-1);
firstPart = prepend.substring(0,i-1);
n = Math.abs(Integer.parseInt((firstPart))-2*(lastDigit-'0'));
prepend = Integer.toString(n);                                          //           right here
System.out.println(firstPart);
System.out.println(lastDigit);
System.out.println("Prepend" + prepend);
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote