954,123 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Java Scanners

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);
    }
}
Loved
Newbie Poster
17 posts since Oct 2009
Reputation Points: 10
Solved Threads: 1
 

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

majestic0110
Nearly a Posting Virtuoso
1,328 posts since Oct 2007
Reputation Points: 256
Solved Threads: 72
 

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

Loved
Newbie Poster
17 posts since Oct 2009
Reputation Points: 10
Solved Threads: 1
 

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.

DWIZARD1
Newbie Poster
3 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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

majestic0110
Nearly a Posting Virtuoso
1,328 posts since Oct 2007
Reputation Points: 256
Solved Threads: 72
 

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

DWIZARD1
Newbie Poster
3 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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

majestic0110
Nearly a Posting Virtuoso
1,328 posts since Oct 2007
Reputation Points: 256
Solved Threads: 72
 

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.

Loved
Newbie Poster
17 posts since Oct 2009
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You