d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Help My Code Plz
12Next
Add Reply New Topic New Poll
Member
Posts: 31,358
Joined: Mar 25 2009
Gold: 15.00
Apr 8 2020 04:33pm
So basically i'm giving an array with 5 elements and a target number.... the target number will be the sum of 2 elements in the array.
for example in this code, the [3] and [4]th element in the array added together would equal the target number (12).
So as you can see in the code, i'm trying to return these indexes of the array, but i guess my syntax is wrong, not sure how to fix it
Been stuck with this for a few hours now


return nums[1, i];



^that part. Hopefully all this makes sense. Just to be clear, i want to return the indexes NOT the actual values....
So there would be 2 indexes that i want to return. in this case it'd be index 3 and 4



Code
package com.company;

import java.util.Scanner;

public class TwoNumbers {


public static void main(String[] args) {

int[] numArray = {4, 5, 7, 9, 3};
int target = 12;



}





public int[] twoSum(int[] nums, int target) {

for(int i=0; i<nums.length; i++) {
if(nums[0] + nums[i] == target) {
return nums[0, i];
} else if(nums[1] + nums[i] == target) {
return nums[1, i];
} else if(nums[2] + nums[i] == target) {
return nums[2, i];
} else if(nums[3] + nums[i] == target) {
return nums[3, i];
} else if(nums[4] + nums[i] == target) {
return nums[4, i];
}
i++;
}

}

}


This post was edited by ferf on Apr 8 2020 04:37pm
Member
Posts: 9,595
Joined: Nov 24 2015
Gold: Locked
Apr 9 2020 02:05am
there are a lot of errors in your code.
first of all, you are not even calling your twoSum method and you are not doing anything with the returned result.
second mistake : you do i++ at the end of the for loop, but the for loop already does i++ so you're skipping an index every iteration
third mistake : to return an array of values, use return new int[] {1, i}
fourth mistake : your twosum function should be static
fifth mistake : if you had 6 in your numArray, it would return that index twice because you look for every result + itself
sixth mistake : I think compiler will not let you compile if you dont put a dummy return at the end of your function, but I don't have a java compiler so I can't test

This post was edited by Hyperdimension on Apr 9 2020 02:06am
Member
Posts: 31,358
Joined: Mar 25 2009
Gold: 15.00
Apr 9 2020 03:17pm
Quote (Hyperdimension @ Apr 9 2020 03:05am)
there are a lot of errors in your code.
first of all, you are not even calling your twoSum method and you are not doing anything with the returned result.
second mistake : you do i++ at the end of the for loop, but the for loop already does i++ so you're skipping an index every iteration
third mistake : to return an array of values, use return new int[] {1, i}
fourth mistake : your twosum function should be static
fifth mistake : if you had 6 in your numArray, it would return that index twice because you look for every result + itself
sixth mistake : I think compiler will not let you compile if you dont put a dummy return at the end of your function, but I don't have a java compiler so I can't test


Yeah thanks, it wasn't complete code yet, just had 1 specific question.... But i actually got some modified code before i read your answer......






















Code
package com.company;

import java.util.Arrays;
import java.util.Scanner;

public class TwoNumbers {


public static void main(String[] args) {

int[] numArray = {4, 5, 7, 9, 3};
int target = 8;


int[] sumArray = twoSum(numArray, target);
System.out.println(Arrays.toString(sumArray));



}





public static int[] twoSum(int[] nums, int target) {

for(int i=0; i<nums.length; i++) {
if(nums[0] + nums[i] == target) {
return new int[] {0, i};
} else if(nums[1] + nums[i] == target) {
return new int[] {1, i};
} else if(nums[2] + nums[i] == target) {
return new int[] {2, i};
} else if(nums[3] + nums[i] == target) {
return new int[] {3, i};
} else if(nums[4] + nums[i] == target) {
return new int[] {4, i};
}
}
return new int[0];

}

}




Granted still has some issues but i'm not done yet either.... actually doing something new now....



Code
package com.company;

import java.util.Arrays;

public class AnyArrayIndexNumbers {


public static void main(String[] args) {



int[] numArray = {4, 5, 7, 9, 3};
int target = 12;


int[] sumArray = twoSum(numArray, target);
System.out.println(Arrays.toString(sumArray));






}


public static int[] twoSum(int[] nums, int target) {

for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums.length; j++) {
if(nums[i] + nums[j] == target) {
return new int[] {i, j};
}
}

}

return new int[0];
}
}






Fixing a bug with this code atm
Member
Posts: 9,595
Joined: Nov 24 2015
Gold: Locked
Apr 9 2020 03:47pm
what's your new bug? The new code is better but you're still verifying twice the same index, do you really want that? what if the same number appears multiple time in your sequence?

This post was edited by Hyperdimension on Apr 9 2020 03:47pm
Member
Posts: 31,358
Joined: Mar 25 2009
Gold: 15.00
Apr 9 2020 04:06pm
Quote (Hyperdimension @ Apr 9 2020 04:47pm)
what's your new bug? The new code is better but you're still verifying twice the same index, do you really want that? what if the same number appears multiple time in your sequence?


that's the bug


j = 1+i

^fixed it :)
Member
Posts: 12,703
Joined: May 17 2013
Gold: 12,935.00
Apr 10 2020 06:23am
I don't understand why you keep attempting to do programming if you don't put in the effort to learn the basics.

It's a waste of your time
Member
Posts: 31,358
Joined: Mar 25 2009
Gold: 15.00
Apr 10 2020 08:10am
Quote (Klexmoo @ Apr 10 2020 07:23am)
I don't understand why you keep attempting to do programming if you don't put in the effort to learn the basics.

It's a waste of your time


These are basics....and i am putting in effort.
Member
Posts: 12,703
Joined: May 17 2013
Gold: 12,935.00
Apr 10 2020 08:36am
Quote (ferf @ 10 Apr 2020 16:10)
These are basics....and i am putting in effort.


https://forums.d2jsp.org/topic.php?t=73306930&f=124

learning the basics does not take almost 5 years with any effort at all

This post was edited by Klexmoo on Apr 10 2020 08:36am
Member
Posts: 31,358
Joined: Mar 25 2009
Gold: 15.00
Apr 10 2020 10:00am
Quote (Klexmoo @ Apr 10 2020 09:36am)
https://forums.d2jsp.org/topic.php?t=73306930&f=124

learning the basics does not take almost 5 years with any effort at all


ehhh i quit a lot and took things slow cuz it was very daunting.... but i'm at a point where it's not nearly as daunting, so i should be moving forward much faster from here on out
:)
Member
Posts: 1,204
Joined: Jan 3 2009
Gold: 1,772.00
Apr 11 2020 04:49am
Quote (ferf @ 10 Apr 2020 17:00)
ehhh i quit a lot and took things slow cuz it was very daunting.... but i'm at a point where it's not nearly as daunting, so i should be moving forward much faster from here on out
:)



Don't let them discourage you mate. If you find Java daunting, try out Python. I started with Python to learn the basics. It's less verbose and more "natural" or "English" like and more suitable for little scripts like this.
Honestly it's a big mistake to listen to people on the internet. One of my teachers at uni was researching computer science teaching methods. The biggest issue is that people that know how to code see certain things as obvious, while beginners struggle. That's why some people on the internet will tell you that something is obvious, while it might not be the case for a beginner.
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll