Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

Question

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));

 

}

}

 

Expert Solution
Check Mark
Still need help?
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

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 ?

Solution
Bartleby Expert
by Bartleby Expert
SEE SOLUTION
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

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 ?

Solution
Bartleby Expert
by Bartleby Expert
SEE SOLUTION
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education