# I am receiving an error while the code is perfectly compiling

• 11-08-2008, 06:00 AM
redasu
I am receiving an error while the code is perfectly compiling
Hi I new to the forum so nice to discuss with you everybody
I am trying to use recursive functions for this assignment it compiles pretty well but it gives me this kind of output:
The minimum number is -5.000
The sum of the numbers at even indexes is -13.43
The sum of the negative numbers is -15.867
The total number of positive numbers is 3
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextDouble(Scanner.java:2387)
at Assignment6.main(Assignment6.java:21)
what's this exception thing??? plz help
here is my code:

import java.text.DecimalFormat;

import java.util.Scanner;

public class Assignment6
{
public static void main (String[] args)
{

DecimalFormat form1 = new DecimalFormat("0.000");
DecimalFormat form2 = new DecimalFormat("0.##");
DecimalFormat form3 = new DecimalFormat("0.0##");

int index;

Scanner scan = new Scanner( System.in );
double [] number = new double[101];
for(index=0;index < number.length;index++)
{

number[index] = scan.nextDouble( );
if(number[index]==0)
{
System.out.println("The minimum number is "+form1.format(findMin(number,index)));
System.out.println("The sum of the numbers at even indexes is "+form2.format(computeSumAtEven(number,index)) );
System.out.println("The sum of the negative numbers is "+form3.format(computeNegativeSum(number,index )));
System.out.println("The total number of positive numbers is "+countPositive(number,index));
}
}

}
public static double findMin(double[] numbers, int i)
{
double min;
if (i==0){
return numbers[0];}
else {
min = findMin (numbers , i-1);
if (numbers[i]< min )
{ return numbers[i];}
else {
return min;}
}}
public static double computeSumAtEven(double[] numbers, int i)
{

if (i==0){
return numbers[0];}
else {
double sum = computeSumAtEven(numbers, i-1);
if (i%2==0){
return sum + numbers[i];}
else
{return sum; }}

}
public static double computeNegativeSum(double[] numbers, int i)
{
double sum;
if (i==0){
if (numbers[i]<0)
{sum = numbers[0];
return sum;}
else {
sum=0;
return sum;}
}
else
{ sum = computeNegativeSum(numbers, i-1);
if (numbers[i]< 0)
{return sum + numbers[i];}
else {
return sum;}
}
}

public static int countPositive(double[] numbers , int i)
{if(i==0)
{ if(numbers[0]>0)
return 1 ;
else
return 0;}
else
{
int sum = countPositive(numbers,i-1);

if (numbers[i]>0)
{ sum++;
return sum;}

else{
return sum ;}}
}
}
• 11-08-2008, 03:18 PM
Norm
Quote:

an error while the code is perfectly compiling
Yes, runtime is different from compile time. We need better compilers that will tell us when we haven't designed our program properly and suggest corrections so that it will do what we want it to. The problem is that then we would be required to put comments in our code describing what we want the code to do. Many students find adding comments very difficult, if not impossible.

Quote:

at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextDouble(Scanner.java:2387)
at Assignment6.main(Assignment6.java:21)
The nextDouble() method is NOT finding a double and is telling you about it. See line 21 in your code
Read the API doc for the nextDouble method and see what exceptions it throws and why.

In your code, instead of assuming that the correct data is being input by the user, you need to be prepared that the user enters bad data. This is done by using a try{}catch() {} block around the code where the error can occur.
• 11-08-2008, 09:06 PM
redasu
exception
We never went through exceptions yet I tried to read the Api but I still don't know what kind of exception should I put.
• 11-08-2008, 10:07 PM
serjant
All the exceptions are extended exceptions from the Exception class,so you can use Exception class to be thrown.
• 11-09-2008, 01:45 PM
Nicholas Jordan
In general, do if( condition within limits ){doWork();}else{System.out.println)"read manual";return;} and wrap all that in try{}catch(){}

Further, do not program for idiots, that leads to "except if the exception is an exceptional exception" which gains little code value and is resolved by considering the following:

person hits wrong key == error.

computer repo'd by Wall Street == exceptional condition.
• 11-09-2008, 02:58 PM
Norm
Quote:

do not program for idiots,
That will restrict the jobs you can get or who can use your programs.
I'd change that to: Always program as if the user is an idiot.
Do NOT assume that they know what to do.