0

I have a String 9899100101103104105 i want to add consecutive numbers i.e 98+99+100...So i am able to cut the numbers from the String and add upto 100.But after that i am sure how to write the logic.I am using String subString() method and i have seen that (0,2),(2,4),(4,7),(7,10),(10,13),(13,16) should be the substring start and end digits.After (4,7) i am not able to proceeed, here is my logic

public class MissingNumberInString {

public static void main(String[] args) {

    String numbr = "9899100101103104105";
    firstTwo(numbr);
 }

public static void firstTwo(String str) {

    int sum =0;
    int  num = 0;
    for(int i=0;i<str.length()-1;i=i+2){

        if(sum <=99){
        sum += Integer.parseInt(str.substring(i, i+2));
        }else{

        sum += Integer.parseInt(str.substring(i, i+3)); 
        }
        System.out.println(sum);
    }

   }
   }

in else part after (4,7) i am not able to proceed ,please help

6
  • 2
    Might I ask why you are doing this? Is this an assignment and you have to do it? Because otherwise I see no reason why you would concatenate numbers to a string and then try to split/parse those numbers back to Integers again. Commented Oct 18, 2017 at 10:19
  • is it feasible to change the input (i.e. putting separator chars into the string or using an array of numbers)? Commented Oct 18, 2017 at 10:19
  • @Rhayene no input can not be changed Commented Oct 18, 2017 at 10:21
  • You need to increment i based on the sum < = 99 too, you can't increment by 2 after 99... Commented Oct 18, 2017 at 10:21
  • 2
    And who defines that string? Why is it 98 99 103 104 and not 9 89 910 3104? You have a lot of implicit assumptions backed into your questions. You should first step back and really clarify such subtle details. Not only for us, but also for yourself. Commented Oct 18, 2017 at 10:25

1 Answer 1

1

Your incrementation failed based on the last value. So keeping your logic :

for(int i=0;i<str.length()-1;i=i+2){
    if(sum <=99){
    sum += Integer.parseInt(str.substring(i, i+2));
    }else{

    sum += Integer.parseInt(str.substring(i, i+3)); 
    }
    System.out.println(sum);
}

You should incremente by 3 when the sum > 99. Do that in the condition block

for(int i=0;i<str.length()-1; /* removed incrementation */){
    if(sum <=99){
        sum += Integer.parseInt(str.substring(i, i+2));
        i=i+2;
    }else{
        sum += Integer.parseInt(str.substring(i, i+3)); 
        i=i+3;
    }
    System.out.println(sum);
}

But honestly, your logic won't be easy to maintain. How do you know that the first value is not 9 and then you should only substring by 1 character first ?

Sign up to request clarification or add additional context in comments.

1 Comment

@AxeIH thanks for the fast response your solution worked for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.