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?

Jagger
  • 9,590
  • 7
  • 42
  • 78
user3114645
  • 57
  • 3
  • 14

2 Answers2

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()

Community
  • 1
  • 1
Madhawa Priyashantha
  • 9,208
  • 7
  • 28
  • 58
  • 3
    Or better: `x < s.length`. – Luiggi Mendoza Dec 22 '14 at 15:32
  • 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 Dec 22 '14 at 15:34
  • @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()`. – NoseKnowsAll Dec 22 '14 at 15:37
  • @user3114645 check my updated code.i will give you explanation in a minute – Madhawa Priyashantha Dec 22 '14 at 15:43
  • 1
    @FastSnail Perfect thank you :) and NoseknowsAll thanks for your help to – user3114645 Dec 22 '14 at 15:56
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.

mgilsn
  • 111
  • 5