r/javahelp • u/Grand-Illustrator397 • Mar 20 '24
I hate strings
import java.util.Scanner;
public class Main {
public static void main(String[]args){
Scanner in = new Scanner(System.in);
String s = in.next();
String rest = s;
String result = ""; // empty string?
int i = 0;
while (rest.length() > 0)
{
String first = rest.substring(0,1); //extracts first character of 'rest' string
rest = rest.substring(1); // now it removes the first character?
if (rest.contains(first))
{
result = result + first; // what is being added if 'result' is empty
}
}
System.out.println(result);
}
}any video recomms for this type of use for loops and strings?? I tried using a table chart to understand a little better but it didnt work out for me, and i still dont understand how it works. Help would be appreaciated.
0
Upvotes
1
u/ShoulderPast2433 Mar 23 '24
Its nothing really complicated - all the problems stem from the +1 -1 errors (for example str.substring(1,3) you don't know if 3 is included or nor, or which exactly letter it will be because indexing starts at 0 and we have been counting from 1 for our entire life)
There is nothing we can explain you - you just need to practise, and your brain will learn how it works.