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 aboutpublic 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);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.