javaworld
javaworld

Reputation: 435

java array using scanner

In my program, I want to display an array using Scanner, i.e the user enters the array values during runtime.

public class NameComparator {
    public static void main(String args[]) {
        Scanner sn = new Scanner(System.in);
        int n = sn.nextInt();
        System.out.println("the array size is" + n);
        int[] a = new int[7];
        for (int i = 0; i >= a.length; i++) {
            a[i] = sn.nextInt();
            System.out.println("the value of a[i]" + a[i]);
        }
        sn.close();
        System.out.println("array values are");
    }
}

Here, I have got the array size from Scanner and then using for loop to get each array value, but I don't know why the array block hasn't executed. JVM just skips the for loop. Scanner works well

Upvotes: 0

Views: 2608

Answers (5)

Yusuf Kapasi
Yusuf Kapasi

Reputation: 587

change the below code

for(int i=0;i>=a.length;i++) with for(int i=0;i<a.length;i++)

the condition should be < instead of >= also use sn.hasNext() can simplify the solution.

Upvotes: 1

Alexander Kohler
Alexander Kohler

Reputation: 1967

I would do some searching around because there's plenty of questions similar to this.

Regardless, you have a couple things incorrect. You prompt the user for an array size but then throw it out and use 7 instead:

    int[] a= new int[7];

So, this should be:

    int[] a= new int[n];

Second, your loop condition:

for(int i=0;i>=a.length;i++)

will be true as long as i is greater than a, which will never happen as long as a is a positive integer (because i starts at zero). So, if we're using less than, we should also remember that arrays are zero indexed, so if you input a value of 3, you only want to populate indexes 0, 1 and 2.

So, this should instead be:

for(int i=0;i < a.length;i++)

Finally, remember to prompt the user, even if this is just a learning exercise it's good practice. Put that all together and you'll get something like this:

    public static void main(String args[])
    {
    Scanner sn=new Scanner(System.in);
    System.out.println("Please enter an array size:" );
    int n=sn.nextInt();
    System.out.println("the array size is "+ n);
    int[] a= new int[n];
    System.out.println("Please enter your " + n + "array values:");
    for(int i=0;i < a.length;i++)
    {
        a[i]= sn.nextInt();
        System.out.println("The value of a[" + i  + "] is "  +  a[i]);
    }
    sn.close();

    System.out.println("Array values are " );
    for (int arrayValue : a)
        System.out.println("    " + arrayValue);

}

Upvotes: 1

Tom
Tom

Reputation: 17567

This:

for(int i=0;i>=a.length;i++)

should be:

for (int i = 0; i < a.length; i++)

You want to loop as long as i is smaller than a.length (i.e. the size of the array). The for loop will be exited (or skipped) if the termination condition returns false. Since you're initializing i with 0, i>=a.length (i.e. 0 >= 7) will be instantly false.

Please note, that I've wrote i < a.length and not i <= a.length. The array size is currently set to 7, therefore the valid indices are from 0 to 6. You'll get an ArrayIndexOutOfBoundsException if you try to access index 7.

And you forgot to use your variable n to set the array size:

int[] a= new int[n];

Upvotes: 1

forgivenson
forgivenson

Reputation: 4435

Take a close look at your for loop.

for(int i=0;i>=a.length;i++)

Notice that you are using a greater than sign.

Since i equals 0, the length of a would have to be 0 for this loop to run, and we already know that you declared a with a length of 7.

Upvotes: 1

user2575725
user2575725

Reputation:

There are few issues:

int[] a= new int[7];//<-- why hard coded?
int[] a= new int[n];// try, you already have array size from user

for(int i=0;i>=a.length;i++)//<-- condition fails, i is 0 for the first time
for(int i=0; i < a.length; i++)//try this

Upvotes: 1

Related Questions