Last active
December 15, 2015 13:39
-
-
Save shade34321/5268748 to your computer and use it in GitHub Desktop.
Write a program with the following requirements: 1. ask the user to enter an integer. Then display what the user entered back on the screen if the user entered an integer ("you entered ..."). If the user enter something other than an integer then display on the screen a message that says "you did not enter an integer". 2. next, the program asks …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.text.SimpleDateFormat; | |
import java.util.*; | |
import java.lang.SecurityException; | |
public class ExceptionHandling{ | |
private Scanner input; | |
public ExceptionHandling(){ | |
input = new Scanner(System.in); //so we can take in the users input | |
input.useDelimiter(System.getProperty("line.separator")); | |
try{ | |
getInt(); | |
getString(); | |
} | |
catch(MyNumberException mne){ | |
System.err.println(mne.getMessage("You did not enter an integer.")); //apparently the user can't follow directions | |
writeToLog("You did not enter an integer."); //outputting to log file | |
} | |
} | |
public void writeToLog(String message){ | |
Formatter output; | |
try{ | |
output = new Formatter(new BufferedWriter(new FileWriter("ExceptionHandlingLog.txt",true))); | |
String timeStamp = new SimpleDateFormat("yyyyMMDD_HHmmss").format(Calendar.getInstance().getTime()); | |
output.format("%s - %s\n", timeStamp, message); | |
output.close(); | |
} | |
catch(FileNotFoundException fnf){ | |
System.err.println("Error opening or creating file."); | |
System.exit(1); | |
} | |
catch(Exception e){ | |
System.err.println("Error with file I/O."); | |
System.out.println(e.getStackTrace()); | |
} | |
} | |
public boolean checkString(String str) throws MyNumberException{ | |
if(str.matches("^[-+]?\\d+(\\.\\d+)?$")) //is it a string? | |
throw new MyNumberException(); //apparently not | |
else{ | |
return true; | |
} | |
} | |
public void getString(){ | |
try | |
{ | |
System.out.println("Please enter a string."); //asking user to enter a string | |
String str = input.nextLine(); //getting user input | |
checkString(str); //checking the user input | |
System.out.println("You entered " + str); //what did the user enter? | |
} | |
catch (MyNumberException mne) { | |
// TODO Auto-generated catch block | |
System.err.println(mne.getMessage("You did not enter a string.")); //apparently the user can't follow directions | |
writeToLog("You did not enter a string."); //outputting to log file | |
} | |
} | |
public void getInt() throws MyNumberException{ | |
try{ | |
System.out.println("Please enter a integer"); //asks user to enter a integer | |
int num = input.nextInt(); //takes in the users input | |
System.out.println("You entered " + num); //displays what the user entered | |
input.nextLine(); //to clear out the input | |
} | |
catch(InputMismatchException ime){ | |
input.nextLine(); //to clear it out the input | |
throw new MyNumberException(); | |
} | |
} | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
ExceptionHandling eh = new ExceptionHandling(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyNumberException extends Exception{ | |
public String getMessage(String error){ | |
return error; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment