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

  • Home
  • SEARCH
  • 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 7901489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:13:56+00:00 2026-06-03T09:13:56+00:00

I have a problem accessing the values in my array for the purpose of

  • 0

I have a problem accessing the values in my array for the purpose of subtracting the values held in column 1 of the array from the value I’m trying to get the nearest match to. I’m getting a type mismatch error cannot convert from double to int

As a bit of an aside I’m even sure if I’m approaching the problem in the correct way. I have a table where I check the values in column 1 for the closest value to the one I’m looking for and the other columns in that row are the values that need to be returned. (think a bit like some of the tables in AD&D if you played it)

// Column 1 ability score,  2 Damage mod, 3 Lift , 4 carry, 5 drag
double arr [][] = {{1.01,-7,11,9,28},
                   {1.51,-6,25,12,63},
                   {2.01,-6,38,14,96},
                   {2.51,-5,51,14,96},
                   {3.01,-5,51,17,128}};

public double EXTcha(double cha) {
    double closestNo = 0;
    double lookingFor = cha;
    int reqIndex = 0;
    double i;

    for (int row=0; row < arr.length; row++) {
        System.out.println(row+"row \t");
        double currentDiff = (lookingFor-arr[i][0]);
        System.out.println(currentDiff+"diff \t");
        if (currentDiff < closestNo) {
            closestNo = currentDiff;
            System.out.println(closestNo+"closest \t");
            reqIndex = row;
        };
    }

    for (int column=0; column<arr[reqIndex].length;column++) {
        System.out.println(arr[reqIndex][column]+"\t");
    }

    return extcha;
}

Here is the code for others after making the suggested changes

// Column 1 ability score,  2 Damage mod, 3 Lift , 4 carry, 5 drag
double arr [][] = {{1.01,-7,11,9,28},
                   {1.51,-6,25,12,63},
                   {2.01,-6,38,14,96},
                   {2.51,-5,51,14,96},
                   {3.01,-5,51,17,128}};

public double EXTcha(double cha) {
    double closestNo = 50;
    double lookingFor = cha;
    int reqIndex = 0;

    for (int row=0; row < arr.length; row++) {
        System.out.println(row+"row \t");
        double currentDiff = Math.abs(lookingFor-arr[row][0]);
        System.out.println(currentDiff+"diff \t");
        if (currentDiff < closestNo){
            closestNo = currentDiff;
            System.out.println(closestNo+"closest \t");
            reqIndex = row;
        };
    }

    for (int column=0; column<arr[reqIndex].length;column++) {
        System.out.println(arr[reqIndex][column]+"\t");
}

    return extcha;
}

I ran into another problem initializing closestNo to 0 meant that when I tested it against (currentDiff < closestNo) it was always less than so I set it to 50 which will always be greater than the double values held in column 1.

I hope this might help someone

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

    There is (at least) one bug in your algorithm, here:

    double currentDiff = (lookingFor-arr[i][0]);
    

    To get the closest match, you should use the absolute value of the difference here. Otherwise you will most likely end up always “matching” the last row (with the highest value), as that will give the smallest negative result within the expression above.

    (And, as others switftly mentioned, you should use ints for indexing into arrays).

    Overall, your approach is very low-level and brittle. You would better use a NavigableMap implementation, such as TreeMap, for this job, with the keys being the first elements in your rows and the values a List containing the rest of the numbers in each row, e.g. something like:

    NavigableMap<Double, List<Double>> characteristics = 
            new TreeMap<Double, List<Double>>();
    characteristics.put(1.01, Arrays.asList(-7.0, 11.0, 9.0, 28.0));
    ...
    Double key = characteristics.floorKey(cha);
    // Null check omitted for brevity
    List<Double> matchingCharacteristics = characteristics.get(key);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have have a problem loading and accessing data from a value object in
I have a problem in accessing the the value of i outside the for
I have a problem with accessing values in the session through pagemethods. The example
I have at the moment a problem with RowUpdating from a GridView and accessing
i have a problem with accessing a control's value in JavaScript. <tr> <th> Kullanici
I am having problem in accessing user control properties from page. I have usercontrol
I have a problem accessing JSON data. I'm new to JSON and jquery so
i have problem with libhid . i found that there 2 way 4 accessing
I am having a problem accessing the ViewData object through javascript. I have set
I have a problem with: NHibernate.Cfg.Configuration.SetProperties() Not accepting the IDictionary: NHibernateConfigHandler I get the

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.