944,135 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 898
  • Java RSS
Oct 18th, 2009
2

Java Scanners

Expand Post »
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

java Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Loved is offline Offline
17 posts
since Oct 2009
Oct 18th, 2009
1
Re: Java Scanners
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)
  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.
Reputation Points: 256
Solved Threads: 72
Nearly a Posting Virtuoso
majestic0110 is offline Offline
1,306 posts
since Oct 2007
Oct 18th, 2009
0
Re: Java Scanners
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)
  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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Loved is offline Offline
17 posts
since Oct 2009
Oct 19th, 2009
0
Re: Java Scanners
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DWIZARD1 is offline Offline
3 posts
since Jun 2009
Oct 19th, 2009
0
Re: Java Scanners
Nobody ever learnt to program in Java by using drag 'n' drop.
Reputation Points: 256
Solved Threads: 72
Nearly a Posting Virtuoso
majestic0110 is offline Offline
1,306 posts
since Oct 2007
Oct 19th, 2009
0
Re: Java Scanners
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DWIZARD1 is offline Offline
3 posts
since Jun 2009
Oct 19th, 2009
0
Re: Java Scanners
here
Please do not hijack threads, start a new one!
Reputation Points: 256
Solved Threads: 72
Nearly a Posting Virtuoso
majestic0110 is offline Offline
1,306 posts
since Oct 2007
Oct 21st, 2009
0
Re: Java Scanners
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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Loved is offline Offline
17 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: hibarnate Bi-directional associations
Next Thread in Java Forum Timeline: Trouble getting a button to toggle a panel, and new to Java.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC