Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Projects: Concepts Tested in this Program : Error Handling Text I/O Project

ID: 3699184 • Letter: J

Question

Java Projects:

Concepts Tested in this Program:

Error Handling

Text I/O

Project 12 consists of three small programs:

Program 12_1:  

Write a program that prompts the user to read two integers and displays their sum . If anything but an integer is passed as input, your program should catch the InputMismatchException that is thrown and prompt the user to input another number by printing "Please enter an integer ."

Name your program’s file as InputMismatch.java

SAMPLE RUN #1: java InputMismatch

Program 12_2:  

Write the bin2Dec (String binaryString) method to convert a binary string into a decimal number (your method should return an integer ). Implement the bin2Dec method to throw a NumberFormatException if the string is not a binary string.

Test your method in a program that prompts the user to input a binary string and uses your bin2Dec method to print out the decimal equivalent of that string. If the method throws an error, have your program catch it and print "Not a binary string." before terminating.

Name your program’s file as NumberFormat.java

Program 12_3:  

Write a program that reads lines from a file and prints them out, removing all occurrences of a specified string from the lines.

For example, if the class that housed your program was called RmvText, running this prompt on the command line:

java RmvText John filename

Would print out the contents of the file filename without any instances of the string John.

Your program should get the arguments from the command line.

Name your program’s file as RmvText.java

SAMPLE RUN #1: java RmvText going input

Explanation / Answer

InputMismatch.java


import java.util.InputMismatchException;
import java.util.Scanner;

public class InputMismatch {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int firstNum, secondNum=0;
while(true){
try{
System.out.println("Enter·an·integer: ");
firstNum = scan.nextInt();
break;
}
catch(InputMismatchException e){
System.out.println("Please·enter·an·integer");
scan.next();
}
}
while(true){
try{
System.out.println("Enter·an·integer: ");
secondNum = scan.nextInt();
break;

}
catch(InputMismatchException e){
System.out.println("Please·enter·an·integer");
scan.next();
}

}
int sum = firstNum + secondNum;
System.out.println("The sum of two numbers is : "+sum);

}

}

Output:

Enter·an·integer:
2.5
Please·enter·an·integer
Enter·an·integer:
4.6
Please·enter·an·integer
Enter·an·integer:
hello
Please·enter·an·integer
Enter·an·integer:
7
Enter·an·integer:
5.6
Please·enter·an·integer
Enter·an·integer:
9.4
Please·enter·an·integer
Enter·an·integer:
10
The sum of two numbers is : 17

import java.util.Scanner;

public class NumberFormat {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

try{

System.out.println("Enter a binary string:");

String s = scan.next();

int integerValue = bin2Dec(Integer.parseInt(s));

System.out.println("The decimal value of "+s+" is "+integerValue);

}

catch(NumberFormatException e){

System.out.println(e);

}

}

public static int bin2Dec(int binaryNumber) throws NumberFormatException{

int decimal = 0;

int p = 0;

try{

while(true){

if(binaryNumber == 0){

break;

} else {

int temp = binaryNumber%10;

decimal += temp*Math.pow(2, p);

binaryNumber = binaryNumber/10;

p++;

}

}

}

catch(NumberFormatException e){

throw new NumberFormatException("Invalid binary number");

}

return decimal;

}

}

Output:

Enter a binary string:
1011000101
The decimal value of 1011000101 is 709

RmvText.java

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class RmvText {

public static void main(String[] args) throws FileNotFoundException {

String word = args[0];

String filename = args[1];

Scanner scan = new Scanner(new File(filename ));

while(scan.hasNextLine()){

String line = scan.nextLine();

line = line.replaceAll(word,"");

System.out.println(line);

}

}

}

Output:

Amazing
cxc
cxz
gggdf
cfff
CC
But we Can't
Do
Everything For Goodness sake.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote