Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7686065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:23:41+00:00 2026-05-31T19:23:41+00:00

When I tested this code, it gave me java.lang.ArrayIndexOutOfBoundsException: 2 The code isn’t finished

  • 0

When I tested this code, it gave me java.lang.ArrayIndexOutOfBoundsException: 2
The code isn’t finished yet but this is what I am trying to do:
A program that asks the user for two numbers. Print all numbers from the smaller
number to the larger number (inclusive) in a random order. Using this order, find the
largest sum of 2 numbers adjacent to one another.
Could some please point out to me what needs to be fixed as well as how to fix it? Thanks.

 boolean isNum = false;
    int in1 = 0;
    int in2 = 0;
    int size = 0;
    int largestsum = 0;
    while (!isNum) {
        try {
            System.out.print("Enter a number: ");
            in1 = Integer.parseInt(in.readLine());
            boolean isSame = false;
            while (!isSame) {
                System.out.print("Enter a number: ");
                try {
                    in2 = Integer.parseInt(in.readLine());
                    if (in1 != in2) {
                        isSame = true;
                    } else {
                        System.out.println("Same number.");
                    }
                } catch (NumberFormatException nfe) {
                    System.out.println("Not a number.");
                }
            }
            isNum = true;
        } catch (NumberFormatException nfe) { //catches error
            System.out.println("Not a number."); //informs user of the error
        }
    }
    if (in1 > in2) {
        size = in1 - in2 + 1;
        int[] num = new int[size];
        for (int i = 0; i < size; i++) {
            num[i] = in2 + i;
        }
        int[] order = new int[size];
        boolean[] used = new boolean[size];
        for (int i = 0; i < size; i++) {
            int r = (int) ((size) * (Math.random()));
            while (used[r]) {
                r = (int) ((size) * Math.random());
            }
            order[i] = r; //fill array
            used[r] = true; //r is now used
        }
        for (int i = 0; i < size; i++) {
            if (num[order[i]] + num[order[i + 1]] > largestsum) {
                largestsum = num[order[i]] + num[order[i + 1]];
            }
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:23:43+00:00Added an answer on May 31, 2026 at 7:23 pm

    The last for loop will overflow when i=size-1

    In your question, you want to get the sum between 2 adjacent numbers. Your logic in the last for loop says that you calculate the sum by adding number i to i+1. The last item in your array is at position size-1, so when you then try to add this to the next adjacent number, (size-1)+1 overflows the array.

    To fix the problem, your last for loop should only continue until i<size-1, like so…

        for (int i = 0; i < size-1; i++) {
            if (num[order[i]] + num[order[i + 1]] > largestsum) {
                largestsum = num[order[i]] + num[order[i + 1]];
            }
        }
    

    With this fixed logic, the largest item in the for loop is size-2, and when you add 1 to it (for the next adjacent number), it will choose item (size-2)+1, which is the last item in the array, rather than overflowing the array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had tested this code: http://support.microsoft.com/kb/316383 It works, but a Word application is opened
I tested this code snippet but the first function is not executing while the
I'm implementing this JQuery Lightbox plugin inside a DataList: http://leandrovieira.com/projects/jquery/lightbox/ I tested this code
So I tested this code (intersection between two ellipses, look at the accepted answer),
So, I have xampp. I tested this code on ZendServer, result the same. <?php
How can I define a CSS scrollbar style cross browser? I tested this code,
While using this code, i can't get any results... Ext.define('Teste.view.Main', { extend: 'Ext.Container', initialize:
I have tested this servlet and it works well, except in Google Chrome it
I've tested this on Win XP and Windows Server 2008 R2 in Powershell 2.0:
I know this particular query works, as I tested it with unprepared, procedural methods.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.