java - Using recursion, getting max int out of a string of numbers and letters? -


my class on chapter 18 of intro java book, chapter recursion. says:

"for assignment, write 2 recursive functions, both of parse length string consists of digits , numbers. both functions should in same class , have following signatures."

now first 1 int sumit(string s) sum ints in given string. completed part, , have @ each char , see if number , add total if so. in return, recall function char removed.

the second function int findmax(string s, int max). way think work start max 0 , call function again in return, , each time check if next int bigger max, , replace if so. wrote similar first function(check code), realized, in one, cannot go character character. if string "xg12zz128-p/9" max should 128. there doesn't seem simple way next integer in string. i'm not sure do.

here code:

public class finder {       public int sumit(string s) {          int total = 0;          if(s.length() > 0) {  // checks string still has characters             if(s.substring(0, 1).matches("[0-9]")) {   // checks if first character number                 total += integer.parseint(s.substring(0, 1)); // if num, parses integer , adds total                 return total + sumit(s.substring(1)); // return total , use recursursion continue searching string ints             }             else {                 return total + sumit(s.substring(1));  // needed else clause in case char not number             }         }         else {             return total; // return total when there no more characters         }     }      public int findmax(string s, int max) {          if (s.length() > 0) { // checks characters              if(s.substring(0, 1).matches("[0-9]")) { // checks number                  int = integer.parseint(s.substring(0, 1));     // parse num int                      if(a > max) {                     return findmax(s.substring(1), a);  // if new int bigger, call function again set max                 }                 else return findmax(s.substring(1), max); // else use old max             }             else return findmax(s.substring(1), max); // in case not num         }          else return max; // return max when characters gone.     }  } 

and here junit test file being used. don't need see code @ least can see examples of answers.

package week4.whitelaw;  import static org.junit.assert.*;  import org.junit.test;  public class testrecursion {      @test     public void testsumit()     {         finder finder = new finder();          assertequals(6, finder.sumit("1d2d3d") );         assertequals(10, finder.sumit("55") );         assertequals(0, finder.sumit("xx") );         assertequals(1, finder.sumit("00001") );         assertequals(3, finder.sumit("x0x0w1y2") );         assertequals(21, finder.sumit("123456") );         assertequals(21, finder.sumit("x123456x") );         assertequals(7, finder.sumit("1ggggg60") );     }      @test     public void testmax()     {         finder finder = new finder();          // assume max smallest possible number         int max = 0;          assertequals(12, finder.findmax("12x8",max) );         assertequals(88, finder.findmax("012x88",max) );         assertequals(100, finder.findmax("012x88ttttt9xe33ppp100",max) );         assertequals(128, finder.findmax("128",max) );         assertequals(0, finder.findmax("abcdef",max) );         assertequals(123456, finder.findmax("123456",max) );         assertequals(2, finder.findmax("x2x1x",max) );     }  } 

any appreciated!!

using pseudo code, since question focused on logic rather syntax, here's 1 possible solution,

findmax(string s, int currentmax, int currentint)     if s empty         return currentmax     if s.firstchar() not int         currentmax = max(currentmax, currentint)         return findmax(s.substring(1), currentmax, 0)     int t = parseint(s.firstchar())     currentint = currentint * 10 + t     return findmax(s.substring(1), currentmax, currentint) 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -