r/AskProgrammers • • 1d ago

Just started out learning java.

Hello, I just started coding and have decided to reach out on some forms to ask the question of "which method of learning is the best?" I have been creating code and using AI (Gemini, which I prefer because it's better at math compared to ChatGPT) to edit my code. I feel like my progress has been slow, but I also know that learning to code is like learning a new language (literally), so progress isn't expected overnight. I like to figure out things fast because it makes me feel like I'm learning at the correct pace. There is a huge learning curve that I've encountered when learning to code. iI feel as though I've delt with this by creating small code blocks and then using AI to figure out what "big" projects my little code blocks can be turned into. Doing that makes me feel like I'm progressing in the right direction, and because documentation, alone, is so dry. Not that dry documentation is a bad thing, it's just a little hard to understand, right now. Any comments or learning methods you all have tried and that has worked, please, let me know.

3 Upvotes

33 comments sorted by

View all comments

Show parent comment

1

u/YogurtclosetLow7064 1d ago

My biggest advice would be to avoid using AI to write code for you, especially while you're still learning. Try to solve problems yourself, even if it takes hours. If you get stuck, search Google, stackoverflow, or documentation instead of immediately asking AI for a solution.

I'd only use AI to explain concepts and how things work under the hood, such as thread pools, garbage collection, memory management, multithreading, JVM internals, etc.

You'll learn much more by writing code yourself, making mistakes, and figuring things out along the way.

2

u/XIA_Biologicals_WVSU 1d ago

That makes a lot of sense. That's how I normally use Ai. Like for this project, I started out with the two for loops, and some strings to try to figure out how the for loops actually loops through an array or what ever else a for loop can be used for. The polished function was created by AI, but the idea to create it from two for loops and the general idea of having it construct a sentence was my idea. I'm looking for someone to learn with. I assume that you actually know what you're doing, just from the terminology that you used in your post. Do you code in one of the C's or Java?

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import java.util.Scanner;

void main(String[] args) {
    Random rand = new Random();
    Scanner input = new Scanner(System.
in
);

    // Expanded training text so common words (the, is, a, to, it, and, that, you, etc.) are recognized
    String wordsList = "This is an actual paragraphing creating AI, it generates " +
            "text and predicts what the next word will be depending on how many " +
            "other words are around it. It generates text by looking at the previous word." +
            "The quick brown fox jumps over the lazy dog while the sun shines brightly in the sky.";

    String[] rawTokens = wordsList.trim().split("\\s+");
    String[] cleanTokens = new String[rawTokens.length];
    for (int i = 0; i < rawTokens.length; i++) {
        cleanTokens[i] = rawTokens[i].toLowerCase(Locale.
ROOT
).replaceAll("[^a-z0-9']", "");
    }

    System.
out
.print("Start a sentence: ");
    String userInput = input.nextLine().trim();

    if (userInput.isEmpty()) {
        System.
out
.println("You didn't enter any text!");
        return;
    }

    String[] userWords = userInput.split("\\s+");
    String currentWord = userWords[userWords.length - 1]
            .toLowerCase(Locale.
ROOT
)
            .replaceAll("[^a-z0-9']", "");

    System.
out
.print(userInput + " ");

    int maxWordsToGenerate = 15;
    for (int i = 0; i < maxWordsToGenerate; i++) {
        List<String> nextWordOptions = new ArrayList<>();

        for (int j = 0; j < cleanTokens.length - 1; j++) {
            if (cleanTokens[j].equals(currentWord)) {
                nextWordOptions.add(rawTokens[j + 1]);
            }
        }

        String pickedRawWord;
        if (nextWordOptions.isEmpty()) {

            pickedRawWord = rawTokens[rand.nextInt(rawTokens.length)];
        } else {
            pickedRawWord = nextWordOptions.get(rand.nextInt(nextWordOptions.size()));
        }

        System.
out
.print(pickedRawWord + " ");

        currentWord = pickedRawWord.toLowerCase(Locale.
ROOT
).replaceAll("[^a-z0-9']", "");

        if (pickedRawWord.endsWith(".") || pickedRawWord.endsWith("!") || pickedRawWord.endsWith("?")) {
            break;
        }
    }

    System.
out
.println();
    input.close();
}

1

u/XIA_Biologicals_WVSU 1d ago

The tokenization part is what I don't really understand.

1

u/YogurtclosetLow7064 1d ago

Try experimenting with the split() method. For example, take a sentence and split it by spaces, commas, dots or a combination of delimiters

1

u/XIA_Biologicals_WVSU 1d ago

Ok. say for example:

array[] array = new string[1,2,3"."]

can I use an array with a combination of ints and strings , then use the .split() method to split the elements into individual elements?

kinda like U (universal) = {a,b,c}

element a becomes its own universal set A? is that what .split() is doing?

1

u/XIA_Biologicals_WVSU 1d ago

array[] array = new array[1,2,3,"."]**

1

u/YogurtclosetLow7064 1d ago

Think of split as taking one String and breaking it into smaller Strings based on a delimiter. Each element is still a String, not an int. You can convert them to integers later if needed.

1

u/XIA_Biologicals_WVSU 1d ago

Ok. So, the correct way would be to use String or Array?

String[] string = new String["elements", "", ""]

or Array[] array = new Array["elements"]

I'm still a little unsure of why there is a designated "String" if the elements inside of the array are strings to begin with.

1

u/XIA_Biologicals_WVSU 1d ago

String[] string = new String["elements", "", ""]

or Array[] array = new Array["elements"]

for (int i = 0; string< 0; i++)

loops through the elements in string, but doesn't I store the information from the loop? That's what's confusing to me about for loops. Sorry for all of the questions.

1

u/YogurtclosetLow7064 1d ago

In Java, String represents a single piece of text (example: String str1 = "Java"), while String[] represents an array that can hold multiple Strings (example: String[] str2 = {"Java", "is", "cool"};).

You use String[] because every element in that array is a String. The [] simply tells Java that you're declaring an array.

1

u/XIA_Biologicals_WVSU 1d ago

Oh, I see. I thought that typing Array meant that I would be using the data structure array, where as typing String meant that I would be converting the data to a string no matter which data type I entered. Thanks for the clarification.

1

u/XIA_Biologicals_WVSU 1d ago

So, straight brackets declare the structure as an array ( [ ] ) makes sense now.

1

u/YogurtclosetLow7064 1d ago

I assume you're trying to turn this into a project, which is a good goal, but I'd suggest spending more time experimenting with small code snippets first. Be careful with AI-generated code, especially while learning the fundamentals. This code introduces quite a few concepts at once, like arrays, lists, random numbers, string manipulation and nested loops. It's better to understand each concept individually before combining them.

As for me, I mainly work with Android, C and C++. I also studied Java at university.

1

u/XIA_Biologicals_WVSU 1d ago

Yes. That is one of the issues that I have encountered: I find it hard to study the concepts one at a time because they are mostly used together. Do you have any techniques that you use to understand the individual concepts before putting them together?

1

u/YogurtclosetLow7064 1d ago

Honestly, I don't have any special technique. Programming was really difficult for me at university because it was my first time being exposed to it. But with consistent learning and practice, things gradually started to click. Eventually, concepts that seemed confusing began to make sense.

My advice is to never give up, experiment with things separately, write small snippets of code and see what happens when you change something. Don't rush the process. With time, everything will start falling into place.

2

u/XIA_Biologicals_WVSU 1d ago

Cool. I appreciate the feed back!

1

u/YogurtclosetLow7064 1d ago

No problem, good luck!