Java Programming Project Help! Thank you! When I run the given code, I get a Num
ID: 3800122 • Letter: J
Question
Java Programming Project Help! Thank you!
When I run the given code, I get a NumberFormatException when it tries to call Integer.parseInt on “We”, which is not an integer. I need to put a 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.
Modify the program to add a try statement that encompasses the entire while loop. The try and opening { should go before the while, and the catch after the loop body. Catch a NumberFormatException and have an empty body for the catch.
Code-
// ****************************************************************
// 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);
}
}
The rest of the assignment is asking for-
Compile and run the program and enter a line with mixed integers and other values. You should find that it stops summing at the first non-integer, so the line above will produce a sum of 0, and the line “1 fish 2 fish” will produce a sum of 1. This is because the entire loop is inside the try, so when an exception is thrown the loop is terminated. To make it continue, move the try and catch inside the loop. Now when an exception is thrown, the next statement is the next iteration of the loop, so the entire line is processed. The dogs-and-cats input should now give a sum of 3, as should the fish input.
Now modify the body of the catch so that it prints a useful message (e.g., “Not a number”). Run the program again.
Any help is appreciated! Thank you!
Explanation / Answer
ParseInts.java
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());
String s = "";
while (scanLine.hasNext())
{
try{
s = scanLine.next();
val = Integer.parseInt(s);
sum += val;
}
catch(NumberFormatException e){
System.out.println(s+ " Not a number");
}
}
System.out.println("The sum of the integers on this line is " +
sum);
}
}
Output:
Enter a line of text
1 fish 2 fish
fish Not a number
fish Not a number
The sum of the integers on this line is 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.