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 9244497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:02:58+00:00 2026-06-18T09:02:58+00:00

I have a section of code that is puzzling me. I define an integer

  • 0

I have a section of code that is puzzling me. I define an integer array inside an if/else statement because the length of the array depends on the length of 2 inputs to the method. My problem is that outside the if/else statement, the variable definition seems to be lost.

import java.util.Arrays;

public class test {

  public String AddArrays(int [] arg1, int [] arg2) {
    int L1 = arg1.length;
    int L2 = arg2.length;
    if (L1 > L2) {
        int[] output = new int[L2];
        for (int i = 0; i < L2; i++) {
            output[i] = arg1[i] + arg2[i];
        }
    } else {
        int[] output = new int[L1];
        for (int i = 0; i < L2; i++) {
            output[i] = arg1[i] + arg1[i];
        }
    }
    String result = Arrays.toString(output);
    return result;
    }
}

The error I get is on the statement String result = Arrays.toString(output); where Eclipse tells me that output cannot be resolved to a variable.

…and by the way, yes, I know that this is not the way to add two integer arrays — I reduced this from more complex code to demonstrate the problem!

  • 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-06-18T09:03:00+00:00Added an answer on June 18, 2026 at 9:03 am

    Define output before if statement. Like this:

    int[] output;
    int L1 = arg1.length;
    int L2 = arg2.length;
    if (L1 > L2) {
        output = new int[L2];
        for (int i = 0; i < L2; i++) {
            output[i] = arg1[i] + arg2[i];
        }
    } else {
        output = new int[L1];
        for (int i = 0; i < L2; i++) {
            output[i] = arg1[i] + arg1[i];
        }
    }
    String result = Arrays.toString(output);
    return result;
    }
    

    When you declared output inside the if statement it simply had only that block scope.

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

Sidebar

Related Questions

I have a section of code that can be summarised as follows; void MyFunc()
I have the following section of code in an app that I am writing:
In my xaml code, inside the Window.Resources section, I have defined a Data Template
I have a section of code that I would not like to run if
I have a section of code that I need to remove from multiple files
I have a section of code that is being used to determine if a
I have a section of code that uses a Boost library that results in
I have this section of code that saves a list item on click to
I have a section of code in a controller that replaces existing HTML with
MVC3 VB.NET application using Itextsharp. I have a section of code that generates a

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.