-
IFBB BRO
JAVA brahs here??????????? Need help fixing an error!
Do not understand why this code will not work:
Code:
import java.util.Scanner;
import java.io.*;
public class diffsInDates
{
public static void main(String[] args) throws IOException
{
int m1= 0 , d1= 0 , y1= 0 , m2= 0 , d2= 0, y2= 0 ;
for(int i = 0 ; i < 6 ; i++)// to keep loop active until all data is scanned.
{
m1 = Scan.nextInt(); // integers that are scanned in.
d1 = Scan.nextInt();
y1 = Scan.nextInt();
m2 = Scan.nextInt();
d2 = Scan.nextInt();
y2 = Scan.nextInt();
System.out.println("Total number of days between dates entered are: " + x(m1, m2, d1, d2, y1, y2) );
}
}
public static int x (int m1, int m2, int d1, int d2, int y1, int y2)
{
int result = 0;
int result1 = 0;
int result2 = 0;
if( m1 == m2 && y1 == y2)
{ // counts days if year and month are the same.
result = d2 - d1 +1;
}
else if ( y1 == y2 )
{
result = daysInMonth( m2 , y2 ) - d1 + 1;
}
for(int i = m1; i < m2 ; i++) // counts months
{
result1 += daysInMonth( m2 , y2 );
}
for (int i = y1; i < y2; i++)
{ // counts years
result2 += daysInYear( y2 );
}
result += result1 + result2;
return result;
}
//methods
public static boolean isLeap(int year) // to find if it is a leap year
{
if( year % 400 == 0)
return true;
else if( year % 4 == 0 && year % 100 != 0)
return true;
else return false;
}
public static int daysInMonth( int month , int year) // to find days in the month
{
int leapMonth;
if (isLeap(year)== true)
{
leapMonth = 29;
}
else
{
leapMonth = 28;
}
switch(month)
{
case 4:
case 6:
case 9:
case 11: return 30;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: return 31;
case 2: return leapMonth;
}
return 28;
}
public static int daysInYear(int year) // to find the days in the year, leap or not.
{
if( isLeap(year)){
return 366;
}
else{
return 365;
}
}
}
Its giving me:
----jGRASP exec: javac -g diffsInDates.java
diffsInDates.java:15: error: cannot find symbol
m1 = Scan.nextInt(); // integers that are scanned in.
^
symbol: variable Scan
location: class diffsInDates
diffsInDates.java:16: error: cannot find symbol
d1 = Scan.nextInt();
^
symbol: variable Scan
location: class diffsInDates
diffsInDates.java:17: error: cannot find symbol
y1 = Scan.nextInt();
^
symbol: variable Scan
location: class diffsInDates
diffsInDates.java:18: error: cannot find symbol
m2 = Scan.nextInt();
^
symbol: variable Scan
location: class diffsInDates
diffsInDates.java:19: error: cannot find symbol
d2 = Scan.nextInt();
^
symbol: variable Scan
location: class diffsInDates
diffsInDates.java:20: error: cannot find symbol
y2 = Scan.nextInt();
^
symbol: variable Scan
location: class diffsInDates
6 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
LOOKING FOR A NEW COMPANY TO REPRESENT
***Greek/Ancient Egyptian Genetics***
**Huge Arms Crew**
ISSA Personal Trainer
LIKE MY PAGE BRAHS: FACEBOOK.COM/GETHUGEBRAH
Atlanta, GA
-
IFBB BRO
If anyone is wondering what the problem im trying to solve, its:
Write a program to accept any two dates in the form of month, day, and year (8 23 2000), separated by spaces, and calculate the total number of days that has elapsed between the two dates, inclusive of the beginning and ending days. Remember that leap year is a year divisible by 4, except for centennial years, which must be divisible by 400, such as 1600 or 2000 (1800 is not a leap year). You can test your program with any typed in dates but it must finally run with the data shown below. System output on the screen is acceptable.
DATA: ANSWERS
7 4 1776 1 1 1987 (76882)
2 1 1983 3 3 1984 (397)
4 7 1983 11 4 1983 (???)
7 3 1983 7 20 1983 (18)
12 29 1990 12 31 1992 (???)
your birthdate to due date (?)
1. Consider using the long data type for dayCounter
2. The program must continue running until all the data is processed – use a loop 1 to 6 or a while (month != -1) or while (!fin.eof()) - choose your own end of data technique.
3. Use if statements, switch statements, and loops to solve this problem. Do not use the Gregorian date method!
LOOKING FOR A NEW COMPANY TO REPRESENT
***Greek/Ancient Egyptian Genetics***
**Huge Arms Crew**
ISSA Personal Trainer
LIKE MY PAGE BRAHS: FACEBOOK.COM/GETHUGEBRAH
Atlanta, GA
-
Registered User
-
Protein Hacker
Because if it were easy, I wouldn't be interested.
God has a better plan for my life, than I do, for my own.
Go, weave your snares with logic and design. The speed of my flight will take your breath away. - Mirza Ghalib
=== 228 Crew ===
-
IFBB BRO
Originally Posted by LAballin24
Originally Posted by mehdi84
Not sure what you guys mean, should I put int i = scan.nextInt(); up top?
About to read my Java book for a couple of hrs, hopefully I can understand this sht
LOOKING FOR A NEW COMPANY TO REPRESENT
***Greek/Ancient Egyptian Genetics***
**Huge Arms Crew**
ISSA Personal Trainer
LIKE MY PAGE BRAHS: FACEBOOK.COM/GETHUGEBRAH
Atlanta, GA
-
Registered User
Scanner Scan = new Scanner( System.in );
you need to put that up top (probably under where you defined your integers) in order to initialize the scanner and accept user input
-
IFBB BRO
Originally Posted by jorts150
Scanner Scan = new Scanner( System.in );
you need to put that up top (probably under where you defined your integers) in order to initialize the scanner and accept user input
Thanks, this sht is incredibly hard IMO for a beginner, currently reading my book which honestly I am very behind on the reading.
LOOKING FOR A NEW COMPANY TO REPRESENT
***Greek/Ancient Egyptian Genetics***
**Huge Arms Crew**
ISSA Personal Trainer
LIKE MY PAGE BRAHS: FACEBOOK.COM/GETHUGEBRAH
Atlanta, GA
-
Registered User
Decent.
Only thing I would say about that is don't have so many returns and else returns for you methods. Have one. For boolean make a boolean variable call it leapYear or whatever then have leapYear = false or leapYear = true and then have it return leapYear. It doesn't change the program, but it's a good habit for good coding. Do this for any method.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Bookmarks