Question

Start with the code below and complete the getInt method. The method should prompt the user...

Start with the code below and complete the getInt method. The method should prompt the user to enter an integer. Scan the input the user types. If the input is not an int, throw an IOException; otherwise, return the int. In the main program, invoke the getInt method, use try-catch block to catch the IOException.

import java.util.*;

import java.io.*;

public class ReadInteger {

pubilc static void main() {

// your code goes here }

public static int getInt() throws IOException {

// your code goes here }

}

Homework Answers

Answer #1

CODE :

import java.util.*;

import java.io.*;
public class ReadInteger {

   public static void main(String[] args) {


      try {

         int a = getInt();
         System.out.println(a+" is an integer.");


      } catch (IOException e) {
         System.out.println("IOException called!");

      }

   }

   public static int getInt() throws IOException {

      Scanner sc= new Scanner(System.in);
      System.out.println("Enter an integer: ");

      if(sc.hasNextInt()){
         int num=sc.nextInt();
         return num;
      }

        else{
           throw new IOException("exception") ;

      }
   }

}

EXAMPLES:

1.

2.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write an application that asks a user to enter an integer. Display a statement that indicates...
Write an application that asks a user to enter an integer. Display a statement that indicates whether the integer is even or odd. ------------------------------------------ import java.util.Scanner; class EvenOdd {     public static void main(String[] args) {         // Write your code here     }     public static boolean isEven(int number) {     } }
Write the complete code necessary to prompt the user for 4 sequential numbers, calculate the summation...
Write the complete code necessary to prompt the user for 4 sequential numbers, calculate the summation of the 4 numbers and display the results including the words "The summation of 1,2,3,4 is". A summation is defined as n + n+1 + n+2.... Declare necessary variables and be sure to include comments. Include int main(void) { Code goes here } The programming language is C in Visual Basic
What is the minimum modification needed to make the code that follows compile? public void readData()...
What is the minimum modification needed to make the code that follows compile? public void readData() {     boolean dataNotFound = true;     if (dataNotFound) {         throw new IOException();     }     catch(IOException e) {         System.out.println("data file can't be found");     } } a. Place the if statement within a try block. b. Add a throws clause to the method declaration. c. Set dataNotFound to false. d. No modification is needed.
Analyze this code and run it, if there are any issues note them and fix them,...
Analyze this code and run it, if there are any issues note them and fix them, if not give the output and Big O notation runtime: public class PrintBits { public static void printBits(int a) { try { String str = Integer.toBinaryString((Integer) a); for(int i = 0; i < str.length(); i++){ System.out.print (str.charAt(i)); } } catch (ClassCastException e) { throw new RuntimeException ("Argument is not an Integer"); } } public static void main (String[] args){ printBits (-17); System.out.println(); printBits (17);...
Complete method printPopcornTime(), with int parameter bagOunces, and void return type. If bagOunces is less than...
Complete method printPopcornTime(), with int parameter bagOunces, and void return type. If bagOunces is less than 3, print "Too small". If greater than 10, print "Too large". Otherwise, compute and print 6 * bagOunces followed by "seconds". End with a newline. Example output for ounces = 7: 42 seconds import java.util.Scanner; public class PopcornTimer { public void printPopcornTime(int bagOunces) { /* Your solution goes here */ } public static void main (String [] args) { PopcornTimer popcornBag = new PopcornTimer();...
7.6 LAB: Exception handling to detect input String vs. Integer The given program reads a list...
7.6 LAB: Exception handling to detect input String vs. Integer The given program reads a list of single-word first names and ages (ending with -1), and outputs that list with the age incremented. The program fails and throws an exception if the second input on a line is a String rather than an Integer. At FIXME in the code, add a try/catch statement to catch java.util.InputMismatchException, and output 0 for the age. Ex: If the input is: Lee 18 Lua...
Hello, I am trying to create a Java program that reads a .txt file and outputs...
Hello, I am trying to create a Java program that reads a .txt file and outputs how many times the word "and" is used. I attempted modifying a code I had previously used for counting the total number of tokens, but now it is saying there is an input mismatch. Please help! My code is below: import java.util.*; import java.io.*; public class Hamlet2 { public static void main(String[] args) throws FileNotFoundException { File file = new File("hamlet.txt"); Scanner fileRead =...
/* This program should check if the given integer number is prime. Reminder, an integer number...
/* This program should check if the given integer number is prime. Reminder, an integer number greater than 1 is prime if it divisible only by itself and by 1. In other words a prime number divided by any other natural number (besides 1 and itself) will have a non-zero remainder. Your task: Write a method called checkPrime(n) that will take an integer greater than 1 as an input, and return true if that integer is prime; otherwise, it should...
Here is my java code, I keep getting this error and I do not know how...
Here is my java code, I keep getting this error and I do not know how to fix it: PigLatin.java:3: error: class Main is public, should be declared in a file named Main.java public class Main { ^ import java.io.*; public class Main { private static BufferedReader buf = new BufferedReader( new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { String english = getString(); String translated = translate(english); System.out.println(translated); } private static String translate(String s) { String latin =...
What is the output of the following code segment? public class Exception { ... static int...
What is the output of the following code segment? public class Exception { ... static int divider(int x, int y) { try { return x/y; } catch (ArrayIndexOutOfBoundsException a) { System.out.print("A"); return 1; } } static int computer(int x, int y) { try { return divider(x,y); } catch (NullPointerException b) { System.out.print("B"); } return 2; } public static void main(String args[]){ try { int i = computer (100, 0); } catch (ArithmeticException c) { System.out.print("C"); } } } ABC A...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT