Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Computer Engineering Expert

File ParseInts.java contains a program that does the following:

§ Prompts for and reads in a line of input

§ Uses a second Scanner to take the input line one token at a time and parses an integer from each token as it is extracted.

§ Sums the integers.

§ Prints the sum.

 

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 go further 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. As follows:

§ 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.

§ 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.

// ****************************************************************

// 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);

}

}

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M92799186
  • Price:- $10

Priced at Now at $10, Verified Solution

Have any Question?


Related Questions in Computer Engineering

In powerpoint what is beneficial about the usage or video

In Powerpoint, what is beneficial about the usage or Video, Audio, Animation, and Photos with Effects during presentation and work. What are some useful tools you found that would improve performance?

Question as we have seen traditional and agile schedule

Question : As we have seen, traditional and Agile schedule developments are quite different. Agile project management was originally developed to do software development. Could a traditional process be successfully used ...

Suppose that we want to convert the small computer into a

Suppose that we want to convert the small computer into a 16- bit RISC type of processor (address and data buses are both 16-bits wide and each instruction is supposed to occupy 16 bits in the instruction memory). If the ...

Argue why wireshark should be outlawed and it is a clear

Argue why Wireshark should be outlawed and it is a clear violation of privacy act and then argue why it should not be out outlawed and that it is a vital resource to be freely used and does not violate the privacy act

You have a deck of magic the gathering cards with 60 cards

You have a deck of Magic the Gathering cards with 60 cards total. In the deck you have 20 Mana cards, 18 Spells, 3 Artifacts, 12 Creatures, 6 Enchantments and 1 Planes Walker. What is the probability that you draw 2 arti ...

Question sql is often presented a a single database

Question : SQL is often presented a a single database language, but some contend that it is a set of languages. Which is it, why do you say so?, Why is that important for you to understand? Support your answers. The resp ...

You have an opportunity to buy a bond with a face value of

You have an opportunity to buy a bond with a face value of $10,000 and coupon rate of 14%, payable semi-annually. NOTE: Interest per 6-month period is 7% of Face Value (i.e. $10,000x0.07 = $700 per 6-month period).  (i) ...

Question write a minimum of 100 words for each question

Question: Write a minimum of 100 words for each question. Provide Citation and reference for each question. Provide two responses for each question. 1. Let us suppose we are designing a computer for use for a graphic art ...

Display the employee titles whose department is located in

Display the employee titles whose department is located in a secret cave.

The equation of regression line for sons heighty in inches

The equation of regression line for son's height(y) in inches versus father's height in inches is y = 35 + 0.5x. For a 72 inch tall father, what would we predict for the son's height?

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As