Java: An Introduction to Problem Solving and Programming (8th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
8th Edition
ISBN: 9780134462035
Author: Walter Savitch
Publisher: PEARSON
bartleby

Concept explainers

Expert Solution & Answer
Book Icon
Chapter 9, Problem 3P

Explanation of Solution

Given program:

The given code segment is highlighted.

//Import required package

import java.util.*;

//Define the class

public class Main

{

    //Define the main method

    public static void main(String[] args)

    {

        //Declare a variable

        int num = 0;

        //Create an object for Scanner class

        Scanner kbd = new Scanner(System...

Explanation of Solution

Wrapping the code inside try-catch block:

The modified code segment is highlighted.

//Import required package

import java.util.*;

//Define the class

public class Main

{

    //Function

    static void fun()

    {

        //Declare a variable

        int num = 0;

         //Create an object for Scanner class

        Scanner kbd = new Scanner(System.in);

        //Do until true

        while(true)

        {

            //Try block

            try

            {

                  //Get the number from the user

System.out.println ("Please enter a number: ");

                num = kbd.nextInt();

                //Print the number

System.out...

Blurred answer
Students have asked these similar questions
The following snippet of code inputs an integer and outputs it: int num=0; Scanner kbd = new Scanner(System.in); num = kbd.nextInt(); System.out.println(num); Run this program but enter text instead of an integer. The program should crash and tell you what kind of exception was thrown by the nextInt method. Wrap this code inside a try/catch block where you catch the exception that is thrown. Add a loop so the user must enter the number again if text is entered
If an exception is raised and the program does not handle it with a try/exceptstatement, what happens?
. The TryParse methods throw an exception if the string argument cannot be converted

Chapter 9 Solutions

Java: An Introduction to Problem Solving and Programming (8th Edition)

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
  • Text book image
    EBK JAVA PROGRAMMING
    Computer Science
    ISBN:9781337671385
    Author:FARRELL
    Publisher:CENGAGE LEARNING - CONSIGNMENT
    Text book image
    Microsoft Visual C#
    Computer Science
    ISBN:9781337102100
    Author:Joyce, Farrell.
    Publisher:Cengage Learning,
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,