# I am with some exercise of basic java and i wender is my program is well written

**URL:** <https://forum.codewithmosh.com/t/i-am-with-some-exercise-of-basic-java-and-i-wender-is-my-program-is-well-written/27871>\
**Category:** Java\
**Created:** [July 24, 2024, 11:39pm UTC](https://forum.codewithmosh.com/t/i-am-with-some-exercise-of-basic-java-and-i-wender-is-my-program-is-well-written/27871 "2024-07-24T23:39:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amine1](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@Amine1](https://forum.codewithmosh.com/u/Amine1)\
**Post date:** [July 24, 2024, 11:39pm UTC](https://forum.codewithmosh.com/t/i-am-with-some-exercise-of-basic-java-and-i-wender-is-my-program-is-well-written/27871/1 "2024-07-24T23:39:08Z")

</div>

import java.util.Scanner;

public class Main {

```
    public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.print("Enter a number: ");
            int number = scanner.nextInt();
            boolean isprime;
            isprime = true;
            int i = 2;

            if (number <= 1) {
                    System.out.println("The number is not prime");
            } else {
                    while (i <= number / 2 && isprime) {
                            if (number % i == 0) {
                                    isprime = false;
                            }
                            i++;
                    }

                    if (isprime == true)
                            System.out.print("the number is prime number");
                    else
                            System.out.println(("the number is not prime number"));

            }
    }

```

}

---

<div class="post-metadata">

**Author:** ![UniqueNospaceShort](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/uniquenospaceshort/32/1814_2.png) [@UniqueNospaceShort](https://forum.codewithmosh.com/u/UniqueNospaceShort)\
**Post date:** [July 25, 2024, 7:51am UTC](https://forum.codewithmosh.com/t/i-am-with-some-exercise-of-basic-java-and-i-wender-is-my-program-is-well-written/27871/2 "2024-07-25T07:51:12Z")

</div>

> [@Amine1](#):
>
> ```auto
> boolean isprime;
> isprime = true;
> int i = 2;
> 
> ```

What can you tell me about this ?  
How are those initializations different ?

There is something funky here

` System.out.println(("the number is not prime number"));`

That said you begin apparently so the 1st thing that matters is it works.

I did transpose that algo to C# and it works from what I can see.

Cheers.

---

<div class="post-metadata">

**Author:** ![King\_Sardius](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/king_sardius/32/6178_2.png) [@King\_Sardius](https://forum.codewithmosh.com/u/King_Sardius)\
**Post date:** [August 14, 2024, 4:03am UTC](https://forum.codewithmosh.com/t/i-am-with-some-exercise-of-basic-java-and-i-wender-is-my-program-is-well-written/27871/3 "2024-08-14T04:03:07Z")

</div>

Greetings,  
There are several techniques to incorporate here to declutter your code, eliminate unnecessary lines, make it simpler, and avoid bugs. Here are some tips:

1. Use declaration and initialization on the same line. Example `boolean isprime: true;`
2. Declare and initialize variables where and when they will be used. Example: your boolean `isprime` should be inside your first `if` statement. Almost all these elements could be inside a method.
3. Avoid adding `if` statements where your flags and checks can be used at the end of an operation. For example, the last two `print()` operations can be used within your first `if` and `else` statements.
4. Avoid repeating code. The `number is not prime` flag could be used once if you review the logic for checking if a number is prime.
5. Remember the difference between the `==` operator and the `isEqual()` method. One is comparing the location in memory (with some exceptions) and the other is comparing the value stored at the address.
6. Use conventional nomenclature for variables, classes, or methods. It makes your code cleaner and easier to read. For example, your variable `isprime` should be written `isPrime` instead.
7. Java syntax allows simplicity. For example, `isprime = false;` is the same as `!isprime;`. Since you had initialized your variable as `true`.

---

<div class="post-metadata">

**Author:** ![Amine1](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@Amine1](https://forum.codewithmosh.com/u/Amine1)\
**Post date:** [August 15, 2024, 1:53am UTC](https://forum.codewithmosh.com/t/i-am-with-some-exercise-of-basic-java-and-i-wender-is-my-program-is-well-written/27871/4 "2024-08-15T01:53:50Z")

</div>

Thank you so much! I’ve been waiting for someone to critique my code. I know it works, but I’m aware it could be written much better. I’ll make sure to follow your notes.

---

<div class="post-metadata">

**Author:** ![King\_Sardius](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/king_sardius/32/6178_2.png) [@King\_Sardius](https://forum.codewithmosh.com/u/King_Sardius)\
**Post date:** [August 15, 2024, 4:08am UTC](https://forum.codewithmosh.com/t/i-am-with-some-exercise-of-basic-java-and-i-wender-is-my-program-is-well-written/27871/5 "2024-08-15T04:08:38Z")

</div>

I recommend that you watch the amazing YouTube videos on coding paradigms and practices by the Youtubers [Fireship](https://youtu.be/tv-_1er1mWI?si=5B1dxmfm9TgtDKW9), [Coderized](https://youtu.be/HlgG395PQWw?si=XyuInwprWlJyfhX-), and [CodeAesthetic](https://youtu.be/CFRhGnuXG-4?si=MSe0pcD00uOr5LKw).

Click on those names. I linked some of their videos to give you some guidance. Do not be intimidated by the potentially “out of your league” subjects covered. Watch the videos. They will give you a great head start, and point you in the right directions. These three channels are my all time favorites. Fireship give expert and straight to the point tips. CodeAesthetic gives expert step by step beautiful demonstrations. And Coderized gives expert and paced explanations with visuals similar to those of CodeAesthetic. You will love them.

One extremely helpful video from the YouTube channel [Low level Learning](https://youtu.be/GWYhtksrmhE?si=hLyJ7i49pxqSF39g), about how NASA produces error free code.
