+ 2
[SOLVED] Related to exception checking (IOException) in Java.
https://code.sololearn.com/cweTji2REm4f/?ref=app I don't get why this code is generating an error. Please help me. I'll be so grateful.
11 odpowiedzi
+ 6
Did not go through the whole code but just to remind you that IOException comes under checked exception which should be handled at all times.
You can do that by either using the throws clause following the main method like-
throws IOException OR
Put the code inside main method into a try block and followed by-
catch(IOException a) {
System.out.println("Exception caught");
}
+ 4
there is interchanged alphabet and inp
// if(!(inp.contains(Character.toString(alphabets.charAt(i))))){
//..
try { // need to catch an exception from vigenere()
vigenere(inp, key);
} catch(IllegalArgumentException e) {
System.out.println(e.getMessage() );
}
} // end of main()
public static void vigenere(String inp, String key) throws IllegalArgumentException {
//function to encrypt by vigenere vipher.
String alphabets = "abcdefghijklmnopqrstuvwxyz";
for(int i=0; i<inp.length(); i++){
// changed here <<<<
// if(!(inp .contains(Character.toString(alphabets.charAt(i))))){
if(!(alphabets.contains(Character.toString(inp .charAt(i))))){
throw new IllegalArgumentException("input must not contain any numbers or special characters.");
}
+ 3
I am wondering why your vignere() method throws an IOException. Your are not working with files or sockets or something like this.
https://stackoverflow.com/questions/13216148/java-what-throws-an-ioexception
+ 2
Denise Roßberg That can be done, or probably create a separate method which checks whether the input meets all the necessary criteria.
Or do a simple check in the main() itself.
We can avoid exceptions this way.
+ 2
zemiak
Denise Roßberg
Thank you for all of your assistance.
I understand it much better now then before.
+ 2
Maybe this article is also helpful for you:
https://stackoverflow.com/questions/77127/when-to-throw-an-exception
+ 2
Well, now I'm planning to try a different approach. I'll create a segregate class- Vigenere, and in it I'll make two methods- encrypt and decrypt.
+ 1
Avinesh Thank you very much
+ 1
use IllegalArgumentException, it is not checked and you do not have to catch it, but then you will not get a message from it.
+ 1
Abdul Samad
I wouldn't throw an exception at all. The method could return the string without modification.
In my opinion, the program that implements vignere () should ensure that the user cannot make incorrect entries.
Avinesh zemiak
What do you think about it?
0
Denise Roßberg Because I've designed it to do so. So should I make any method to throw IOException only when it is dealing with files or sockets.
My vigenere () method takes two arguments which mustn't contain any special character or number. This sounds like a fault in the parameters (which are the inputs) given to the method, so I made it to throw IOException.
Should I change it to throw some other kind of excpetion? If yes then please tell me which one, Sir.
I'm a beginner in Java.