r/learnjava • u/kadenhicklin • Feb 08 '21
How Do I assign a variable in java?!
I have used python for around 2 years and for a college class we have started to dabble with Java and I can basically only print things in it. All I want to do is assign a variable but whenever I do var name = 1; or something along those lines it immediately throws and error. I have tried pasting in code from google and none of it works. But once I delete it all and go back to the code that prints stuff it works just fine. I am literally about to punch a hole in my computer or rip my hair out since I am on a time crunch.
Thanks,
Kaden
Edit:
Question 4:
Write a program to calculate then display the total amount of cents for the following
3 dollars
2 quarters
5 dimes
10 cents
This is all I want to do, piece of cake right?
Sorry for the temper I'm just really mad that I cant find any answers on the internet and my professor is no help and didn't even teach this material first.
2
u/itsJustLana Feb 08 '21
String aString = "myString"; System.out.println(aString); etc.
Also, watch out for your variable names, some may be keywords and/or reserved.
1
u/kadenhickin Feb 08 '21
Even if I only type in x = 1; or int var x=1; it says command execution failed, then if I delete it the rest of the code runs fine, I cannot for the life of me figure out what I am doing wrong :(
1
1
u/kadenhickin Feb 08 '21
The String aStronf = “myString”; works tho just not anything with an integer
1
u/TicklesMcFancy Feb 08 '21
You need to declare what it is.
int val = 3;
String stringy = 'hello';
Boolean carb = true;I might be over simplifying it and missing something, but I'm kind of new to Java as well.
1
u/kadenhickin Feb 08 '21
Even if I only type in x = 1; or int var x=1; it says command execution failed, then if I delete it the rest of the code runs fine, I cannot for the life of me figure out what I am doing wrong :(
1
u/TicklesMcFancy Feb 08 '21
int x = 1
Instead of
int var x = 11
u/kadenhickin Feb 08 '21
I got it! Thanks man
1
u/TicklesMcFancy Feb 08 '21
I came from python as well.
Everything needs a constructor statement.
Object obj = new Object(); Or Value value = xyz;
1
u/MatthiasDunkel Feb 08 '21
Python is a dynamic programming language. That means you don't have to declare the type of the variable (boolean, int double, etc). In Java, you always need to tell Java what type an object is. So if you want to assign a new int variable the code would look like this:
int variable = 5;
Keep this in mind. For more information about that read this: Difference between dynamic and static programming
I hope this helps.
1
u/Bad_brazilian Feb 08 '21
If the answers up to this point don't solve your problem, care to share your code?
1
u/feral_claire Feb 09 '21
var name = 1; works in recent Java versions. What version are you using? It might be older. Otherwise int name = 1; works in all versions.
Could you share your code and the exact error message? Its hard to say exactly what the problem is without it.
7
u/desrtfx Feb 09 '21
Show your full code.
In java, everything must be encompassed in a class and executable code must me in methods inside the class.
Stop lying. Just read the sidebar here -> Free Tutorials and do the MOOC linked there.