Java Scanners

Thread Solved

Join Date: Oct 2009
Posts: 7
Reputation: Loved is an unknown quantity at this point 
Solved Threads: 0
Loved Loved is offline Offline
Newbie Poster

Java Scanners

 
2
  #1
Oct 18th, 2009
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

  1. import java.util.*;
  2.  
  3. public class MainClass
  4. {
  5. public static void main()
  6. {
  7. System.out.println("Enter the employee type (Professional or Hourly)");
  8. Scanner scan = new Scanner(System.in);
  9. String type = scan.nextLine();
  10. type = type.toUpperCase();
  11.  
  12.  
  13. System.out.println("Enter the employee's name");
  14. String name = scan.nextLine();
  15. System.out.println("Enter the employee's ID Number (int)");
  16. int idNumber = scan.nextInt();
  17. System.out.println("Enter the employee's pay (double)");
  18. double pay = scan.nextDouble();
  19.  
  20. if (type.equals("PROFESSIONAL"))
  21. {
  22. System.out.println("Enter the employee's accrued vacation (int)");
  23. int vacation = scan.nextInt();
  24. }
  25. else
  26. {
  27. Pay person1 = new Pay(name, idNumber, pay);
  28. }
  29. System.out.format("%s is id %d with pay $%.2f %d%n", name, idNumber, pay, vacation);
  30. }
  31. }
Last edited by Loved; Oct 18th, 2009 at 4:38 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,293
Reputation: majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about 
Solved Threads: 67
majestic0110's Avatar
majestic0110 majestic0110 is offline Offline
Nearly a Posting Virtuoso
 
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:

  1. import java.util.*;
  2.  
  3. public class MainClass
  4. {
  5. public static void main(String[] args) //need to declare a parameter //that is a String array
  6. {
  7. System.out.println("Enter the employee type (Professional or Hourly)");
  8. Scanner scan = new Scanner(System.in);
  9. String type = scan.nextLine();
  10. type = type.toUpperCase();
  11. int vacation = 0; // declare int in main method
  12.  
  13. System.out.println("Enter the employee's name");
  14. String name = scan.nextLine();
  15. System.out.println("Enter the employee's ID Number (int)");
  16. int idNumber = scan.nextInt();
  17. System.out.println("Enter the employee's pay (double)");
  18. double pay = scan.nextDouble();
  19.  
  20. if (type.equals("PROFESSIONAL"))
  21. {
  22. System.out.println("Enter the employee's accrued vacation (int)");
  23. vacation = scan.nextInt();
  24. }
  25. else
  26. {
  27. // Pay person1 = new Pay(name, idNumber, pay);
  28. }
  29. System.out.format("%s is id %d with pay $%.2f %d%n", name, idNumber, pay, vacation);
  30. }
  31. }

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!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 7
Reputation: Loved is an unknown quantity at this point 
Solved Threads: 0
Loved Loved is offline Offline
Newbie Poster
 
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


Originally Posted by majestic0110 View Post
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:

  1. import java.util.*;
  2.  
  3. public class MainClass
  4. {
  5. public static void main(String[] args) //need to declare a parameter //that is a String array
  6. {
  7. System.out.println("Enter the employee type (Professional or Hourly)");
  8. Scanner scan = new Scanner(System.in);
  9. String type = scan.nextLine();
  10. type = type.toUpperCase();
  11. int vacation = 0; // declare int in main method
  12.  
  13. System.out.println("Enter the employee's name");
  14. String name = scan.nextLine();
  15. System.out.println("Enter the employee's ID Number (int)");
  16. int idNumber = scan.nextInt();
  17. System.out.println("Enter the employee's pay (double)");
  18. double pay = scan.nextDouble();
  19.  
  20. if (type.equals("PROFESSIONAL"))
  21. {
  22. System.out.println("Enter the employee's accrued vacation (int)");
  23. vacation = scan.nextInt();
  24. }
  25. else
  26. {
  27. // Pay person1 = new Pay(name, idNumber, pay);
  28. }
  29. System.out.format("%s is id %d with pay $%.2f %d%n", name, idNumber, pay, vacation);
  30. }
  31. }

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

here
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 3
Reputation: DWIZARD1 is an unknown quantity at this point 
Solved Threads: 0
DWIZARD1 DWIZARD1 is offline Offline
Newbie Poster
 
0
  #4
Oct 19th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,293
Reputation: majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about 
Solved Threads: 67
majestic0110's Avatar
majestic0110 majestic0110 is offline Offline
Nearly a Posting Virtuoso
 
0
  #5
Oct 19th, 2009
Nobody ever learnt to program in Java by using drag 'n' drop.
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!
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 3
Reputation: DWIZARD1 is an unknown quantity at this point 
Solved Threads: 0
DWIZARD1 DWIZARD1 is offline Offline
Newbie Poster
 
0
  #6
Oct 19th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,293
Reputation: majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about 
Solved Threads: 67
majestic0110's Avatar
majestic0110 majestic0110 is offline Offline
Nearly a Posting Virtuoso
 
0
  #7
Oct 19th, 2009
here
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!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 7
Reputation: Loved is an unknown quantity at this point 
Solved Threads: 0
Loved Loved is offline Offline
Newbie Poster
 
0
  #8
Oct 21st, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC