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

4

u/[deleted] Mar 21 '24 edited Mar 21 '24

What exactly is this code supposed to do? Pretty tough to read. Also .substring() is a doozy of a method of get used to since the first index is included, and the second is excluded. (I think. 15+ years of Java. I still read the documentation lol)

String is just a built-in, super convienant array of 'char'. They're actually pretty heavy on your computer. Try adding an array of integers to another, like ""+"" and you'll notice how nice this is for the concatenation of strings.

StringBuilder is the best way to simplify the concatenation. If you ever get around to decompile your compiled .class files, you'll see that the compiler actually converts all the ""+"" to the StringBuilder form. The compiler does a lot of optimizations like this for you.

^ important to note that inside a loop, it'll make a new StringBuilder each iteration. So declare it above the loop. Will make your loops MUCH faster.

1

u/Grand-Illustrator397 Mar 21 '24

I have solved it now, but I feel guilty because i used GPT to help me, but here is what the program was supposed to do..
The loop below is intended to collect all characters that occur more than once in a given string. When you try it out with a string such as "Mississippi", you will note that letters that occur three or more times are collected more than once. Modify the loop so that each repeated letter is collected exactly once.
For example, when s is "Mississippi", result should be "isp" and not "issisip"

2

u/[deleted] Mar 21 '24

For something simple like this, GPT is okay. It should have printed out good comments for it. If you go into Bing's search bar with Co-Pilot (powered by GPT 4), it should be able to print out a decent visualization of what it's doing in each iteration, too. Just make sure you understand what's going on in each loop iteration. Using String.toCharArray() would help a lot, too. This way, you're just iterating through an array like normal. No reliance on .substring(...) or anything. Your teacher may of wanted you to use substring() though. Idk.

You're in Java, so don't be afraid to name your variables with pretty large names that describe exactly what they are. result->collectedRepeatedChars. remain-> remainingUnparsedChars. You can name them whatever you like, but naming variables with detail can make your code much easier to figure out. It'll save you from having to comment as much, too. Some people love comments... I don't. To each their own.

You're gonna be mad when in about a week or so, your teacher shows you what 'Set's are. Pretty much 'List's, but it can only have unique items; no repeats. They would have solved half your problem for you. It's good to be able to hardcode these things yourself, though, and not be completely reliant on pre-made libraries that Java has. Don't be afraid to go into some source code and learn from it, though. Learn, not copy; sometimes it's the same thing, though.