Exception in thread "main" java.lang.NumberFormatException: For input string: "x" for Java code public class Finder { //Write two recursive functions, both of which will parse any length string that consists of digits and numbers. Both functions //should be in the same class and have the following signatures. //use the if/else statement , Find the base case and -1 till you get to base case //recursive function that adds up the digits in the String publicstaticint sumIt(String s) { //if String length is less or equal to 1 retrun 1. if (s.length()<= 1){ return Integer.parseInt(s); }else{ //use Integer.praseInt(s) to convert string to Integer //returns the interger values //else if the CharAt(value in index at 0 = 1) is not equal to the last vaule in the string else {//return the numeric values of a char value + call the SumIt method with a substring = 1 return Character.getNumericValue(s.charAt(0) ) + sumIt(s.substring(1)); } } //write a recursion function that will find the largest interger in the string. publicstaticint findMax(String s,intmax) {//check the characters of the string is greater than 0 if(s.length() == 0) { returnmax; } if(s.substring(0, 1).matches("[0-9]")) { intend = 1; while(end < s.length() && s.substring(end, end+1).matches("[0-9]")) { end++; }//create varabile to convert to string with ( 0,end) inta = Integer.parseInt(s.substring(0, end)); if(a > max) {// call findMax(use s.substring(end),a) returnfindMax(s.substring(end), a); }//call again elsereturnfindMax(s.substring(end), max); }//call again elsereturnfindMax(s.substring(1), max); } //input string result //"1d2d3d" 6 //"55" 10 //"xx" 0 //input string result //"12x8" 12 //"012x88" 88 //"012x88ttttt9xe33ppp100" 100 //Testing publicstaticvoid main(String[] args) { String a = "12X8"; String b = "55"; String c = "xx"; String d = "12x8"; String e = "012x88"; String f = "012x88ttttt9xe33ppp100"; System.out.println(sumIt(a)); System.out.println (sumIt(b)); System.out.println (sumIt(c)); System.out.println (findMax(d, 0)); System.out.println (findMax(e,0)); System.out.println (findMax(f,0)); } }
Exception in thread "main" java.lang.NumberFormatException: For input string: "x" for Java code
public class Finder {
//Write two recursive functions, both of which will parse any length string that consists of digits and numbers. Both functions
//should be in the same class and have the following signatures.
//use the if/else statement , Find the base case and -1 till you get to base case
//recursive function that adds up the digits in the String
publicstaticint sumIt(String s)
{
//if String length is less or equal to 1 retrun 1.
if (s.length()<= 1){
return Integer.parseInt(s);
}else{
//use Integer.praseInt(s) to convert string to Integer
//returns the interger values
//else if the CharAt(value in index at 0 = 1) is not equal to the last vaule in the string else {//return the numeric values of a char value + call the SumIt method with a substring = 1
return Character.getNumericValue(s.charAt(0) ) + sumIt(s.substring(1));
}
}
//write a recursion function that will find the largest interger in the string.
publicstaticint findMax(String s,intmax)
{//check the characters of the string is greater than 0
if(s.length() == 0) {
returnmax;
}
if(s.substring(0, 1).matches("[0-9]")) {
intend = 1;
while(end < s.length() && s.substring(end, end+1).matches("[0-9]")) {
end++;
}//create varabile to convert to string with ( 0,end)
inta = Integer.parseInt(s.substring(0, end));
if(a > max) {// call findMax(use s.substring(end),a)
returnfindMax(s.substring(end), a);
}//call again
elsereturnfindMax(s.substring(end), max);
}//call again
elsereturnfindMax(s.substring(1), max);
}
//input string result
//"1d2d3d" 6
//"55" 10
//"xx" 0
//input string result
//"12x8" 12
//"012x88" 88
//"012x88ttttt9xe33ppp100" 100
//Testing
publicstaticvoid main(String[] args) {
String a = "12X8";
String b = "55";
String c = "xx";
String d = "12x8";
String e = "012x88";
String f = "012x88ttttt9xe33ppp100";
System.out.println(sumIt(a));
System.out.println (sumIt(b));
System.out.println (sumIt(c));
System.out.println (findMax(d, 0));
System.out.println (findMax(e,0));
System.out.println (findMax(f,0));
}
}
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 3 images
For the SumIt function - the print statement is suppose to print only the numeric total for the string
input string | result
//"1d2d3d" | 6
//"55" | 10
//"xx" |0
How do I fix the formatting ?