Click here to Skip to main content
14,925,639 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
import java.util.*;

public class string_difference {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        String str = scan.next();
        StringBuilder s = new StringBuilder(str);
        
        for(int i = 1; i < s.length(); i++){
            int diff = s.charAt(i) - s.charAt(i-1);
            System.out.println(diff);
            s.insert(i, diff);
        }
        System.out.println(s);
    }
}


What I have tried:

loop stops when i comment out the s.insert() line but would go on forever if uncommenetd
Posted
Updated 8-May-21 0:38am

Why do you think?

Lets see if it makes sense in the real world:
You have five coins of mixed denomination in a row.
You look at the first and second coins, subtract one from the other and get a new value. You find a coin of that value in your pocket and you insert it in between the first and second coins.
How many coins are in the row?
Six. So you're not at the end.
You look at the second and third coins, subtract one from the other and get a new value. You find a coin of that value in your pocket and you insert it in between the second and third coins.
How many coins are in the row?
Seven. So you're not at the end.
...
Can you ever reach the final coin?

Of course you can't - the length of the row increases each time you compare two values, so you can never get to the end of string!

What I'd suggest doing is re-reading your assignment carefully: are you sure that you should be comparing previous results at all?

To be honest, a quick minute with the debugger would have shown you exactly what was going on, and saved you the time writing the question and waiting for an answer.
How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Get used to doing this: you are going to need it more and more as you move on in your course, so it's well worth developing the skill on tiny bits of code like this, because it will save you hours of head scratching as your tasks get more and more complex!
   
Comments
Yogi Panda 8-May-21 19:34pm
   
thanks bud, for solution and more so for the advice.
OriginalGriff 9-May-21 2:05am
   
You're welcome!
Quote:
Why wont the forloop stop in java when using stringbuilder

Be cause you loop until the end of s and you continuously append something to s in the loop.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

jdb - The Java Debugger[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   
Comments
Yogi Panda 8-May-21 19:36pm
   
thanks bud :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900