Hi, I am fairly new to java and have been playing around with scanners and while trying to understand this problem I am running into an error
"Non-static variable vacation cannot be referenced from a static context"

Obviously this isn't quite a completed program, but I am not sure why vacation cannot be given a value within the if statement. When I try giving it a value outside of the if statement it works fine.

Any help with clarifying this would be greatly appreciated-thanks

import java.util.*;

public class MainClass
{
   public static void main()
   {
    System.out.println("Enter the employee type (Professional or Hourly)");
    Scanner scan = new Scanner(System.in);
    String type = scan.nextLine();
    type = type.toUpperCase();

    
    System.out.println("Enter the employee's name");
    String name = scan.nextLine();    
    System.out.println("Enter the employee's ID Number (int)");
    int idNumber = scan.nextInt();
    System.out.println("Enter the employee's pay (double)");
    double pay = scan.nextDouble();
       
    if (type.equals("PROFESSIONAL"))
        {
            System.out.println("Enter the employee's accrued vacation (int)");
            int vacation = scan.nextInt();
        }
        else
        {
                Pay person1 = new Pay(name, idNumber, pay);
        }
    System.out.format("%s is id %d with pay $%.2f %d%n", name,  idNumber, pay, vacation);
    }
}

Recommended Answers

All 7 Replies

Take a look at Line 23 where you declare the int variable vacation. This variable is declared within the body of a conditional statement, which means its lifespan is confined to that conditional block. Nothing outside of that block can see that variable. Take a look at the changes I have made:

import java.util.*;

public class MainClass
{
   public static void main(String[] args) //need to declare a parameter //that is a String array
   {
    System.out.println("Enter the employee type (Professional or Hourly)");
    Scanner scan = new Scanner(System.in);
    String type = scan.nextLine();
    type = type.toUpperCase();
    int vacation = 0; // declare int in main method
    
    System.out.println("Enter the employee's name");
    String name = scan.nextLine();    
    System.out.println("Enter the employee's ID Number (int)");
    int idNumber = scan.nextInt();
    System.out.println("Enter the employee's pay (double)");
    double pay = scan.nextDouble();
       
    if (type.equals("PROFESSIONAL"))
        {
            System.out.println("Enter the employee's accrued vacation (int)");
          vacation = scan.nextInt();
        }
        else
        {
//                Pay person1 = new Pay(name, idNumber, pay);
        }
    System.out.format("%s is id %d with pay $%.2f %d%n", name,  idNumber, pay, vacation);
    }
}

I hope this helps. I also fixed your main method declaration. Take a look here for more information on variables & scope:

here

Thank you very much majestic! So anything within conditional statements needs to be initialized outside of the statements but values can be modified within them. Your changes make great sense. Thanks

Take a look at Line 23 where you declare the int variable vacation. This variable is declared within the body of a conditional statement, which means its lifespan is confined to that conditional block. Nothing outside of that block can see that variable. Take a look at the changes I have made:

import java.util.*;

public class MainClass
{
   public static void main(String[] args) //need to declare a parameter //that is a String array
   {
    System.out.println("Enter the employee type (Professional or Hourly)");
    Scanner scan = new Scanner(System.in);
    String type = scan.nextLine();
    type = type.toUpperCase();
    int vacation = 0; // declare int in main method
    
    System.out.println("Enter the employee's name");
    String name = scan.nextLine();    
    System.out.println("Enter the employee's ID Number (int)");
    int idNumber = scan.nextInt();
    System.out.println("Enter the employee's pay (double)");
    double pay = scan.nextDouble();
       
    if (type.equals("PROFESSIONAL"))
        {
            System.out.println("Enter the employee's accrued vacation (int)");
          vacation = scan.nextInt();
        }
        else
        {
//                Pay person1 = new Pay(name, idNumber, pay);
        }
    System.out.format("%s is id %d with pay $%.2f %d%n", name,  idNumber, pay, vacation);
    }
}

I hope this helps. I also fixed your main method declaration. Take a look here for more information on variables & scope:

here

Hi am new to java too i stopped at creating frame .. I want you to donload Netbeans so that it will so easy for you to programme.

Nobody ever learnt to program in Java by using drag 'n' drop.

Can someone tell me how i can train my self on java as i have no money to spend in learning Java..May be were i can download books, video and tools that i can use

thanks

here
Please do not hijack threads, start a new one!

DWizard, I am using BlueJ for my coding. It is a pretty simple interface and decent at showing you errors for newbies. Once you write more involved code, then it would be a good time to use a larger application. Thanks again @ majestic.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.