Reputation: 1
I am writting a program that takes input using a series of numbers. Using recursion and Arrays I am trying to add all the numbers entered that are divisible by 3 and add them together. Ex. 3 4 5 6 Output should be 9. My current output keeps giving me 3 as the output for my entries. Any help or suggestions?
import java.io.*;
import java.text.*;
public class Assignment9 {
public static void main (String args[]) throws IOException{
int i = 0;
int [] nums;
nums = new int [100];
InputStreamReader inRead = new InputStreamReader(System.in);
BufferedReader buffRead = new BufferedReader(inRead);
String line = buffRead.readLine();
try {
while (line.equals("0") == false && i<100) {
i++;
line = buffRead.readLine();
nums[i]=(int) Double.parseDouble(line);
}
} catch(IOException e) {
System.out.println("Array index out of bound");
}
int endIndex = computeSumDivisibleBy3(nums, 0, nums.length-1);
System.out.print ("The minimum number is " + min + ('\n'));
System.out.print ("The sum of the numbers divisible by 3 is " + endIndex + ('\n'));
}
}
public static int computeSumDivisibleBy3(int [] numbers, int startIndex, int endIndex) {
if(startIndex == endIndex) {
if(numbers[endIndex] %3 == 0){
return (int) numbers[endIndex];
} else {
return 0;
}
} else {
if(numbers[endIndex] %3 == 0) {
return (int) (computeSumDivisibleBy3(numbers, startIndex, endIndex - 1) + numbers
}
else {
return computeSumDivisibleBy3(numbers, startIndex, endIndex - 1);
}
}
}
Upvotes: 0
Views: 3722
Reputation: 31
... String line = buffRead.readLine();
try {
while (line.equals("0") == false && i < 100) {
nums[i] = Double.parseDouble(line);
i++;
line = buffRead.readLine();
}
} catch (IOException e) {
System.out.println("Array index out of bound");
}
int sum = computeSumDivisibleBy3(nums);
System.out.print("The sum of the numbers divisible by 3 is " + sum + ('\n'));
.....
public static int computeSumDivisibleBy3(double[] numbers) {
return _computeSumDivisibleBy3(numbers, 0);
}
private static int _computeSumDivisibleBy3(double[] numbers, int currentIndex ) {
int sum = 0;
if (currentIndex != numbers.length) {
int currentNumber = (int)numbers[currentIndex];
sum = (currentNumber % 3) == 0 ? currentNumber : 0;
sum += _computeSumDivisibleBy3(numbers, ++currentIndex );
}
return sum;
}
Upvotes: 0
Reputation: 1897
Ok a few things here:
1) A number n is divisible by three if: n%3 == 0
not n%3 == 1
2) When you check to see if a number is divisible by 3, you are checking if the INDEX is divisible by 3, not the actually number in the array (use numbers[endIndex])
Sort those two things out and it should work. This seems like homework, so I'm wary to just give you the correct code rather than letting you work through it and understand it.
Once you get it working, I have two recommendations:
1) You should use an int[] instead of double[] for the array. Any number that isn't an integer won't cleanly divide by 3. When you are reading the file and adding to the array of numbers, use n%1==0
in an if statement to check if the number read is in fact an integer and should be added to your array. This will reduce the amount of recursive calls as the array will be potentially shorter assuming that you were able to dispose of some non integer values.
2) You can make a recursive method with just two parameters, the array of ints and one index. Probably not necessary, but saves you the headache of worrying about passing indices. HINT: startIndex never gets changed, your base case can be improved by knowing the length of the array.
Please feel free to ask me more questions in the comments if you want clarification/more hints.
Upvotes: 1