Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Parsing a string to an int while receiving user input?

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    6
    My Mood
    Amused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Parsing a string to an int while receiving user input?

    package exercise2;
    import java.util.Scanner;
     
    public class Exercise2 {
     
        Scanner user_input = new Scanner(System.in);
     
     
        public static void main(String[] args) {
     
     
            System.out.println ("Pick a number from 0 to 100");
     
     
            String number = user_input.next(); 
           int pn =Integer.parseInt(number.trim());
     
     
        } 
    }

    I keep getting this error on the "String number = user_input.next();" line.
    non-static variable user_input cannot be referenced from a static context

    Any help? I've looked online and I get lots of help for assigning variables, but not with user input.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,172
    My Mood
    Hungover
    Thanks
    141
    Thanked 600 Times in 515 Posts

    Default Re: Parsing a string to an int while receiving user input?

    This problem has nothing to do with user input. The main() method is a static method, which means it can only access static methods and variables. user_input is not a static variable.

    Your options are to create an instance of Exercise2 and access user_input form within it, or to make user_input static.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    6
    My Mood
    Amused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Parsing a string to an int while receiving user input?

    Is there any chance that I could get pointed in the right direction? I'm brand new at this.
    Thank you for the help though

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,172
    My Mood
    Hungover
    Thanks
    141
    Thanked 600 Times in 515 Posts

    Default Re: Parsing a string to an int while receiving user input?

    I'd say google is your friend here- you must understand what the static keyword does.

    Basically: normal, non-static variables and methods (like your user_input variable) must be a member of a specific instance of a class. For example, it doesn't make sense for there to be a JFrame height or width without an instance of JFrame to have a height or width.

    However, static variables and methods (like the main method) do not need to be a member of a specific instance of a class. So you don't need to create an instance of Exercise2 before calling main (how would you?). System.out is another example of a static variable.

    The confusion in your case is caused by mixing static and non-static. While you're "in" main, you aren't working from a specific instance of Exercise2. So when you try to use user_input from the main method, it's like trying to use a JFrame's height or width without creating a JFrame first.

    You should probably create an instance of Exercise2, mostly because overusing the static keyword is a really bad practice to habitualize. But you could also make user_input static.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    TopdeK (May 11th, 2011)

  6. #5
    Junior Member
    Join Date
    May 2011
    Posts
    6
    My Mood
    Amused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Parsing a string to an int while receiving user input?

    Thank you! I'll be sure to google everything in the world!! haha

Similar Threads

  1. Parsing many Ints from a user input String.
    By helpmeplease111 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2011, 08:41 PM
  2. Parsing Scientific Notation from String
    By aussiemcgr in forum Java Theory & Questions
    Replies: 7
    Last Post: December 14th, 2010, 08:45 AM
  3. JTable Updating String Values from User Input
    By aussiemcgr in forum AWT / Java Swing
    Replies: 5
    Last Post: August 3rd, 2010, 01:48 PM
  4. Parsing input based on grammar rules
    By Winterfresh in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 11th, 2010, 06:54 PM
  5. Input file parsing to insert into DB
    By IDForums in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: September 30th, 2009, 02:29 AM

Tags for this Thread