1

I am new to Java, This may be a silly question but I really need your help.

Code:

String str[] ={"Enter your name","Enter your age","Enter your salary"};
        Scanner sc = new Scanner(System.in);
        int[] i = new int[2];
        String[] s = new String[2];
        int[] y = new int[2];
        for(int x = 0  ; x <= 2 ; x++)
        {
            System.out.println(str[0]);
            s[x] = sc.nextLine();
            System.out.println(s[x]);

            System.out.println(str[1]);
            i[x]=sc.nextInt();
            System.out.println(i[x]);

            System.out.println(str[2]);
            y[x]=sc.nextInt();
            System.out.println(y[x]);
        }

Output :

run:
Enter your name
Sathish
Sathish
Enter your age
26
26
Enter your salary
25000
25000
Enter your name

Enter your age
23
23
Enter your salary
456
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
    at javaapplication1.JavaApplication1.main(JavaApplication1.java:121)
456
Enter your name
Java Result: 1
BUILD SUCCESSFUL (total time: 34 seconds)

Note: 1st loop works correctly. Then it throws error.

Can someone tell me where my mistake is and why it is not working?

2 Answers 2

5

this line make error

for(int x = 0  ; x <= 2 ; x++)

change to

for(int x = 0  ; x < 2 ; x++)

complete code

   public static void main(String[] args) {
        String str[] = {"Enter your name", "Enter your age", "Enter your salary"};
        Scanner sc = new Scanner(System.in);
        int[] i = new int[2];
        String[] s = new String[2];
        int[] y = new int[2];
        for (int x = 0; x < 2; x++) {
            System.out.println(str[0]);
            s[x] = sc.nextLine();
            System.out.println(s[x]);

            System.out.println(str[1]);
            i[x] = sc.nextInt();
            System.out.println(i[x]);

            System.out.println(str[2]);
            y[x] = sc.nextInt();
            System.out.println(y[x]);
            sc.nextLine();// add this line to skip "\n" Enter key
        }
    }

........................explain..................................

the error is here

for (int x = 0; x =< 2; x++) {

  s[x] = sc.nextLine();// when x=2 error occurs

because s array is length of 2 and has only 2 elements but array indexes are zero based and you can't get s[2] .

and second problem is "But 1st loop works correctly. when loop 2 starts its not allowing me to type Name .its directly goes to age .Do you know why ? "

well..

input.nextInt() which only reads the int value. when you continue reading with input.nextLine() you receive the "\n" Enter key. So to skip this you have to add the input.nextLine()

to get more explanation about this 2nd issue you must read this question Skipping nextLine() after use nextInt()

Sign up to request clarification or add additional context in comments.

5 Comments

Or better: x < s.length.
Thanks for the fastest reply But 1st loop works correctly. when loop 2 starts its not allowing me to type Name .its directly goes to age .Do you know why ?
@user3114645 - that is a different error. When you use sc.nextInt(), you are getting every character up until you hit enter (BUT NOT THE RETURN CHARACTER ITSELF). So then the next time you call sc.nextLine() it is only holding the return character and nothing else. The way to properly flush is to simply call sc.nextLine() and don't store it after every time you call sc.nextInt().
@user3114645 check my updated code.i will give you explanation in a minute
@FastSnail Perfect thank you :) and NoseknowsAll thanks for your help to
0

In Java as many programming language, the count begins with 0, so an array of length 3, when the computer begins to count is : 0,1,2 and not 1,2,3. The general rules is: Array Length - 1.

As you are working with arrays use the attribute length for check it , it is more secure.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.