File ParseInts.java contains a program that does the following:
Save ParseInts to your directory and compile and run it. If you give it the input
10 20 30 40
it should print
The sum of the integers on the line is 100.
Try some other inputs as well. Now try a line that contains both integers and other values, e.g.,
We have 2 dogs and 1 cat.
You should get a NumberFormatException when it tries to call Integer.parseInt on “We”, which is not an
integer. One way around this is to put the loop that reads inside a try and catch the NumberFormatException but not do anything with it. This way if it’s not an integer it doesn’t cause an error; it goes to the exception handler, which does nothing. Do this as follows:
// ****************************************************************
// ParseInts.java
//
// Reads a line of text and prints the integers in the line.
//
// ****************************************************************
import java.util.Scanner;
public class ParseInts
{
public static void main(String[] args)
{
int val, sum=0;
Scanner scan = new Scanner(System.in);
String line;
System.out.println("Enter a line of text");
Scanner scanLine = new Scanner(scan.nextLine());
while (scanLine.hasNext())
{
val = Integer.parseInt(scanLine.next());
sum += val;
}
System.out.println("The sum of the integers on this line is " +
sum);
}
}
//****************************************************************
//ParseInts.java
//
//Reads a line of text and prints the integers in the line.
//
//****************************************************************
import java.util.Scanner;
public class ParseInts {
public static void main(String[] args)
{
int val, sum=0;
Scanner scan = new
Scanner(System.in);
String line;
System.out.println("Enter a line of
text");
Scanner scanLine = new
Scanner(scan.nextLine());
while
(scanLine.hasNext())
{
val =
Integer.parseInt(scanLine.next());
sum +=
val;
}
System.out.println("The sum of the
integers on this line is " +
sum);
}
}
//end of program
Output:
//****************************************************************
//ParseInts.java
// Modification 1
//Reads a line of text and prints the integers in the line.
// Modified program to catch NumberFormatException
//****************************************************************
import java.util.Scanner;
public class ParseInts {
public static void main(String[] args)
{
int val, sum=0;
Scanner scan = new
Scanner(System.in);
String line;
System.out.println("Enter a line of
text");
Scanner scanLine = new
Scanner(scan.nextLine());
// try-catch block to catch the
NumberFormatException
// but now the process stops when
it encounters a non-integer data
// as the control reached the catch
block which is outside the while
try {
while
(scanLine.hasNext())
{
val =
Integer.parseInt(scanLine.next());
sum +=
val;
}
}catch(NumberFormatException
e)
{
}
System.out.println("The sum of the
integers on this line is " +
sum);
}
}
//end of program
Output:
//****************************************************************
//ParseInts.java
// Modification 2
//Reads a line of text and prints the integers in the line.
// Modified ParseInts to catch the NumberFormatException but continue processing until the entire line has been read
//****************************************************************
import java.util.Scanner;
public class ParseInts {
public static void main(String[] args)
{
int val, sum=0;
Scanner scan = new
Scanner(System.in);
String line;
System.out.println("Enter a line of
text");
Scanner scanLine = new
Scanner(scan.nextLine());
while (scanLine.hasNext())
{
// try-catch block to catch the
NumberFormatException
// when non-integer data is encountered, the
control goes
// to the catch block but processing continues
as the try-catch block is within the loop
// so the process continues till we reach end of
the input line
// and hence it calculates the sum of all
integers in the input line
try {
val =
Integer.parseInt(scanLine.next());
sum += val;
}catch(NumberFormatException e)
{
}
}
System.out.println("The sum of the
integers on this line is " +
sum);
}
}
//end of program
Output:
Placing Exception Handlers File ParseInts.java contains a program that does the following: Prompts for and reads...
Modify your program in Lab Assignment 4A to throw an exception if the file does not exist. An error message should result. Use the same file name in your program as 4A, however, use the new input file in 4B assignment dropbox. My code: import java.io.*; import java.util.*; public class Lab4B { public static void main(String[] args) throws IOException { final String input_file = "Lab_4A.txt"; final String output_file = "Lab_4A.txt"; Scanner x = null; FileWriter y = null; try {...
Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written to a file. Each line of the input file will contain the student name (a single String with no spaces), the number of semester hours earned (an integer), the total quality points earned (a double). The following shows part of a typical...
File Factorials.java contains a program that calls the factorial method of the MathUtils class to compute the factorials of integers entered by the user. In this program, two things can possibly occur: The program always returns 1 if negative integers are entered by the user. Returning 1 as the factorial of any negative integer is not correct. The program also returns negative numbers when the user enters integers greater than 16. Returning a negative number for values over 16 also...
Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file doesn’t exist. You will process through the file skipping any text or real (double) numbers. You will print the max, min, sum, count, and average of the integers in the file. You will want to create test files that contain integers, doubles, and Strings. HINT: Use hasNextInt() method and while loop. You may also want to use Integer.MAX_VALUE and Integer.MIN_VALUE for the initialization of...
Select the correct answer. (1) Which of the following will open a file named MyFile.txt and allow you to read data from it? (a) File file = new File("MyFile.txt"); (b) FileWriter inputFile = new FileWriter(); (c) File file = new File("MyFile.txt"); FileReader inputFile = new FileReader(file); (d) FileWriter inputFile = new FileWriter("MyFile.txt"); (2) How many times will the following do - while loop be executed? int x = 11; do { x...
import java.util.Scanner; import java.io.File; public class Exception2 { public static void main(String[] args) { int total = 0; int num = 0; File myFile = null; Scanner inputFile = null; myFile = new File("inFile.txt"); inputFile = new Scanner(myFile); while (inputFile.hasNext()) { num = inputFile.nextInt(); total += num; } System.out.println("The total value is " + total); } } /* In the first program, the Scanner may throw an...
In Java - Write a program that prompts the user to enter two integers and displays their sum. If the input is incorrect, prompt the user again. This means that you will have a try-catch inside a loop.
Question 1 (5 points) Question 1 Unsaved What is displayed on the console when running the following program? public class Quiz2B { public static void main(String[] args) { try { System.out.println("Welcome to Java"); int i = 0; int y = 2 / i; System.out.println("Welcome to Java"); } catch (RuntimeException ex) { System.out.println("Welcome to Java"); } finally { System.out.println("End of the block"); } } } Question 1 options: The program displays Welcome to Java two times. The program displays Welcome to...
Hi. This is a prototype of Java. The following Java program was developed for prototyping a mini calculator. Run the program and provide your feedback as the user/project sponsor. (Save the code as MiniCalculator.java; compile the file: javac MiniCalculator.java; and then run the program: java MiniCalculator). HINTs: May give feedback to the data type and operations handled by the program, the way the program to get numbers and operators, the way the calculation results are output, the termination of the...
Looking for some simple descriptive pseudocode for this short Java program (example italicized in bold directly below): //Create public class count public class Count { public static void main(String args[]) { int n = getInt("Please enter an integer value greater than or equal to 0"); System.out.println("Should count down to 1"); countDown(n); System.out.println(); System.out.println("Should count up from 1"); countUp(n); } private static void countUp(int n) {...