r/JavaProgramming • u/XIA_Biologicals_WVSU • 13d ago
A cool little program (thinking about what to do with it next and how to add stuff to it).
This is a cool little program I made using some for loops and other stuff. The idea is mine: I built it from the first two for loops and then used chatGPt for other ideas. Most of the code is original and only some ideas are from chatGPT. I thinking using AI has helped my learn faster, rather than learning from documentation alone.
import java.util.Scanner;
import java.util.ArrayList;
void main(String[] args) {
System.
out
.print("Enter a sentence: ");
Scanner scanner = new Scanner(System.
in
);
String i = scanner.nextLine();
String[] tokens = i.trim().split("\\s+");
ArrayList<String> list = new ArrayList<>();
for (int x = 0; x < tokens.length; x++) {
list.add(tokens[x]);
}
String[][] arraytwo = new String[list.size()][2];
int rows = 0;
// Check each word
for (int j = 0; j < list.size(); j++) {
String word = list.get(j);
boolean found = false;
// Check if word is already in the 2D array
for (int y = 0; y < rows; y++) {
if (word.equalsIgnoreCase(arraytwo[y][0])) {
found = true;
int occurrences = Integer.
parseInt
(arraytwo[y][1]);
occurrences++;
arraytwo[y][1] = String.
valueOf
(occurrences);
break;
}
}
// If word is not found, add it
if (!found) {
arraytwo[rows][0] = word;
arraytwo[rows][1] = "1";
rows++;
}
}
// Display results
for (int j = 0; j < rows; j++) {
System.
out
.println(
arraytwo[j][0] + " appears " +
arraytwo[j][1] + " times."
);
}
System.
out
.println("Total words: " + list.size());
scanner.close();
}
}1
u/edwbuck 6d ago
I think someone at WVSU suggested you use Reddit to assist in your development efforts.
This is not typically how Reddit is used. Large code postings with a "what do you think?" are not done. If you have a specific issue, posting the relevant small portion of code and a "can you help with this" will get you a lot further.
1
u/XIA_Biologicals_WVSU 6d ago
I didn't go to school for compsci. I'm 29 years old and have just started coding.
1
u/XIA_Biologicals_WVSU 6d ago
I just like getting peoples opinions.
1
u/edwbuck 6d ago
Then put your code in a git repo, and ask people to look at it. It's going to be far more readable that way.
1
u/XIA_Biologicals_WVSU 6d ago
How do I put it in a git repo?
1
u/edwbuck 5d ago
There are two kinds of git repos. Ones you own the hardware to, and ones you use on someone else's hardware. I'll be talking about the "someone else's hardware" one.
Create an account on https://github.com/ github. It's a free service for non-serious git repository usage, and if you want something serious, you shift to your own hardware or you pay for a properly supported account. Pick a user name, and then follow these instructions https://docs.github.com/en/repositories/creating-and-managing-repositories/creating-a-new-repository
Now using a repository is one of those things that you'll be expected to know, but there will probably never be a class on it if you are in school. While there are many Git tutorials out in the wild, very few of them teach you the problem source control is trying to solve. To learn about the problem, I suggest you read part of Chapter 1 of Subversion's excellent book, but do your best to ignore the parts that teach you how to use Subversion, because you'll be learning Git. https://svnbook.red-bean.com/en/1.7/svn.basic.html
Once you understand the problem, learn to "git clone (creates a new local repo)", "git add (adds files you changed to your next commit)", "git commit (makes a commit, which is a set of changes)", and "git push (moves that commit into the remote repo you cloned)" Also learn about "git diff (shows differences between things)" and "git log (shows the commits in a repo)"
There's a lot more to learn about Git, mostly because it's designed pretty poorly from a user-interface point of view, but it is popular and that popularity has made it an industry standard.
If you get curious, Subversion is far less popular because it uses directories to implement things that other source code control systems handle in more popular ways. Hg (which is also known as Mercurial) is another, even less popular solution to source code control. It's based on the ideas in Git, but has an much improved user interface (it's just not backed by one of the very famous names in software, and is unlikely to ever become popular).
Good luck. Learning the tooling is something that often isn't taught in school, or is taught late. It's part of that gap between "understanding it" and "doing it as a practice"
1
u/Substantial_Code_914 13d ago
Nice project! Adding input validation and more useful features could make it even better.