Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Just started today, please help with basic knowledge

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I decided to start to learn to program, and I've already gone through a few tutorials. But, at this point the knowledge hasn't sunk in. I was hoping to get some help in base knowledge. I would appreciate an explanation of what is wrong... not a code fix.

I've given myself the task of writing a program with the following goals:
1. How many days have you been alive?
2. How many hours have you been alive?
3. How many minutes have you been alive?
4. How many seconds have you been alive?
5. What milestones have you passed?
a. 16 - Driver's License
b. 18 - Adult
c. 21 - Alcohol
d. 25 - Cheaper insurance
e. 35 - Can be president.
f. 55 - Senior citizen
g. 65 - Retirement
h. 100 - Centennial

I stole (borrowed?) some code from a website (I would give proper credit to the author, but I lost the website) and have been trying to manipulate it to gain some understanding of how things work in Java.

This is where I'm at:
 
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ryan,

Welcome to JavaRanch

The error is you cannot have assignment statements outside a block (say method block). If you need to assign values to your variables in your class body, you have to specify them during declaration itself.

So below is a valid construct.


If you are new to Java, please start with the Oracle Java tutorials. Also use the JavaRanch Bunkhouse search to find beginner's books.
 
Ranch Hand
Posts: 95
Python C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you really want to assign values to the 3 variables outside then you can do something like this:

Sorry John I realized later that me and you are on the same lines!!!
 
Ryan Wamsat
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both for your fast reply. I can see that this is going to be a helpful forum.
So, I guess it is technically accurate to assign values to the variables at the declaration. Would that be best practice? Where/when should I be assigning these values?
 
Naishadh Parmar
Ranch Hand
Posts: 95
Python C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah Ryan this is a really helpful forum. People here are very experienced and provide you with an accurate answer(I'm not praising myself!!!)

Yes, of course there is no problem in this practice. But, you can also do this-- leave the declaration where it is and assigning variables at the start of the main method
 
Sheriff
Posts: 14759
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Initial values can be assigned when variables are declared, in constructors, or prior to their first use. It depends on your application logic. Instance variables like your month, day, and year variables are assigned default values. For object references, the default is null. For int, the default is 0. Therefore, explicitly assigning 0 to the instance variables as suggested in previous replies is actually redundant.
 
Naishadh Parmar
Ranch Hand
Posts: 95
Python C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yeah Junilu you are right. That thought didn't come to my mind.
 
this llama doesn't want your drama, he just wants this tiny ad for his mama
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!