The question is to find the number of days between two dates.example-input-26/3/2000 and 12/8/2014.the output will be the no of days in between these two dates.
There is an error saying "identifier expected" and i=1 is highlighted.Also I am not sure whether the program is completely correct.
import java.util.*;
class yearst
{
int a[]={0,31,28,31,30,31,30,31,30,31,30,31,30};
int i,s,s1,s2,s3,k,diy,m,m1,m2,d1,d2,y1,y2,y;
i=1;s1=0;s2=0;s3=0;diy=365;
void leap(int y)
{
if(y%4==0 && y%100!=0 || y%400==0) //for leap year
{
a[2]=29;
diy=366;
}
else
{
a[2]=28;
diy=365;
}
}
public static void main(String args[])
{
Scanner ob=new Scanner(System.in);
System.out.println("Enter the months,dates and years");
m1=ob.nextInt();
m2=ob.nextInt();
d1=ob.nextInt();
d2=ob.nextInt();
y1=ob.nextInt();
y2=ob.nextInt();
for(i=y1;i<y2;i++)
{
ob.leap(i+1)
m=1*diy;
s1=s1+m;
}
for(i=1;i<m1;i++)//no of days left in y1
{
ob.leap(y1);
s2+=a[i];
}
s2+=d1;
k=diy-s2;
for(i=1;i<m2;i++)//no of days passed
{
ob.leap(y2);
s3+=a[i];
}
s3+=d2;
s=s1+s2+s3;
System.out.println("No of days in between"+s)
}
}
Please help.
Your program is a bunch of errors. First, you are calling class variables in main method without declaring them static or initializing them in constructor. Second, you are calling leap() which is method of a class from object of Scanner. It is not possible. The program will never compile nor run this way. I have modified your code to make it compilable and runnable. But one thing is for sure. Its logic is incorrect. It is giving wrong output as you want to calculate number of days between two dates. That is your job. I removed its errors. Now it is running. Here you are :-
import java.util.*;
class yearst
{
static int a[]={0,31,28,31,30,31,30,31,30,31,30,31,30};
static int i=1,s,s1=0,s2=0,s3=0,k,diy=365,m,m1,m2,d1,d2,y1,y2,y;
static void leap(int y)
{
if(y%4==0 && y%100!=0 || y%400==0) //for leap year
{
a[2]=29;
diy=366;
}
else
{
a[2]=28;
diy=365;
}
}
public static void main(String args[])
{
//i=1;s1=0;s2=0;s3=0;diy=365;
Scanner ob=new Scanner(System.in);
System.out.println("Enter the months,dates and years");
m1=ob.nextInt();
m2=ob.nextInt();
d1=ob.nextInt();
d2=ob.nextInt();
y1=ob.nextInt();
y2=ob.nextInt();
for(i=y1;i<y2;i++)
{
leap(i+1);
m=1*diy;
s1=s1+m;
}
for(i=1;i<m1;i++)//no of days left in y1
{
leap(y1);
s2+=a[i];
}
s2+=d1;
k=diy-s2;
for(i=1;i<m2;i++)//no of days passed
{
leap(y2);
s3+=a[i];
}
s3+=d2;
s=s1+s2+s3;
System.out.println("No of days in between"+s);
}
}
All the Best :)
Only declarations and static blocks allowed out of the methods. The below executable statement must be either in static block or in constructor
int i=1,s1=0,s2=0,s3=0,diy=365;
So, I recommend you move above code to constructor.
yearst(){
i=1;s1=0;s2=0;s3=0;diy=365;
}
A few things:
You'll need to initialize your variables inside a constructor, as initializing inside a class isn't allowed
Have you checked out the Date class in Java? It might be more useful for this case.
According to convention, class names should start with a capital letter
Related
I've done a bunch research into trying to solve this issue (for about 2.5 hours), but I'm still not able to compile my program. I have tried making the method not static, but when attempting to run it, it gives me this error:
"Error: Main method is not static in class prog6, please define the
main method as: public static void main(String[] args)"
When the main method is static, I get following error in a compiler
Error: "non-static variable input cannot be referenced from a static
context
usd = input.nextDouble();"
I'm sorry if this question comes off redundant, I don't mean to ask without looking for an answer on my own, but I've been working at this for hours now and I don't understand what I'm doing wrong.
Some extra info on this program: it's meant to take inputs from the user to find out what currency they want to convert to, and how much USD they would like to convert. Then, I would invoke a method in order to do the calculations and return them. (Any amount trying to be converted over $200, will need 5% fee.)
import java.util.Scanner;
public class prog6
{
Scanner input = new Scanner(System.in);
public static void main (String[] args)
{
char curr = 0;
double usd;
double result;
while (curr!='Q' || curr!='q') { //loop
System.out.println("What type of currency would you like to buy?");
curr = input.next().charAt(0);
System.out.println("How many dollars would you like to convert?");
usd = input.nextDouble(); //asking user for info needed to convert
if (usd>200) {
usd = (usd)*(0.95);
}
result = calc (curr,usd); //invoke the method
}
}
public double calc (char mCurr,double mUsd) //method
{
if (mCurr=='E' || mCurr=='e') {
return (mUsd)*(0.88);
}
else if (mCurr=='P' || mCurr=='p') {
return (mUsd)*(0.77);
}
else if (mCurr=='Y' || mCurr=='y') {
return (mUsd)*(113.17);
}
return 0;
}
}
The main method will need to be static. From there, create an instance of your class and call a non-static method from the static main method. eg..
public class Prog6 {
private Scanner input = new Scanner(System.in);
public static void main (String[] args) {
Prog6 prog6 = new Prog6();
prog6.start();
}
public void start() {
char curr = 0;
double usd;
double result;
// etc...
}
}
You could make the member variable static but it's better form to use regular non-static members and methods and call this from the static main method.
There are two ways to solve your prolem
Change the input variable to static;
or
In main method, prog6 myprog= new prog6(); and refer input as myprog.input ....
This question already has answers here:
Error: Main method not found in class Calculate, please define the main method as: public static void main(String[] args) [duplicate]
(5 answers)
Closed 6 years ago.
I'm getting this error
Main method not found in class Bank, please define the main method as:
public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application
Here is my Bank class
public class Bank { //Bank Class to calculate the value for infrastructure of each Bank
final int N = 9; // Equal to highest number in my CQU Student ID 12029103
int cost;
public int costPerBank(int numberOfComputers) {
// Calculate the total cost for numberOfComputers entered by the user
if (numberOfComputers == 0) { // When user enters '0' or negative values
cost=0;
}
if (numberOfComputers <= 2 && numberOfComputers >= 1) { // When user enters '1' or '2'
cost=1000;
}
if (numberOfComputers > 2 && numberOfComputers<=20) { // When user enters '3' to '20'
cost=1000+((numberOfComputers-2)*400);
}
if (numberOfComputers > 20 && numberOfComputers<=100) { // When user enters '21' to '100'
cost=1000+((numberOfComputers-2)*300);
}
if (numberOfComputers > 100) { // When user enters more than '100'
cost=numberOfComputers*200;
}
return cost;
}
}
A Java program needs to have a main method, essentially the starting point of the program. I suggest looking through some Java tutorials to get started, as they'll teach you stuff like this. Tutorialpoints is a good site.
Change the first few lines to:
public class Bank { //Bank Class to calculate the value for infrastructure of each Bank
final int N = 9; // Equal to highest number in my CQU Student ID 12029103
int cost;
public static void main(String[] args) {
(new Bank()).costPerBank(5);
}
(It creates a new Bank object, then calls the method on that.)
Simply defining a class won't do anything. It's like handing the computer tools (Methods), but not telling it to use (call) the tools.
public static void main(String[] args){
Bank bank = new Bank();
bank.costPerBank(1);
}
Put something like this in your class. This is the first method that is called when you run the program
I am new in encapsulation.
I coded a simple java program that can identify if it is given an odd or even number and I tried to use encapsulation with it. I know that encapsulation uses get and set method, but I made it this way.
Is it still considered as encapsulation?
Main
import java.util.Scanner;
public class Implement
{
public static void main(String[]args)
{
Scanner inp = new Scanner(System.in);
System.out.println("Please enter number: ");
int ent = inp.nextInt();
Practice pract = new Practice();
pract.oddeven(ent);
}
}
Practice class
public class Practice
{
public void oddeven(int a)
{
do
{
if(a>=2)
{
a-=2;
}
}
while(a>1);
if(a==1)
{
System.out.println("This number is Odd.");
}
else if(a==0)
{
System.out.println("This number is Even.");
}
}
}
Is it still considered as encapsulation?
Your class has no internal state, so there is no internal state to hide. This means that "encapsulation" (which is all about hiding internal state) is entirely moot.
I should also point out that:
Your oddeven method is buggy. It won't print anything for numbers less then zero.
That is a horribly inefficient approach to testing for odd or even.
A method that performs a calculation and prints a result to standard output is less useful, flexible and reusable than a method that does the same calculation and returns it.
There are a number of Java style violations in your code:
You are violating the rules for Java identifiers. The method name oddeven should be oddEven because "oddeven" isn't an English word.
Your indentation is all messed up.
Your use of white-space between tokens is wrong.
Encapsulation applies at object level and is specific to Method's and fields. You just have a method which acts upon an input parameter and has nothing to do with encapsulation.
Oracle's definition to data encapsulation is : "Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation — a fundamental principle of object-oriented programming."
For it to be Encapsulated in the method you have to hide whats happening. Thats the true essence of Encapsulation concept. This is done by using two methods:
Getter : gets the value for the main class
Setter : set the value inside the Encapsulation class (Here private variables are used).
The following code will show a modification of yours using encapsulation:
public class implement
{
public static void main(String[]args)
{
Practice pract = new Practice();
Scanner inp = new Scanner(System.in);
System.out.println("Please enter number: ");
int ent = inp.nextInt();
pract.setNum(ent);
if(pract.getNum() == 1){
System.out.println("This number is Odd");
}else if (pract.getNum() == 0){
System.out.println("This is Even");
}
}
}
practise class:
public class Practice
{
private int x;
public int getNum(){
return x;
}
public void setNum(int a){
x = a;
do{if(x >= 2)
x -=2;
}while(x>1);
}
}
I'm trying to do a input dialog, if the input number is bigger than 10 it should print a special message. So I inserted the return, but then I get this error: unexpected return value.
Here is the code:
import javax.swing.JOptionPane;
public class Program extends JFrame {
public static void main(String[] args) throws IOException {
int number= Integer.parseInt(JOptionPane.showInputDialog("Enter a number:"));
if(number> 10)
return System.out.println("The number must be less that 10");
else
System.out.println("...");
}
}
Sorry for my bad English.
What you should do is print the error message on the error stream, and exit with an error code, like so:
public class Program extends JFrame {
public static void main(String[] args) throws IOException {
int number= Integer.parseInt(JOptionPane.showInputDialog("Enter a number:"));
if(number> 10) {
System.err.println("The number must be less that 10");
System.exit(1);
} else {
System.out.println("...");
}
}
}
You have wrongly understood the return concept.
Since the main method's return type is void there will be no return.
if(number> 10)
System.out.println("The number must be less that 10");
else
System.out.println("...");
You are just printing to console. So there is no need of that return statement.
As a side note use always {} for if -else
There are two core errors here:
main is a void method so you can't return anything from it
System.out.println is a void method so you couldn't return it anyway
Drop the return bit completely and just print the result you want instead, or make it a separate String method and return the result instead of printing it. If you want your program to terminate after a faulty input, just add a simple lone return:
if(number> 10) {
System.out.println("The number must be less that 10");
return;
}
else //..and so on
Here you are trying to return a value from a void method(i.e. public static void main(String[] args), which is completely invalid.
I would sugest you to consider other other class completely for this thing and then call the methods from some other class consisting the main method.
the main method is void, it can not return anything, nor is it supposed to.
just remove the "return" keyword, and keep the print statement.
I'm just messing around with the few things I have learnt and I decided to try and make a math test. The test asks you the question and then gives you an entry box to answer it in.
My problem is when trying to compile I get the error "illegal start of type" for the if statement. Thank you.
import java.util.Scanner;
class mathtest {
public static void main(String[] args);{
Scanner x = new Scanner(System. in );
System.out.println("What is 5 + 5 ?");
double y = x.nextDouble();
}
if (y == 10) {
System.out.println("Correct");
} else
System.out.println("Incorrect");
}
}
Here's the problem:
public static void main(String[] args);{ // remove the ;
Delete the ; between main and {.
class mathtest {
public static void main(String[] args){
...
}
}
Shouldn't it be x.nextDouble()?
don't use ; remove it
public static void main(String[] args)
{
}
It should be x.nextDouble();, should it not?
And your problem lies in the main method. Remove the semicolon, so it looks like this:
public static void main(String[] args) {
There are quite a few issues with the code .
No ; is required after declaring a method.
There is no variable a declared anywhere in the code
A variable declared inside a method is only visible inside the scope of the method. You have declared y inside main and used it outside .
there is an extra } at the end.