0

Everything outputs exactly as it should in the program and in eclipse, no errors whatsoever but then it gives a few errors on ASU submission site.

//  Description: This class reads doubles from the keyboard, logs them to an array
//                  and finds the lowest value in the array along with the sum of
//                  all positive numbers and the count of negative numbers.

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Scanner;

public class Assignment2 {

    public static void main(String[] args) { //Main class, takes care of beginning and end operations
        NumberFormat single = new DecimalFormat("###,###,###,###,##0"); //These alter the outputs
        NumberFormat $ = new DecimalFormat("$###,###,###,###,##0.00"); //later in the code
        double[] nums = new double[100];
        int count = 0;
        @SuppressWarnings("resource") //Eclipse gives an error without this
        Scanner scan = new Scanner(System.in);
        while(nums[count] < 100){
                //have a count to keep track of input numbers
                //input values assigned to nums
                for(int i = 0; i < 100; i++) {
                    double temp = scan.nextDouble();
                    nums[i] = temp;
                    if (nums[i] == 0){
                        break;
                    }
            }
            double min = findMin(nums, count);
            double positive = computePositiveSum(nums, count);
            int negative = countNegative(nums, count);
            System.out.println("The minimum number is " + single.format(min));
            System.out.println("The sum of the positive numbers is " + $.format(positive));
            System.out.println("The total number of negative numbers is " + single.format(negative));
        }

This is the main method, the 3 listed aren't important because the error is on line 27, the double temp = scan.nextDouble(); I realize it's unnecessary but I used thsat to see if it would fix the problem because nums[i] = scan.nextDouble(); had the error as well. The submission site results are as follows:

Grade

Assignment2

Compilation

2 pts. - Passed
Functionality

Test #1

Program Output
The minimum number is 0
The sum of the positive numbers is $0.00
The total number of negative numbers is 0

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 Assignment2.main(Assignment2.java:27)

Expected Output
The minimum number is 0
The sum of the positive numbers is $0.00
The total number of negative numbers is 0

0 pts. - Failure

Can anyone help me understand what I'm doing wrong? Also any criticism on my coding practices are welcome, I'm a freshman and I'm having a hard time learning java so if I can have perfect "grammar", so to speak, I'll take it. Thanks in advance!

Steinar Lima
  • 7,383
  • 2
  • 36
  • 40
Captain Hook
  • 71
  • 2
  • 2
  • 9
  • 2
    Comment on coding practice: Don't use `$` as a variable name. Don't use `$` in variable names at all, because the Java compiler sometimes generates names with `$` in them. – ajb Jan 25 '14 at 02:17
  • I think your program able to pass the test #1. So my doubt is that there is a line break or something between each test. – RP- Jan 25 '14 at 02:20
  • `NoSuchElementException` means that your program called `nextDouble` when there was nothing left in the input. I don't know how the program is being run at ASU or where it's getting the input from, but your program is probably not forgiving enough if the input doesn't look like you expect. You may want to use [`hasNextDouble`](http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#hasNextDouble()). – ajb Jan 25 '14 at 02:20
  • 2
    The reason `@SuppressWarnings("resource")` is necessary in your code is because you're not closing your scanner instance. Typically closing is done after your use of it is over; so after your while loop you need to call `scan.close()`. Often closing is done in a `finally{}` block to ensure the statement runs. For more on `finally{}` blocks check out this thread: http://stackoverflow.com/questions/3421486/why-do-we-use-finally-blocks – Durandal Jan 25 '14 at 02:21
  • 1
    If you get an exception that you don't understand, you should refer to the API documentation to see what circumstances cause that exception to be emitted. The Java documentation is really quite good, it'll answer most questions if you bother to use it. – Jon Kiparsky Jan 25 '14 at 02:38
  • All of your comments were very helpful actually, it was a combination of the errors I made and a superfluous while loop that caused it. Thanks for all the tips! – Captain Hook Jan 30 '14 at 20:56

0 Answers0