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: just a simple array code. please help

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default just a simple array code. please help

    import java.util.Scanner;

    public class Returnee
    {
    public Scanner nylro = new Scanner(System.in);
    public static void main(String[]args)
    {
    int nums[] = new int [5];
    int value;

    for(int x=0 ; x<nums.length;x++)
    {
    System.out.println("Enter value "+ (x+1)+ ":");
    nums[x] = nylro.nextInt();
    }

    }
    }

    /*Returnee.java:14: error: non-static variable nylro cannot be referenced from a static context
    nums[x] = nylro.nextInt();
    ^
    1 error*/


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: just a simple array code. please help

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    non-static variable nylro cannot be referenced from a static context
    The nylro variable only exists when an instance of the class is created.
    Some solutions:
    move it into the main() method
    make it static so the code in the static method: main() can reference it
    create an instance of the class and use the reference to that instance to refer to nylro
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: just a simple array code. please help

    oh my god. thanks a lot amigo. i never saw it.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: just a simple array code. please help

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

  5. #5
    Junior Member
    Join Date
    Jul 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: just a simple array code. please help

    Running code:---
    ...............................
    **** Code removed

    note: Either your u have to take scanner as static otherwise define inside the main method
    Last edited by Norm; July 3rd, 2014 at 06:52 AM. Reason: Please don't do OPs work for him

Similar Threads

  1. Problem with simple array program.
    By Psychen in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 13th, 2013, 06:48 PM
  2. Simple Array Problem
    By kbrady481 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 25th, 2013, 05:04 PM
  3. [SOLVED] Simple array code
    By azizmaiden in forum Java Theory & Questions
    Replies: 1
    Last Post: March 2nd, 2013, 06:54 PM
  4. [SOLVED] Array (simple).
    By 18107 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 25th, 2012, 05:59 PM
  5. Error for a simple array
    By DudeJericho in forum Collections and Generics
    Replies: 4
    Last Post: April 25th, 2011, 02:46 PM