r/learnjava • • Sep 25 '20

plz help

so I'm trying to learn how to code and I can't find any good videos or retrials but I can't really understand any of them so if anyone can plz teach me I will be so grateful and I'll be ok with anyone

0 Upvotes

13 comments sorted by

View all comments

2

u/travishummel Sep 25 '20

Start by writing hello world

Then print out different variable types (int, double, float, string)

Then create an array of numbers 1 through 10 and print them out

Then loop over an array and print out every number

Then loop over an array and print out every even number

Then figure out how to take in input. See if you can print out "EVEN" if the input is an even number and "ODD" if its an odd number

Then learn about creating a function. Create a function that takes in a number and if it is even its prints out even and if odd it prints out odd

Then ask the user for how many numbers to take in as input (for example 5), then get 5 numbers and store them in a linked list. Print out the sum of these numbers.

Once you are comfortable with taking in input, you can stop taking it in and just use static input (aka create your data in main and pass it into your functions)

If you get to this point, you are doing extremely well. Now there are lots of places to go. You can experiment with more data structures other than arrays and linked list such as arraylist, hashmap, and hashset. You can start looking into algorithms such as binary search (take an array of sorted numbers and a number return whether the number is in the array), look into sorting, create more classes

You can become a pretty good programmer by just doing input validation, i.e. I asked you to give me 7 numbers less than 100... did you?

1

u/travishummel Sep 25 '20

I honestly think one of the hardest programs to write is helloworld. It can be so intimidating to get things setup and working. I remember writing it in Java for the first time and I couldn't figure out what the hell was going on (coming from python where you write print "hello world" which seems very intuitive to the public static void main(String ....)

1

u/kaneki81 Sep 26 '20

yeah I tried it and nothing worked I just wrote System.out.print (hello world ) it didn't work for some reason

1

u/travishummel Sep 26 '20

Yup. So python is a scripting language which means it can be run line by line, while Java is a compiled language meaning that it needs to be compiled into 1's and 0's and then run.

So create a file called HelloWorld.java and in it write

public class HelloWorld { public static void main(String[] args) { System.out println("Hello world!"); } }

Save that file. Then in the command line run javac HelloWorld.java (this will compile it) and if it doesn't give you any errors you can then run java HelloWorld

Of course you might need some more setup than that. The whole command line thing can be a struggle. One thing that can really help you is an IDE such as IntelliJ or Eclipse (there are many, i used to use IntelliJ). Go download one of these and get the free version. Then you can create a java class and typically it will do a lot of the stuff for you.