Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Help with a java assignment?

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with a java assignment?

    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:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^

    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.
    Last edited by Norm; September 30th, 2011 at 12:17 PM.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    Last edited by rberry719; September 30th, 2011 at 12:28 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with a java assignment?

    Did you read the end of my last post?

    What is this about:
    incompatible types
    required: boolean
    found: int

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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>

  6. #6

    Default 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.
    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.
    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.
    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&#8230;                        // your code, change to
     
    //if(Salary.aSalary(Integer.parseInt(salesArray[i…
     
    System.out.println("is even");
    else
    System.out.println("not even");
    }
     
    }
    Last edited by kenster421; September 30th, 2011 at 01:57 PM. Reason: Syntax Highlighting
    Kenneth Walter
    Software Developer
    http://kennywalter.com

  7. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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");
    }

    }

    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

  9. #9

    Default Re: Help with a java assignment?

    You don't have a return statement.
    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:
    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:
    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...
    if( aSalary( Integer.parseInt( salesArray[i] ) )%2==0 )
    Last edited by kenster421; September 30th, 2011 at 01:58 PM. Reason: Syntax Highlighting
    Kenneth Walter
    Software Developer
    http://kennywalter.com

Similar Threads

  1. [SOLVED] OOP java assignment
    By enkei in forum Object Oriented Programming
    Replies: 1
    Last Post: April 27th, 2011, 11:16 AM
  2. Java Assignment Help
    By welsh_rocker in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 12th, 2011, 06:02 AM
  3. my java assignment -- please help
    By java_beginner in forum Java Theory & Questions
    Replies: 6
    Last Post: May 20th, 2010, 08:32 AM
  4. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM
  5. I need extensive help with my java assignment
    By MoshMoakes in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 12th, 2009, 07:44 PM