| | |
Java Scanners
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 7
Reputation:
Solved Threads: 0
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
"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
java Syntax (Toggle Plain Text)
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); } }
Last edited by Loved; Oct 18th, 2009 at 4:38 pm.
1
#2 Oct 18th, 2009
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:
I hope this helps. I also fixed your main method declaration. Take a look here for more information on variables & scope:
here
java Syntax (Toggle Plain Text)
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
Last edited by majestic0110; Oct 18th, 2009 at 4:58 pm.
If you have a quality, be proud of it and let it define you. Add it to the world!
If you got your answer, please mark the thread as Solved. It saves time when people are looking to contribute threads or for answers!
If you got your answer, please mark the thread as Solved. It saves time when people are looking to contribute threads or for answers!
•
•
Join Date: Oct 2009
Posts: 7
Reputation:
Solved Threads: 0
0
#3 Oct 18th, 2009
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:
java Syntax (Toggle Plain Text)
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
0
#7 Oct 19th, 2009
here
Please do not hijack threads, start a new one!
Please do not hijack threads, start a new one!
If you have a quality, be proud of it and let it define you. Add it to the world!
If you got your answer, please mark the thread as Solved. It saves time when people are looking to contribute threads or for answers!
If you got your answer, please mark the thread as Solved. It saves time when people are looking to contribute threads or for answers!
![]() |
Similar Threads
- FT Junior Java Developer Needed for Major Media firm in NYC (Software Development Job Offers)
- Java/J2EE Senior Software Engineer (Software Development Job Offers)
- Front-end Java Software Engineer for Digital Video/Media Market Leader (Software Development Job Offers)
- Senior Software Engineer (Java) (Web Development Job Offers)
- Java Software Engineer (Software Development Job Offers)
- Java Front-end Developer Engineer for Stealth Media Start-up (Software Development Job Offers)
- Java Developer Required (Software Development Job Offers)
Other Threads in the Java Forum
- Previous Thread: hibarnate Bi-directional associations
- Next Thread: Trouble getting a button to toggle a panel, and new to Java.
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary blackberry block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working






