SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Stuart Reges, Marty Stepp
Chapter:
File Processing
Exercise:
Exercises
Question:3 | ISBN:9780136091813 | Edition: 2

Question

Write a method called negativeSum that accepts a Scanner reading input from a file containing a series of integers, and print a message to the console indicating whether the sum starting from the first number is ever negative.
You should also return true if a negative sum can be reached and false if not. For example, suppose the file contains the following text:
38 4 19 -27 -15 -3 4 19 38
Your method would consider the sum of just one number (38), the first two numbers (38 + 4), the first three numbers (38 +4 +19), and so on to the end. None of these sums is negative, so the method would produce the following output and return false:
no negative sum
If the file instead contains the following numbers:
14 7 -10 9 -18 -10 17 42 98
The method finds that a negative sum of 8 is reached after adding the first six numbers. It should output the
following to the console and return true:
sum of -8 after 6 steps

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer



import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class NegativeSum {
    
    
    public boolean negativeSum() throws FileNotFoundException {
        
        // reads the input from Integers.txt file
        File file = new File("${basepath}//Integers.txt");
        
        try (// send file as an argument to the Scanner constructor
        Scanner input = new Scanner(file)) {
            int sum = 0;
            int steps = 0;
            
            
            while(input.hasNext()) {
                sum += input.nextInt();
                steps++;
                
                if(sum < 0) {
                    System.out.println(sum + " after " + steps + " steps");
                    return true;
                }
            }
        }
        System.out.println("no negative sum");
        
       
        return false;
        
    }

    // below method throws FileNotFoundException
    public static void main(String[] args) throws FileNotFoundException {

        NegativeSum ns = new NegativeSum();
        System.out.println(ns.negativeSum());
        
        
    }

}
Integers.txt file contains

25 -23 31 -32 12 -87 24 65 47 -23 19 -35 -1
Output:

-74 after 6 steps
true

 

0 0

Discussions

Post the discussion to improve the above solution.