• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

very basic java help

lockmac

Senior member
Hi guys.. was just wandering if anybody could lend me some quick help on a small java application that I have to compile for an assignment.

I have to copy the following code into a compiler (we use JCreator with the JDK 1.6).. and see what the program does.. however when I do this, i am getting "class, interface, or enum expected" errors on lines 1, 6, 7, 9, 10-13, 15-18. Heres the code....


public static void main(String[] args) {
// Declarations
// Note: If the last 4 digits of your ID begin with 0's
// then drop those. For example: 0035 would become just 35
double birthDay = 20;
double birthMonth = 4;
double studentID = 5246;
//Calculations
birthDay = birthDay * birthMonth * 100
studentID = studentID / birthMonth;
birthDay = 1 + (birthDay % birthMonth);
studentID = studentID - birthDay + (birthMonth * 7);
studentID = studentID + (birthDay * birthMonth);
//Display Results
System.out.println("The value of birthDay =" + birthDay);
System.out.println("The value of birthMonth =" + birthMonth);
System.out.println("The value of studentID =" + studentID);
}



Any body know why?

Also... what does the percentage sign mean in birthDay = 1 + (birthDay % birthMonth); <--- that line of code?

Many thanks guys
 
You need to declare the method inside of a class.

% is modulus - think of the remainder left after division.
 
You need to declare a class around that method.

What do you need to name your class? Lets say it's called "MyClass"

put at very top of your file: public class MyClass {

then close that brace at the very bottom.

the percent signs are mod operations
 
You guys are absolute legends!

Do you think you could explain this modulus to me again? Still really not sure what it means.. perhaps give me an expected value that would come out of this solution?

Many thanks!
 
if you do say, 15%4, the answer would be 3, since 15/4=3 with a remainder of 3. or if you do 15%6, the answer would also be 3, because the remainder is 3.

In school, one of our projects was to make a program that determines if the year is a leap year (not an easy project for a beginner, lemme tell you) and we made extensive use of modulus (moduli?) in that program.
 
Thanks for that mate.. I think I get it now.. What if their is no remainder.. eg... 8000%4... does this just equal 0?

 
Originally posted by: lockmac
Thanks for that mate.. I think I get it now.. What if their is no remainder.. eg... 8000%4... does this just equal 0?

Why not try it and find out? Not trying to be a jerk but isn't that the best way?
 
Originally posted by: BigPete
Originally posted by: lockmac
Thanks for that mate.. I think I get it now.. What if their is no remainder.. eg... 8000%4... does this just equal 0?

Why not try it and find out? Not trying to be a jerk but isn't that the best way?

I did try it out.. and was getting a completely different answer from what was expected.. but its ok now, i worked it out, i had changed the original values but didnt recompile the executable.

Cheers
 
Hi. as you might now, im a bit new to Java and am learning it at Uni at the moment. Just another quick question if anyone could possibly help me out...

I have a class called Applicant, in that class I have a method to read a file and store them in a new object, which works fine. How can i call that method from my main? I can call other methods, but when I try to call this one, it doesnt let me because its a 'non-static mathod which cannot be referenced from a static context', but if i change my method in Applicant to static, then the applicant class comes up with the same errors. Im not really too sure what the static means so if anyone could help me that would really be appreciated

Many thanks
 
If you want to call a non-static method, you need to first create an instance of the class. Using the keyword new, create the object:
Applicant myApplicant = new Applicant();
Then you can call your method on the 'myApplicant' object.
 
Thanks very much mate, that worked a treat.
One final question, how do I get it so when I create an instance for my second person (who is on the second line of the text file), the FileReader will read values from the second line? Eventually, Im going to put this into a loop so it will keep creating a new object for every line of the file (which represents a different person) until the EOF.

Many thanks
 
Attached is some pseducode for the process. You might want to make a new constructor for Applicant which accepts a String value.
 
Thanks mate.. when I try this, i get errors telling me that it cant find symbol constructor BufferReader().
Sorry if this is annoying as Im learning Java at the moment and need this for my assignment. Also.. what is supposed to go in the brackets of the first line where the dots are? new FileReader?

Many thanks
 
Here are two incredibly valuable resources:
Java API Docs
Sun's Java Tutorial

In the former, look up BufferedReader, see what it requires for a constructor (yes, a FileReader), which package it belongs to, etc. Between that and the Basic I/O section of the Java tutorial, you should pick up enough to finish your code.
 
Back
Top