Help with a java assignment?
Hi, my names Robert and I'm an introductory Java student.
I have an assignment where the program receives an array of integers, runs them all through a formula, and then displays the results of each instance of the formula in a table. For the moment, I'm just trying to get the actual equation working, I can focus on the table once that's done. After a few attempts with no success, I decided to try running the program with a method. It looks like it will work but theres an error where the IF statement begins, telling me it cannot call a non-static method cannot be referenced from a static context. And that makes sense, I just don't know what I should do to fix it. Here's the code:
import java.util.Scanner;
public class Salary {
public int aSalary(int salary) {
if (salary <= 0){
return 0;
} else {
salary = (sales * 0.09) + 200;
}
}
public static void main(String[] args) {
Scanner input = new Scanner ( System.in );
System.out.print( "Please enter the amount of items sold by the employees:");
String sales = input.nextLine();
String salesArray[] = sales.split(" ");
for(int i=0; i<salesArray.length; i++){
System.out.print(salesArray[i] + " ");
if(aSalary(Integer.parseInt(salesArray[i…
System.out.println("is even");
else
System.out.println("not even");
}
}
}
I would greatly appreciate any help or advice. Thank you
Re: Help with a java assignment?
Quote:
theres an error where the IF statement begins, telling me it cannot call a non-static method cannot be referenced from a static context.
When you get errors, please copy and paste here the full text. The message includes the line number and the statement with the error.
Please copy full text of error message and paste it here. Here is a sample:
Code :
TestSorts.java:138: cannot find symbol
symbol : variable var
location: class TestSorts
var = 2;
^
Quote:
non-static method cannot be referenced from a static context
This means that in a static method you are trying to reference a method that will only exist in an instance of the class. To get to that method you need to create an instance of the class and use that to call the method.
AClass aCls = new AClass(); // create an instance of the class
aCls.theMethod(); // use the reference to AClass to call theMethod()
Or you can make the method static, the same as the main method.
Re: Help with a java assignment?
Noted and thank you for the tip. Here's the error message:
Line 22: non-static method aSalary(int) cannot be referenced from a static context
incompatible types
required: boolean
found: int
Edit: Whenever I save the project, it suddenly says theres another error on Line 5, saying that its missing a return statement.
Re: Help with a java assignment?
Did you read the end of my last post?
What is this about:
Quote:
incompatible types
required: boolean
found: int
Re: Help with a java assignment?
Thats the error I get from NetBeans for the error on line 22 of the code. Sorry if thats not the one that would help. I'll transcribe the one from the command prompt too if it helps.
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types
required: boolean
found: int
at Salary.main <Salary.main:22>
Re: Help with a java assignment?
Problems:
0. When you post code, please use the "CODE" tags.
1. You make a reference to a variable outside of your current scope.
Quote:
Code Java:
public int aSalary(int salary) {
if (salary <= 0){
return 0;
} else {
salary = (sales * 0.09) + 200; //<----- sales is not in scope
}
2. Your class does not contain a constructor, which is okay if the class is static and has only static members and methods.
Quote:
Code Java:
public class Salary { //add static keyword
public int aSalary(int salary) { //add static keyword
if (salary <= 0){
return 0;
} else {
salary = (sales * 0.09) + 200; //<----- sales is not in scope
}
}
3. Your main method does not create a new instance of class Salary if not static and doesn't reference Salary if it is supposed to be static.
Quote:
Code Java:
public static void main(String[] args) {
Scanner input = new Scanner ( System.in );
// Salary salary = new Salary(); //not present, not needed if static
System.out.print( "Please enter the amount of items sold by the employees:");
String sales = input.nextLine();
String salesArray[] = sales.split(" ");
for(int i=0; i<salesArray.length; i++){
System.out.print(salesArray[i] + " ");
if(aSalary(Integer.parseInt(salesArray[i… // your code, change to
//if(Salary.aSalary(Integer.parseInt(salesArray[i…
System.out.println("is even");
else
System.out.println("not even");
}
}
Re: Help with a java assignment?
Made the method static, corrected two of the variable references in the method as well. However I still get the same error as before on line 22, which is the IF statement. It insists that what is being called for is a boolean value, but instead its getting an integer. Also still getting the error on line 5 (the method declaration), which insists there's no return statement. Here's the updated code:
import java.util.Scanner;
public class Salary {
public static int aSalary(int sales) { //line 5, error: missing return statement
double salary;
if (sales <= 0){
salary = 0;
} else {
salary = (sales * 0.09) + 200;
}
}
public static void main(String[] args) {
Scanner input = new Scanner ( System.in );
System.out.print( "Please enter the amount of items sold by the employees:");
String sales = input.nextLine();
String salesArray[] = sales.split(" ");
for(int i=0; i<salesArray.length; i++){ //Line 22, error: incompatible types, required: boolean, found: int
System.out.print(salesArray[i] + " ");
if(aSalary(Integer.parseInt(salesArray[i])))
System.out.println("is even");
else
System.out.println("not even");
}
}
}
Re: Help with a java assignment?
What statement is at line 22?
The compiler sees that you have coded something where it wants there to be a boolean value, but you have coded something that has an int value.
Re: Help with a java assignment?
You don't have a return statement.
Quote:
Code Java:
public static int aSalary(int sales) { //line 5, error: missing return statement
double salary;
if (sales <= 0){
salary = 0;
} else {
salary = (sales * 0.09) + 200;
}
Change to:
Code Java:
public static int aSalary(int sales) {
if (sales <= 0){
return 0;
} else {
return (int)((double)(sales) * 0.09 + 200); // java does not implicitly cast int to double in expressions, it truncates
}
Addition:
Code Java:
if(aSalary(Integer.parseInt(salesArray[i]))) // this is not a boolean expression
Salary.aSalary() returns an integer, you cannot use it as a boolean
I think you forgot the modulus...
Code Java:
if( aSalary( Integer.parseInt( salesArray[i] ) )%2==0 )