Question

please write JAVA code: Your Own Exception Class Write a text-based (non-GUI) program to read in...

please write JAVA code:

Your Own Exception Class

Write a text-based (non-GUI) program to read in file that contains only positive numbers and outputs to the screen the sum of all numbers.

Create your own exception class that describes the situation of a non-positive number. In your program, use this exception to handle this situation. Use other exception classes from the Java standard library to handle other I/O situations.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// Numbers.java

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class Numbers {

              public static void main(String[] args) {

                           //defining scanner to read user input

                           Scanner scanner = new Scanner(System.in);

                           //prompting and getting file name

                           System.out.print("Enter file name: ");

                           String filename = scanner.nextLine();

                           try {

                                         //reinitializing scanner to read from file

                                         scanner = new Scanner(new File(filename));

                                         //initializing sum to 0

                                         int sum = 0;

                                         //looping until the end of file

                                         while (scanner.hasNextInt()) {

                                                       //reading integer

                                                       int n = scanner.nextInt();

                                                       //checking if n is negative

                                                       if (n < 0) {

                                                                    //throwing negative number exception

                                                                    throw new NegativeNumberException("Negative number found: "

                                                                                                + n);

                                                       }

                                                       //adding n to sum

                                                       sum += n;

                                         }

                                         //closing scanner

                                         scanner.close();

                                         //displaying sum

                                         System.out.println("Sum of all numbers is " + sum);

                           } catch (FileNotFoundException e) {

                                         //file not found

                                         System.err.println(e.getMessage());

                           } catch (NegativeNumberException e) {

                                         //negative number found in the file

                                         System.err.println(e.getMessage());

                           }

              }

}

/**

* class to represent an Exception when a negative value is found

*/

class NegativeNumberException extends Exception {

              public NegativeNumberException(String str) {

                           //passing error message to super class constructor

                           super(str);

              }

}

//OUTPUT when file does not exist

Enter file name: abc

abc (The system cannot find the file specified)

//OUTPUT when file exists and contains numbers 1 2 3 4 5 6 7 8

Enter file name: input.txt

Sum of all numbers is 36

//OUTPUT when file exists and contains numbers 1 2 3 4 5 -6 7 8

Enter file name: input.txt

Negative number found: -6

Add a comment
Know the answer?
Add Answer to:
please write JAVA code: Your Own Exception Class Write a text-based (non-GUI) program to read in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA Problem Description: For this assignment, you will be writing your own exception class and you...

    JAVA Problem Description: For this assignment, you will be writing your own exception class and you will handle run-time exceptions resulting from one particular input situation. Specifics: 1) Design and implement a program that has an exception class called StringTooLongException, designed to be thrown when a string is discovered that has too many characters in it. 2) In the main driver of the program (call this class MyExceptionTest), read strings from the user until the user enters “DONE”. If a...

  • i need the sloution with java code please :) Using your Queue class write a GUI...

    i need the sloution with java code please :) Using your Queue class write a GUI program that opens a file contains integers in the range [0 .. 999] "in.txt". The program stops reading if -1 is read. Your program should use queues to make the output such that the first textfield output contains the integers in the range 0..9 but in their same order as in the input, the same for the second textfield but in the range 10..19,...

  • (use java) Write a program that creates an exception class called StringTooLongException, designed tobe thrown when...

    (use java) Write a program that creates an exception class called StringTooLongException, designed tobe thrown when a string is discoveredthat has too many characters in it. In the main driver of the program, read strings from the user until the user enters “DONE”. If a string is entered that has too many characters (say 20), throw, catch, and handle the exception. The exception is handled by printing an appropriate message, and the programwill continueprocessing more strings.

  • (Java) Rewrite the following exercise below to read inputs from a file and write the output...

    (Java) Rewrite the following exercise below to read inputs from a file and write the output of your program in a text file. Ask the user to enter the input filename. Use try-catch when reading the file. Ask the user to enter a text file name to write the output in it. You may use the try-with-resources syntax. An example to get an idea but you need to have your own design: try ( // Create input files Scanner input...

  • 4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java...

    4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java source code file named H01_43.java (your main class is named H01_43). Problem: Write a program that prompts the user for the name of a Java source code file (you may assume the file contains Java source code and has a .java filename extension; we will not test your program on non-Java source code files). The program shall read the source code file and output...

  • please answer correctly and follow the code structure given [JavaFX/ Exception handing and text I/O] 1....

    please answer correctly and follow the code structure given [JavaFX/ Exception handing and text I/O] 1. Write a program for the following. NOTE that some of these steps are not dependent on each other. Using methods is mandatory. Make sure to use methods where it makes sense. a. Ask the user for a series of integers entered from the keyboard. Use a sentinel value such as 999 to end the input process. If the entered values are not integers, throw...

  • In Java. Write a program in a single file that: Main: Creates 10 random doubles, all...

    In Java. Write a program in a single file that: Main: Creates 10 random doubles, all between 1 and 11, Calls a class that writes 10 random doubles to a text file, one number per line. Calls a class that reads the text file and displays all the doubles and their sum accurate to two decimal places. There has to be three classes, main for create random numbers, one to write , one to read all in the same file...

  • What to submit: your answers to exercises 2. Write a Java program to perform the following...

    What to submit: your answers to exercises 2. Write a Java program to perform the following tasks: The program should ask the user for the name of an input file and the name of an output file. It should then open the input file as a text file (if the input file does not exist it should throw an exception) and read the contents line by line. It should also open the output file as a text file and write...

  • Java using data structures The objective is to create your own Hash Table class to hold...

    Java using data structures The objective is to create your own Hash Table class to hold a list of employees and their ID numbers. I've provided the TableEntry class which will be each data object in the hash table. The list of employees will be provided as a .txt file and must be read with the code. please create a .txt file called Employees.txt with the info provided so that the java code can read it in. Employees.txt: (No WhiteSpace...

  • Write a program that reverses a text file by using a stack. The user interface must...

    Write a program that reverses a text file by using a stack. The user interface must consist of 2 list boxes and 3 buttons. The 3 buttons are: Read - reads the text file into list box 1. Reverse - reverses the items in list box 1 by pushing them onto stack 1, then popping them from stack 1 (in the reverse order) and adding them to list box 2. Write - writes the contents of list box 2 to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT