Created
January 25, 2016 13:34
-
-
Save Viacheslav77/1c25c30e7bd5b69a6c28 to your computer and use it in GitHub Desktop.
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.util.Scanner; | |
public class Myclass { | |
public static class TextExceptions extends Exception { | |
public TextExceptions (String message){ | |
super(message); | |
} | |
@Override | |
public String getMessage(){ | |
return "TextExceptions" + super.getMessage(); | |
} | |
} | |
public static class NumeralExceptions extends Exception { | |
public NumeralExceptions (String message){ | |
super(message); | |
} | |
@Override | |
public String getMessage(){ | |
return "NumeralExceptions" + super.getMessage(); | |
} | |
} | |
public static class InputStrNum { | |
private String io; | |
Scanner s = new Scanner(System.in); | |
public String SetStr() throws TextExceptions{ | |
System.out.print("Введите буквы :"); | |
io = s.next(); | |
StringBuilder sb = new StringBuilder(io); | |
for(int i = 0; i < sb.length(); i++){ | |
StringBuilder sb1 = new StringBuilder ("-0123456789"); | |
for(int j = 0; j < sb1.length(); j++){ | |
if (sb.charAt(i)==sb1.charAt(j)){ | |
throw new TextExceptions (" Недопустимые символы"); | |
} | |
} | |
} | |
return io; | |
} | |
public String SetNum() throws NumeralExceptions{ | |
System.out.print("Введите цифры :"); | |
io = s.next(); | |
StringBuilder sb = new StringBuilder(io); | |
for(int i = 0; i < sb.length(); i++){ | |
if (sb.charAt(i)>=97&&sb.charAt(i)<=122){ | |
throw new NumeralExceptions (" Недопустимые символы"); | |
} | |
} | |
return io; | |
} | |
} | |
public static void main(String [] args){ | |
InputStrNum i = new InputStrNum(); | |
for(;;){ | |
try { | |
System.out.println(i.SetStr()); | |
} catch (TextExceptions e) { | |
System.out.println (e.getMessage()); | |
} | |
try { | |
System.out.println(i.SetNum()); | |
} catch (NumeralExceptions e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment