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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:15:30+00:00 2026-06-11T01:15:30+00:00

I have to pass two strings to a thread, which will return an array

  • 0

I have to pass two strings to a thread, which will return an array containing both of them, and I must use callable.

Here’s what I have so far:

public class toArray implements Callable<String[]> {

private String string1 string2;

public toArray (String first, String second)
{
    string1= first;
    string2 = second;
}

    @Override
    public String[] call() throws Exception {
        String [] allStrings = null;
            allStrings[0] = string1;
            allStrings[1] = string2;
        return allStrings;
    }
}

Below is the main:

public class theFuntion{

    public static void main(String[] args) {


        FutureTask<String[]> task = new FutureTask (new MyCallable());
        ExecutorService es = Executors.newSingleThreadExecutor();
        es.submit (task);
        try{
               String[] result = task.get();
               System.out.println(result[1] + result[2]);
           }
        catch(Exception e){
                           System.err.println(e);
    }
         es.shutdown();
}

The problem is in main: it says that result is expecting a String[] but it’s getting an object. If I cast result it will say an exception must be declared to be thrown.

  • 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-11T01:15:32+00:00Added an answer on June 11, 2026 at 1:15 am

    Change the signature of the class to

    public class toArray implements Callable<String[]>
    

    and the signature of the call method to

    public String[] call() throws Exception
    

    UPDATE:
    Ok, there are more than one error…
    See the following working sample

    public class Test{
    
        public static void main(String[] args) {
    
    
            FutureTask<String[]> task = new FutureTask(new MyCallable("a", "b"));
            ExecutorService es = Executors.newSingleThreadExecutor();
            es.submit (task);
            try{
                String[] result = task.get();
                System.out.println(result[0] + result[1]);
            }
            catch(Exception e){
                System.err.println(e);
            }
            es.shutdown();
        }
    
        public static class MyCallable implements Callable<String[]>
        {
    
            private String string1, string2;
    
            public MyCallable (String first, String second)
            {
                string1= first;
                string2 = second;
            }
    
            @Override
            public String[] call() throws Exception {
                String [] allStrings = new String[2];
                allStrings[0] = string1;
                allStrings[1] = string2;
                return allStrings;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models, Project and Category, which have a many-to-many relationship between them.
I have written two simple Java classes (one of them containing main(), and the
I have two lists containing unicode strings. I'm trying to remove every element from
I have a WCF service which I want to pass URL strings to. However,
I have a situation where I need to pass two parameters to an action.
The problem I have is to pass the chained search of two models to
I have two pages and I want to pass data to each other. How
i have two flex applications and i want to pass the data from one
I have two ListActivity classes that I want to pass into into a third
I Have two problem in this Case : I want to pass a JSON

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.