Results 1 to 6 of 6
  1. #1
    redasu is offline Member
    Join Date
    Nov 2008
    Posts
    9
    Rep Power
    0

    Default 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
    Exception in thread "main" java.util.NoSuchElementException
    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 ;}}
    }
    }
    Last edited by redasu; 11-08-2008 at 05:53 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    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.

    Exception in thread "main" java.util.NoSuchElementException
    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.
    Last edited by Norm; 11-08-2008 at 04:26 PM.

  3. #3
    redasu is offline Member
    Join Date
    Nov 2008
    Posts
    9
    Rep Power
    0

    Default 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.

  4. #4
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    All the exceptions are extended exceptions from the Exception class,so you can use Exception class to be thrown.

  5. #5
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    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.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    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.

Similar Threads

  1. Help me out in compiling the source code
    By aks.nitw in forum Advanced Java
    Replies: 3
    Last Post: 10-17-2008, 08:33 AM
  2. Error in compiling Firewall source code
    By ajib85 in forum Networking
    Replies: 8
    Last Post: 10-02-2008, 04:37 AM
  3. Trouble compiling code
    By waelhelbawi in forum New To Java
    Replies: 1
    Last Post: 05-12-2008, 04:25 AM
  4. Compiling and running code in runtime
    By tim in forum New To Java
    Replies: 4
    Last Post: 01-27-2008, 06:58 PM
  5. Error while compiling
    By ai_2007 in forum Advanced Java
    Replies: 1
    Last Post: 07-01-2007, 11:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •