Home > Back-end >  iterate over two vectors with some conditions up to ten times
iterate over two vectors with some conditions up to ten times

Time:11-03

I have two vectors, vectorName and vectorNum, I need to iterate over them asking for input up to ten times or until nothing is entered, the problem is that when I reach the 10th loop it asks for vectorName an 11th time and then throws IndexOutOfBoundsException

this is my code:

public static void main(String[] args) {
    Scanner read = new Scanner(System.in);
    int nameQuantity = 0;
    int numQuantity = 0;
    String vectorName[] = new String[10];
    int vectorNum[] = new int[10];
    System.out.println("enter name or enter nothing to end");
    vectorName[nameQuantity] = read.nextLine();

    if (vectorName[nameQuantity].length()==0) {
        System.out.println("end");
    }else { 
        nameQuantity  ;
        while(nameQuantity<11) {
            System.out.println("enter a number from 1 to 12");
            int num = read.nextInt();
            if (num<=12 && num>=1) {
                
                vectorNum[numQuantity] = num;
                numQuantity  ;
                System.out.println("enter name or enter nothing to end");
                read.nextLine();
                vectorName[nameQuantity] = read.nextLine();
                
                if(vectorName[nameQuantity].length() == 0  || numQuantity == 10) {  
                                
                    for (int n = 0; n<=numQuantity-1; n  ) {
                        System.out.print("\n "   vectorName[n]   " "  vectorNum[n]);
                    }
                    nameQuantity = 11;
                            }else {
                                nameQuantity  ;
                        }
                    }else {
                System.out.println("number must be from 1 to 12");
            }
            
            }
        }
    }
}

I tried changing the vector size to 11 which worked but as a result it saves an 11th name which I don't need since I expect it to ask only up to ten times the names and numbers. I also tried changing the do while to a while, the same thing happens.

CodePudding user response:

Start of 1st Iteration: nameQuantity = 1 numQuantity = 0

Start of 2nd Iteration: nameQuantity = 2 numQuantity = 1

...

Start of 10th Iteration: nameQuantity = 10 numQuantity = 9

Calling vectorName[nameQuantity] will cause the IndexOutOfBoundsException at this point, since vectorName has a size of 10, meaning the last index is 9.

I'm not sure why you're asking for the user to enter a name, then starting a loop where you ask for a number and then ask for a name.

Your logic:

Receive name from user

If name is empty

    Stop

Else

    Loop 10 times:

        Receive number from user

        If name is empty or received 10 numbers

            Stop

Why not just have everything in a loop where you ask for a name first, and if a name was provided, ask for a number?

Loop 10 times

    Receive name from user

    If name is empty

        Stop

    Else
    
        Receive number from user
  • Related