Prompts for and reads in a line of input

Assignment Help Basic Computer Science
Reference no: EM131961529

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

}

}

Reference no: EM131961529

Questions Cloud

Identify the work and the artist or writer : Identify the work and the artist or writer, describe its features and style, and explain the manner in which it responds to the Industrial Revolution.
The promotion of eduss acquisition : Topics for Research and Analysis Promotional Goals: Identify specific objectives related to the promotion of EDUS's acquisition of the institution.
Describe how successful the author was in convincing you : Describe how successful the author was in convincing you to accept the validity of the "surprise ending" that was different from what you expected.
Explain person life in terms of nature-nurture influences : Explain the person's life in terms of nature/nurture influences. Provide an analysis of the role cognitive, physical, and social-emotional development/changes.
Prompts for and reads in a line of input : File ParseInts.java contains a program that does the following:
Prints the number of occurrences : File CountLetters.java contains a program that reads a word from the user and prints the number of occurrences of each letter in the word.
Describe in detail political developments in russia : Describe in detail political developments in Russia from 1991 to present. Conclude with your opinion whether democracy is likely in Russia in the near future.
Write response on the given case : Provide a substantive contribution that advances the discussion in a meaningful way by identifying strengths of the posting, challenging assumptions.
What would you suggest to that company in order to improve : In the case of the Coyote and Road Runner, one might question the ethics of the two characters' behaviors on many fronts.

Reviews

Write a Review

Basic Computer Science Questions & Answers

  Identifies the cost of computer

identifies the cost of computer components to configure a computer system (including all peripheral devices where needed) for use in one of the following four situations:

  Input devices

Compare how the gestures data is generated and represented for interpretation in each of the following input devices. In your comparison, consider the data formats (radio waves, electrical signal, sound, etc.), device drivers, operating systems suppo..

  Cores on computer systems

Assignment : Cores on Computer Systems:  Differentiate between multiprocessor systems and many-core systems in terms of power efficiency, cost benefit analysis, instructions processing efficiency, and packaging form factors.

  Prepare an annual budget in an excel spreadsheet

Prepare working solutions in Excel that will manage the annual budget

  Write a research paper in relation to a software design

Research paper in relation to a Software Design related topic

  Describe the forest, domain, ou, and trust configuration

Describe the forest, domain, OU, and trust configuration for Bluesky. Include a chart or diagram of the current configuration. Currently Bluesky has a single domain and default OU structure.

  Construct a truth table for the boolean expression

Construct a truth table for the Boolean expressions ABC + A'B'C' ABC + AB'C' + A'B'C' A(BC' + B'C)

  Evaluate the cost of materials

Evaluate the cost of materials

  The marie simulator

Depending on how comfortable you are with using the MARIE simulator after reading

  What is the main advantage of using master pages

What is the main advantage of using master pages. Explain the purpose and advantage of using styles.

  Describe the three fundamental models of distributed systems

Explain the two approaches to packet delivery by the network layer in Distributed Systems. Describe the three fundamental models of Distributed Systems

  Distinguish between caching and buffering

Distinguish between caching and buffering The failure model defines the ways in which failure may occur in order to provide an understanding of the effects of failure. Give one type of failure with a brief description of the failure

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd