/*
*Find if a year is leap or not
*/
public class LeapYear{
private static int leapYear;
public void setLeapYear(int leapYear){
this.leapYear = leapYear;
}// end method
public static void main (String[] args) {
LeapYear leap = new LeapYear();
leap.setLeapYear(2010);
leap.setLeapYear(2008);
leap.setLeapYear(1900);
leap.setLeapYear(2000);
leap.setLeapYear(1565);
// Is it Divisible by 4?
if (leapYear % 4 == 0) {
// Is it Divisible by 4 but not 100?
if (leapYear % 100 != 0) {
System.out.println(leapYear + ": is a leap year.");
}
// Is it Divisible by 4 and 100 and 400?
else if (leapYear % 400 == 0) {
System.out.println(leapYear + ": is a leap year.");
}
// It is Divisible by 4 and 100 but not 400!
else {
System.out.println(leapYear + ": is not a leap year.");
}
}
// It is not divisible by 4.
else {
System.out.println(leapYear + ": is not a leap year.");
}
}
}
I am new to Java and I wrote this code so that it would call for all five years into the boolean and generate answers for all of them. However it only calls the last one. How would I do this right?
Problem 1
The four first calls to setLeapYear:
leap.setLeapYear(2010); // leap.leapYear = 2010;
leap.setLeapYear(2008); // leap.leapYear = 2008;
leap.setLeapYear(1900); // leap.leapYear = 1900;
leap.setLeapYear(2000); // leap.leapYear = 2000;
gets overridden by the last one:
leap.setLeapYear(1565); // leap.leapYear = 1565;
I would probably write a boolean method called something like isLeapYear(int year) and do
System.out.println(isLeapYear(2010));
System.out.println(isLeapYear(2008));
...
Problem 2
leapYear is static, so you don't need to / shouldn't do LeapYear leap = new LeapYear(); (alternatively, you should drop the static modifier).
You need use separate object for each year or at least call the Leap Year checking method as soon as you crated the object for that year.
What you have is a series of call to a function that assigns a value to an attribute of the same object. Therefore, only the last statement has the effect as previous values are overwritten.
On additional note, your code doesn't seem to be properly organized. Whey do you make the checkings in Main and it seems that leapYear isn't defined anywhere.
Perhaps, you may want to define a function that returns true/false depending on the value of the passed parameter or the value of the year stored in object.
The code may look something like this:
leap.setLeapYear(2010); // leap.leapYear = 2010;
System.out.println(leap.isLeapYear());
leap.setLeapYear(2008); // leap.leapYear = 2008;
System.out.println(leap.isLeapYear());
leap.setLeapYear(1900); // leap.leapYear = 1900;
System.out.println(leap.isLeapYear());
leap.setLeapYear(2000);
System.out.println(leap.isLeapYear());
leap.setLeapYear(1565);
System.out.println(leap.isLeapYear());
You have to define isLeapYear() by moving the checks in main to that function.
I would be inclined to write the tests in your if statements in order from 400 to 4:
String result;
if (year % 400 == 0) {
result = "is a leap year.";
} else if (year % 100 == 0) {
result = "is not a leap year.";
} else if (year % 4 == 0) {
result = "is a leap year.";
} else {
result = "is not a leap year.";
}
System.out.println(year + ": " + result);
Related
so i have a problem validating the last section of this code where i have commented
public void newTicket() throws Exception {
boolean validation = false;
boolean phoneUnique = false;
int num;
int member;
String memberPhoneNum;
final int memTicket = 80;
final int nonMem = 100;
int min = 100;
int max = 200;
int random_int = (int) Math.floor(Math.random() * (max - min + 1) + min);
int dd;
int mm;
int yy;
String ticketDate;
boolean isTrueDate = true;
System.out.println();
System.out.println("Transaction Id: " + random_int);
int transactionId = random_int;
do {
System.out.print("Enter Day: ");
dd = scan.nextInt();
System.out.print("Enter Month");
mm = scan.nextInt();
System.out.print("Enter Year");
yy = scan.nextInt();
ticketDate = (dd + "" + mm + "" + yy);
System.out.println("Ticket date: " + ticketDate);
validation = v.checkDate(ticketDate);
if (!isTrueDate) {
System.out.println("Invalid"); // PROBLEM WITH VALIDATION
}
} while (!isTrueDate);
Whenever i enter a wrong date, for example: 300224, it still proceeds to the next line. ive tried changing the true/false statements but it still does work how i want it to.
public static boolean checkDate(String ticketDate) {
boolean isTrueDate = true;
int day = 0;
int month = 0;
int year = 0;
if (month > 12) {
isTrueDate = false;
} else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
if (day <= 31) {
isTrueDate = true;
} else if (day >= 31) {
isTrueDate = false;
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
if (day <= 30) {
isTrueDate = true;
} else if (day >= 30) {
isTrueDate = false;
}
} else if (month == 2) // February check
{
if (year % 4 == 0) // Leap year check for February
{
if (day <= 29) {
isTrueDate = true;
} else if (day >= 29) {
isTrueDate = false;
}
} else if (year % 4 != 0) {
if (day <= 28) {
isTrueDate = true;
} else if (day >= 28) {
isTrueDate = false;
}
}
}
return isTrueDate;
This is my validation class to test for leap years and whatnot
I appreciate any help given
I am new to this site too, but I can try to give you a response that may help.
This is going to be a long one, as I have a lot of potential issues to solve. I will provide something that I think could work at the end though.
The first thing I noticed is that your long series of conditionals involves a lot of brackets and you may have missed one as you nested many of them into each other. I did a thorough check and really, the only one I did not see was the one that should end the boolean method after return isTrueDate; so I thought I should mention it just in case. To be clear, you probably have it, but make sure you have a bracket that closes the method. Also in the future, make sure to put the whole code snippet for clarity. Thanks in advance.
Now the second thing I see is the way you got the validation. I do not know exactly what you mean when you say validation = v.checkDate(ticketDate); in the first code snippet because I do not know what v is, although I will assume it is the main class. In this case, while I am not sure, it looks like you are trying to run your method and assign the boolean value of whether the date is a real date to the value of validation. This makes sense, but what I think you should change is how you used isTrueDate to verify by asking if the value is not isTrueDate. I think it might solve an issue if you simply put an invalid response on validation being false and vice versa for validation being true. Here's an example:
validation = v.checkDate(ticketDate);
if (validation == true) {
//Something here if you want.
//However you may not need this space.
} else {
System.out.println("invalid")
}
However, with this, there are really two approaches that I can think of: the one that preserves security and the one that ensures your code is working.
So the code above is better at preserving security b/c it accounts for anything that may not make the validation false in the else statement in addition to it being false because your code worked properly. For example, if your series of conditionals has a flaw in it, b/c you initialized validation as being false, if something goes wrong, its likely to just invalidate the ticket, which could be semi-useful in a security sense to avoid some vulnerabilities. However, you can do that later. The first thing you need to make sure of is that your code works properly, so I would also recommend that when you declare validate at the top of your first code snippet, to not initialize it.
Like this:
boolean validation;
Instead of this:
boolean validation = false;
This ensures that your series of conditionals is responsible for the value of validation rather than the conditions simply not working and the value being false by default.
This brings me to another thing that I believe would also cause problems in your code.
It refers to this section from your first code snippet:
boolean isTrueDate = true;
System.out.println();
System.out.println("Transaction Id: " + random_int);
int transactionId = random_int;
So two things:
The easy one refers to the bottom two lines. It's not a mistake causing you errors, but I would suggest switching the order of the bottom two lines to assign transactionId to the value of random_int and then printing:
System.out.println("Transaction Id: " + transactionId);
Now the second one is the real problem. In the code above you are assigning true to isTrueDate.
This is a problem because you are later using isTrueDate in that series of conditionals in that other boolean method called checkDate()
This will mess up your code. Here is how:
So first the isTrueDate you used inside your method, checkDate() is kept local to that method because you declared it inside there. If you did not do this, you likely would have gotten a separate error because you declared a boolean isTrueDate in two separate places. You declared it at the top in that first code snippet, and you declared it in that method. Normally, this would cause an error because you have already declared it once and changing the value would only require you to assign isTrueDate to something else with an equal sign rather than declaring it again. To really show you what I mean, look at this example:
Incorrect:
boolean isTrueDate = true;
//Other actions
//I want to change the value of isTrueDate
boolean isTrueDate = false;
Correct:
boolean isTrueDate = true;
//Other actions
//I want to change the value of isTrueDate
isTrueDate = false;
What you did (doesn't return error but messes up your code):
I put it in a different order to better show the problem.
public static boolean checkDate(String ticketDate) {
boolean isTrueDate = true; //first declaration
//Lots of conditions
}
static void someMethod() {
//This method may not be exactly how you did it,
//but you did not show me that.
boolean isTrueDate = true; //usually invalid second declaration.
//However, it still works b/c they are in different methods and
// thus kept separate from each other. This still messes up your
// code though
//more code
validation = v.checkDate(ticketDate);
if (!isTrueDate) {
System.out.println("Invalid");
}
//Doesn't work b/c the value of isTrueDate you initialized
// in someMethod() is the one that this if else statement checks
// instead of the one that you used in checkDate()
//That means, isTrueDate will always be true.
//To solve this, you must first use validation to present the
// invalid message as discussed above, but furthermore, you should
// delete the initialization of isTrueDate in the method that is
// not checkDate() because you don't need it there.
}
Sorry, that's a lot to explain, and there may be more to fix, but I have one more correction for now: I looked through the conditionals that you used to account for all the ways a date could be correct or incorrect and I think its pretty good for the most part, but I can offer one thing that will save you some lines.
Every time you check if a date is within the max number of days for that month, you then put a separate else if condition that accounts for the other of the two possibilities. Here's an example from your code:
if (day <= 31) {
isTrueDate = true;
} else if (day >= 31) {
isTrueDate = false;
}
This does help to show exactly what is happening, but I would still advise deleting this else if part and replacing it with one big else {} part at the end because it would say the same thing as all of these other separate else if blocks, just much more concisely. Here is what that would look like:
//This new list of conditions will work such that it finds every true
// possibility and denies everything else.
if (month < 12) //Changed to fit new strategy
{
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
if (day <= 31)
{
isTrueDate = true;
}
} else if (month == 4 || month == 6 || month == 9 || month == 11)
{
if (day <= 30)
{
isTrueDate = true;
}
} else if (month == 2) // February check
{
if (year % 4 == 0) // Leap year check for February
{
if (day <= 29)
{
isTrueDate = true;
}
} else if (year % 4 != 0)
{
if (day <= 28)
{
isTrueDate = true;
}
}
}
} else { // All the times it was false
isTrueDate = false;
}
return isTrueDate;
So there you have it. As thorough as I could because in truth, this is my first answered question and I wanted to do it right. I hope it helps in some way. The main thing is to make sure that the data you received from the scanner correctly enters into the method and that the right data from that method goes to your validation. I'll leave you with everything I can think to correct on both snippets.
New Snippet 1:
public void newTicket() throws Exception {
boolean validation;
boolean phoneUnique = false;
int num;
int member;
String memberPhoneNum;
final int memTicket = 80;
final int nonMem = 100;
int min = 100;
int max = 200;
int random_int = (int) Math.floor(Math.random() * (max - min + 1) + min);
int dd;
int mm;
int yy;
String ticketDate;
//Removed unnecessary declaration of isTrueDate in this part
System.out.println(); //I don't know why you have this but I will leave it alone.
int transactionId = random_int; //Switched
System.out.println("Transaction Id: " + transactionId);
do { // I like this part
System.out.print("Enter Day: ");
dd = scan.nextInt();
System.out.print("Enter Month");
mm = scan.nextInt();
System.out.print("Enter Year");
yy = scan.nextInt();
ticketDate = (dd + "" + mm + "" + yy);
System.out.println("Ticket date: " + ticketDate);
validation = v.checkDate(dd, mm, yy); //This change is explained in snippet 2
if (validation == false) { //uses validation instead of isTrueDate
System.out.println("Invalid"); // Hopefully this helps
}
} while (!isTrueDate); //I don't know what happens here so I won't touch.
//Code that was not shown
New Snippet 2:
//parameters changed
public static boolean checkDate(int day, int month, int year) {
boolean isTrueDate; //Changed out of preference
//int day = 0;
//int month = 0; No longer needed in this new code.
//int year = 0;
//One more problem:
//It looks like you tried to use ticketDate as the parameter
// to get the values for days, month, and year, but this
// doesn't quite work b/c there is not anything that takes that
// string and then extracts the values for day, month and year.
//The way I chose to address this was by placing the day, month,
// and year as parameters in the checkDate() method, and then
// calling checkDate() in snippet one with the values of the
// scanner.
//However, there are other ways to do this. For example, you could
// keep your ticketDate String and then access different parts of
// that String to get the values of day, month, and year.
//In the end, I just did what seemed easiest to me.
//This new list of conditions will work such that it finds every true
// possibility and denies everything else.
if (month < 12) //Changed to fit new strategy
{
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
if (day <= 31)
{
isTrueDate = true;
}
} else if (month == 4 || month == 6 || month == 9 || month == 11)
{
if (day <= 30)
{
isTrueDate = true;
}
} else if (month == 2) // February check
{
if (year % 4 == 0) // Leap year check for February
{
if (day <= 29)
{
isTrueDate = true;
}
} else if (year % 4 != 0)
{
if (day <= 28)
{
isTrueDate = true;
}
}
}
} else { // All the times it was false
isTrueDate = false;
}
return isTrueDate;
} //The extra bracket! Closes the method
Peace <3
I wrote a simple program that is supposed to display whether if a user inputted int is a leap year or not, and if so what leap year is it.
During running of the program whenever a number that was not supposed to be a leap year was inputted it did not print the else statement.
Note: This was written in the IDE BlueJ so io was automatically imported hence why I did not import it
/**
* Reads a user inputted integer value and determines if it is a leap year
* Created by Oh boy I suck at this
* 9 September 2019
*/
import java.util.Scanner;
public class LeapYear
{
public static int getYear(String prompt)
{
String newLine = System.getProperty("line.separator");
int value = 0;
boolean flag = true;
while (flag)
{
Scanner scan = new Scanner(System.in);
System.out.println(prompt + ": ");
try
{
value = scan.nextInt();
flag = false;
}
catch(java.util.InputMismatchException e)
{
System.out.println("What you have inputed was not an int.");
System.out.println(newLine);
}
}
return value;
}
public static void main (String[] args)
{
int year = getYear("Input the year ");
final int leapYear = year/4;
if(year % 4 == 0){
if(year % 100 >= 1){
if(year % 400 >= 1){
System.out.println("The year inputted: " + year + " is equivilant to " + leapYear + " leap year(s).");
}
else{
System.out.println("The year inputted: " + year + " is not a leap year.");
}
}
}
}
}
Let us look at your if statements, and remember the rules for leap years.
Understanding leap years
In general, a leap year is a year that is divisible by four, i.e. where the statement year % 4 == 0 is true.
But: If a year is not a leap year, when it is divisible by 100. I.e. year % 100 == 0 is true.
Exception: When that is also divisible by 400, i.e. year % 400 == 0 is true - then we have a leap year.
Basically: divisible by four yields a candidate - you then have to study it further to make a final decision.
Decision tree
Is it a leap year candidate? year % 4 == 0 Otherwise: Display "not a leap year"
Is it a leap year exception (every 100 years)? year % 100 == 0 Otherwise: Display "not a leap year"
Is it a leap year exception exception (every 400 years)? year % 400 == 0 True => Display "leap year", False => Display "not a leap year"
Your implementation
Let's first look at your if statement.
Your first if statement checks whether the year is divisible by four. This is so that you know whether you deal with a leap year candidate. So this is correct - but you forgot to deal with the case, when the year is NOT a leap year (so here you miss a possible "is not a leap year" output)
Now it gets a bit confusing. You check whether the year is NOT divisible by 100. If a leap year candidate is NOT divisible by 100 it IS a leap year. So you can output success, but you have to deal with the "else" case.
The third if is nested in the wrong if block. It belongs into the else block of the parent.
Hints
Try to get an understanding on how the input and the output relate to each other and try to hit every if / else.
Whenever you write an if think about whether you need the corresponding else block.
If things get weird, try to "trace" your program with "breadcrumb outputs": System.out.println("1"); System.out.println("2"); ... on every line where you deal with branching or looping (if,else,switch,while...) this will trace every step the program does on the command line. You can use this until your school encourages you to use a proper IDE.
Don't forget: Practice makes perfect ;)
Pseude code (spoiler)
Only use this if you're completely stuck.
if (year % 4 == 0) {
// this might be a leap year - we need to dig further
if (year % 100 == 0) {
if (year % 400 == 0) {
print "leap year"
} else {
print "not a leap year"
}
} else {
print "leap year"
}
} else {
print "not a leap year"
}
Bottom line
Some of your logic is incorrect and you forget to implement the else branches.
HTH
I have a small problem. I figured out how to solve this program, but I can't get the output to output ALL the leap years in a table format. Instead, the output shoots out the leap years one at a time, which is something I don't want with my program.
What could be the problem?
EDIT:
I'm trying to get all the years to pop out using a "JOptionPane" message box (JOptionPane.showMessageDialog(null, " ");), but I keep getting only one output per box and many boxes... compared to all the outputs in just one box.
Here is the code:
String enterYear = JOptionPane.showInputDialog(null, "Enter the starting year: \nExample: 2015"); // User enters an input (Year)
String enterLastYear = JOptionPane.showInputDialog(null, "Enter the ending year: ");
int i = Integer.parseInt(enterYear);
if (Integer.parseInt(enterYear) < Integer.parseInt(enterLastYear)){
for (i = Integer.parseInt(enterYear); i < Integer.parseInt(enterLastYear); i += 4){
if(i % 400 == 0 || i % 4 == 0) {
JOptionPane.showMessageDialog(null, i + "\n");
}
}
} else {
JOptionPane.showMessageDialog(null, "Error: Starting Year is greater than Ending Year!");
}
}
}
You Simply need to build the message before passing it to the JOptionPane.
Sample Code:
public class LeapYearTest {
public static void main(String[] args) {
LeapYearTest leapYearTest = new LeapYearTest();
leapYearTest.showLeapYears(2000, 2020);
leapYearTest.showLeapYears(2000, 2000);
}
private void showLeapYears(int start, int end) {
StringBuffer msg = new StringBuffer();
if (start < end) {
msg.append("<html><table><tr><td><b>Leap Years</b></td></tr>");
for (int i = start; i < end; i++) {
if(i % 400 == 0 || i % 4 == 0) {
msg.append("<tr><td>" + i + "</td></tr>");
}
}
msg.append("</table></html>");
} else {
msg.append("Error: Starting Year is greater than Ending Year!");
}
JOptionPane.showMessageDialog(null, msg.toString());
}
}
Outputs:
I think you should replace i += 4 with i++. Also correct formula for leap year is
year % 4 == 0 && year % 100 != 0 || year % 400 == 0
I guess you can apply this logic
public void calculateLeapYears(int fromYear,int tillYear){
int count=0;
int firstLeapYear;
if(fromYear% 4==0)
firstLeapYear=fromYear;
else
firstLeapYear=fromYear+(4-fromYear%4);
for(int i=firstLeapYear;i<=tillYear;i+=4)
count++;
System.out.println(count);
}
I am still learning Java and am stuck and getting errors. Can someone help with a solution of how the best way to do this would be.
import java.util.Scanner;
public class LeapYear2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int year;
year = scan.nextInt();
boolean boolVar1,boolVar2,boolVar3;
boolVar1 = (year / 4);
boolVar2 = (year / 100);
boolVar3 = (year / 400);
if (boolVar1 == true && boolVar2 == true && boolVar3 == true)
{
System.out.println("This is a leap year.");
}
else if (boolVar1 == true && boolVar2 == true && boolVar3 != true)
{
System.out.println("This is not a leap year.");
}
else if (boolVar1 == true)
{
System.out.println("This is a leap year.");
}
else
{
System.out.println("This is not a leap year.");
}
}
}
If you want to know whether an int value is evenly divisible by another, you can do this:
boolVar1 = year % 4 == 0;
boolVar2 = year % 100 == 0;
boolVar3 = year % 400 == 0;
In Java, integer and boolean values are not interchangeable (unlike languages like C where 0 is false and any non-zero value is considered true). You need to use a comparison operator like ==. The % operator is the remainder function, which (from the rest of your code) looks like what you want to be using.
P.S. In the future, when you are asking about how to deal with errors, it would be very helpful if you posted the error message(s) in your question.
P.P.S. You should get in the habit of giving your variables meaningful names. For example, I would replace boolVar1 with divisibleBy4, etc. This will save you lots of headaches when you start writing more complex code.
Also, as others have pointed out, with a boolean variable b, recommended Java style is to use
if (...b...)
instead of
if (...b == true...)
and
if (...!b...)
instead of
if (...b == false...)
You need an explicit comparison to make an expression boolean. You do not need an explicit comparison with true on booleans, so the code should be as follows:
boolean boolVar1,boolVar2,boolVar3;
boolVar1 = (year % 4) == 0;
boolVar2 = (year % 100) == 0;
boolVar3 = (year % 400) == 0;
if (boolVar1 && boolVar2 && boolVar3)
{
System.out.println("This is a leap year.");
}
else if (boolVar1 && boolVar2 && !boolVar3)
{
System.out.println("This is not a leap year.");
}
else if (boolVar1)
{
System.out.println("This is a leap year.");
}
else
{
System.out.println("This is not a leap year.");
}
Note that boolVar != true and boolVar == false is equivalent to !boolVar.
use the modulo method for this.
if(year % 4 == 0){
System.out.println("This is a leap year.");
}
and so on. From what I can see you do not need to declare 3 separate int types for this. Just reuse this in each conditional statement.
You do not need all those booleans. You can simply test:
if ((year % 4 == 0) && (year % 100 != 0)){
System.out.println(year + " is a leap year.");
}
else if (year % 400 == 0){
System.out.println(year + " is a leap year.");
}
else {
System.out.println(year + " is not a leap year.");
}
rather than calculating so many conditions just put the following code
if(year%4==0)
System.out.println("The given year is leap year");
else
System.out.println("The given year is not leap year");
There is no situation where you stuck up between int value and boolean.
Use this code
boolVar1 = (year % 4) == 0;
boolVar2 = (year % 100) ==0;
boolVar3 = (year % 400) == 0;
Note that the original version would have worked in C.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
For some reason I get a syntax error that says, "Syntax error, insert "}" to complete ClassBody." I checked every method, every statement to make sure I have complete opening and closing brackets, so I don't know why this is happening. Can anybody tell me why I might be getting this issue?
Copying the code into another file doesn't fix the problem, nor does going to Project > Clean.
import java.util.Scanner;
public class jloneman_Numerology
{
private String[] report;
private int day, month, year, num;
public jloneman_Numerology()
{
introduction();
report = new String[9];
num = 0;
}
public void introduction()
{
System.out.println("Welcome to ACME Numerology Reports! We will " +
"determine your special\nnumerology report based on your " +
"birth date.\n");
}
public void getDate()
{
char slash1, slash2;
do
{
System.out.print("Please enter your birth date (mm / dd / yyyy): ");
Scanner in = new Scanner(System.in);
String date = in.nextLine();
month = in.nextInt();
day = in.nextInt();
year = in.nextInt();
slash1 = date.charAt(3);
slash2 = date.charAt(8);
} while (validDate(slash1, slash2) == false);
calcNum();
}
public boolean validDate(char slash1, char slash2)
{
boolean isValid = true;
// Check for valid month
if (month < 1 || month > 12)
{
isValid = false;
System.out.printf("Invalid month: %d\n", month);
}
// Check for valid day
if (day < 1 || day > 31)
{
isValid = false;
System.out.printf("Invalid day: %d\n", day);
}
// Check for months with 30 days, else 31 days = invalid
if ((month == 4 || month == 6 || month == 9 || month == 11) && (day < 1 || day > 30))
{
isValid = false;
System.out.printf("Invalid day: %d\n", day);
}
else if (day < 1 || day > 31)
{
isValid = false;
System.out.printf("Invalid day: %d\n", day);
}
// Check for valid year
if (year < 1880 || year > 2280)
{
isValid = false;
System.out.println("Please enter a valid year between 1880 and 2280.");
}
// Check for correct separating character
if (slash1 != '/' || slash2 != '/')
{
isValid = false;
System.out.println("Invalid separating character, please use forward slashes");
}
if (leapYear() == true)
{
if (month == 2 && day > 29)
{
isValid = false;
System.out.printf("Invalid day for 2/%d: %d", year, day);
}
}
return isValid;
}
public boolean leapYear()
{
boolean isLeap;
if (year % 4 == 0 && year % 400 != 0)
isLeap = false;
else
isLeap = true;
return isLeap;
}
public void calcNum()
{
// Separate each digit of the date and add to a single number
// Test number for debugging
num = 5;
}
public void printReport()
{
report[0] = ":1: ";
report[1] = ":2: ";
report[2] = ":3: ";
report[3] = ":4: ";
report[4] = ":5: ";
report[5] = ":6: ";
report[6] = ":7: ";
report[7] = ":8: ";
report[8] = ":9: ";
System.out.println(report[num]);
}
}
78,0-1 Bot
Try removing (or commenting out) one method and see if the problem persists. If it does, remove or comment out an additional method, and so on, until the error disappears. Then restore everything but the last method.
If the error doesn't reappear, the problem is probably in that last method.
If it does reappear, the problem is more subtle; perhaps a control character embedded in the code. Try copying and pasting the code into a text-only editor (so any control characters will be ignored, save it, and recompiling.
I too had this error.
There are NO errors in the code above (when I used the code above as as a sub class).
I corrected my error by cleaning up my IDE's workspace and making sure the caller was working properly.
Project settings
Run settings
Made sure that main was listed properly
public static void main(String[] args) { ... }
I have faced the similar problem, solved it by deleting the class file and then recreated the file again and pasted the same code. It worked.
Take out the System.out.println and put it into the main function instead of the introduction() inside of the main(String[] args).