r/javahelp • • 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

21 comments sorted by

View all comments

2

u/DelayLucky Mar 21 '24

Sorry for going off topic. Is it what Reddit do not formatting code indentations well? I haven't posted any code yet so no idea if this is a platform thing. But if easy, strongly recommend to format the code so it's easier to read.

Nvm if Reddit is just like that.

1

u/chet714 Mar 21 '24

Not off topic at all. This sub's sidebar addresses it and links to a guide but many don't take the time to read it. I've seen worse, at least OP used a code block. :D